Android SQLiteDatabase 使用
2010-07-01 14:10:00 来源:WEB开发网SQLiteDatabase
[功能]
SQLiteDatabase 是关于数据库操作的 可用于 insert delete update query 等操作 可惜美中不足的是:
1. 其不支持创建数据库
2. 其不支持版本更新 或者说其不知道如何做 因为具体数据的差异
鉴于以上的缺陷 有一个辅助类可以完成上面功能 那就是:SQLiteOpenHelper
[代码]
1. 定义SQLiteOpenHelper 并完成 创建 更新 功能
Java代码
public class DBHelper extends SQLiteOpenHelper {
public static final String TB_NAME = "mycountry";
public static final String ID = "_id";
public static final String COUNTRY = "country";
public static final String CODE = "code";
public DBHelper(Context context, String name,
CursorFactory factory,int version) {
super(context, name, factory, version);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS "
+ TB_NAME + " ("
+ ID + " INTEGER PRIMARY KEY,"
+ COUNTRY + " VARCHAR,"
+ CODE + " INTEGER)");
}
public void onUpgrade(SQLiteDatabase db,
int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TB_NAME);
onCreate(db);
}
}
public class DBHelper extends SQLiteOpenHelper {
public static final String TB_NAME = "mycountry";
public static final String ID = "_id";
public static final String COUNTRY = "country";
public static final String CODE = "code";
public DBHelper(Context context, String name,
CursorFactory factory,int version) {
super(context, name, factory, version);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS "
+ TB_NAME + " ("
+ ID + " INTEGER PRIMARY KEY,"
Tags:Android SQLiteDatabase 使用
编辑录入:coldstar [复制链接] [打 印]更多精彩
赞助商链接