Android数据库相关代码解读之 SQLLite
2010-05-19 16:17:00 来源:WEB开发网} catch (Exception e) {
Log.v(TAG, "insert Table TestUser 1 record fail");
return false;
} finally {
//this.db.close();
Log.v(TAG, "insert Table TestUser ");
}
}
public Cursor loadAll() {
Cursor cur = db.query("TestUser", new String[]
{ "ID", "NAME","SEX","AGES"}, null,
null, null, null, null);
return cur;
}
public void open(Context context){
if (null == db || !this.db.isOpen()){
this.context = context;
this.db = context.openOrCreateDatabase(this.DataBaseName,
context.MODE_PRIVATE, null);
createTabel();
Log.v(this.TAG, "create or Open DataBase。。。");
}
}
public void close() {
db.close();
}
}
package com.topsun;
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 = "UserDB_DBHelper.java";
private static final String DataBaseName = "UserDB";
SQLiteDatabase db;
Context context;
public DBHelper(Context context) {
this.open(context);
}
private void createTabel() {
// TODO Auto-generated method stub
String sql = "";
try {
sql = "CREATE TABLE IF NOT EXISTS TestUser
(ID INTEGER PRIMARY KEY autoincrement,
NAME TEXT, SEX TEXT, AGES INTEGER)";
this.db.execSQL(sql);
Log.v(TAG, "Create Table TestUser ok");
} catch (Exception e) {
Log.v(TAG, "Create Table TestUser fail");
} finally {
//this.db.close();
Log.v(TAG, "Create Table TestUser ");
更多精彩
赞助商链接