Android SQLite3 基础入门
2010-03-25 16:47:00 来源:WEB开发网public void onClick(View v) {
if (v. getId_r() == R.id.btnAdd) {
// 增加
db.save(inputTxt. getText_r().toString());
db.close();
} else if (v. getId_r() == R.id.btnViewAll) {
// 浏览所有数据
Cursor cur = db.loadAll();
StringBuffer sf = new StringBuffer();
cur.moveToFirst();
while (!cur.isAfterLast()) {
sf.append(cur. getInt_r(0)).append(" : ").append(
cur. getString_r(1)).append(" ");
cur.moveToNext();
}
db.close();
viewAll.setText(sf.toString());
}
}
};
btnAdd.setOnClickListener(listener);
btnViewAll.setOnClickListener(listener);
}
}
package org.imti;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class DBHelper {
private static final String TAG = "DBDemo_DBHelper";// 调试标签
private static final String DATABASE_NAME = "dbdemo.db";// 数据库名
SQLiteDatabase db;
Context context;//应用环境上下文 Activity 是其子类
DBHelper(Context _context) {
context = _context;
//开启数据库
db = context.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE,null);
CreateTable();
Log.v(TAG, "db path=" + db. getPath_r());
}
public void CreateTable() {
try {
db.execSQL("CREATE TABLE t_user (" +
"_ID INTEGER PRIMARY KEY autoincrement,"
+ "NAME TEXT"
+ ");");
Log.v(TAG, "Create Table t_user ok");
} catch (Exception e) {
Log.v(TAG, "Create Table t_user err,table exists.");
}
}
public boolean save(String uname){
更多精彩
赞助商链接