WEB开发网
开发学院手机开发Android 开发 Android SQLiteDatabase 使用 阅读

Android SQLiteDatabase 使用

 2010-07-01 14:10:00 来源:WEB开发网   
核心提示:+ COUNTRY + " VARCHAR,"+ CODE + " INTEGER)");}public void onUpgrade(SQLiteDatabase db,int oldVersion, int newVersion) {db.execSQL("DROP

+ COUNTRY + " VARCHAR,"

+ CODE + " INTEGER)");

}

public void onUpgrade(SQLiteDatabase db,

int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS "+TB_NAME);

onCreate(db);

}

}

2. 从SQLiteOpenHelper 得到SQLiteDatabase的实例

Java代码

DBHelper helper = new DBHelper(this, DB_NAME, null, VERSION);

SQLiteDatabase db = helper.getWritableDatabase();

DBHelper helper = new DBHelper(this, DB_NAME, null, VERSION);

SQLiteDatabase db = helper.getWritableDatabase();

3. SQLiteDatabase 的一些操作:

* 插入数据:

Java代码

ContentValues values = new ContentValues();

values.put(DBHelper.COUNTRY, "中国");

values.put(DBHelper.CODE, 86);

db.insert(DBHelper.TB_NAME,DBHelper.ID, values);

ContentValues values = new ContentValues();

values.put(DBHelper.COUNTRY, "中国");

values.put(DBHelper.CODE, 86);

db.insert(DBHelper.TB_NAME,DBHelper.ID, values);

* 改动数据

Java代码

db.insert(DBHelper.TB_NAME,DBHelper.ID,null);

values.clear();

values.put(DBHelper.COUNTRY, "意大利");

values.put(DBHelper.CODE, 39);

db.update(DBHelper.TB_NAME, values,DBHelper.ID + " = 2",null);

db.insert(DBHelper.TB_NAME,DBHelper.ID,null);

values.clear();

values.put(DBHelper.COUNTRY, "意大利");

values.put(DBHelper.CODE, 39);

db.update(DBHelper.TB_NAME, values,DBHelper.ID + " = 2",null);

* execSQL 执行SQL语言

Java代码

db.execSQL("INSERT INTO "

+ DBHelper.TB_NAME + "("

+ DBHelper.COUNTRY + ","

+ DBHelper.CODE + ") VALUES "

+ "('洪都拉斯',504)");

db.execSQL("INSERT INTO "

+ DBHelper.TB_NAME + "("

Tags:Android SQLiteDatabase 使用

编辑录入:coldstar [复制链接] [打 印]
赞助商链接