在 Android 里使用数据库
2010-02-24 04:45:00 来源:WEB开发网mOpenHelper = new DatabaseHelper(this);
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
// 在数据库中插入两条记录
ContentValues values = new ContentValues();
values.put(COL_NAME, "zhangshan");
values.put(COL_TEL, "13512341234");
long result = db.insert(CONT_TABLE_NAME, COL_NAME, values);
values.clear();
values.put(COL_NAME, "lisi");
values.put(COL_TEL, "13512341238");
result = db.insert(CONT_TABLE_NAME, COL_NAME, values);
// query
// 把所有记录查上来,显示到屏幕中
String[] columns = new String[]{
COL_NAME,
COL_TEL,};
Cursor c = db.query(CONT_TABLE_NAME, columns, "name = 'zhangshan'", null, null, null, null);
c.moveToFirst();
while(!c.isAfterLast())
{
tv.append(c.getString(0));
tv.append(" - ");
tv.append(c.getString(1));
tv.append(" ");
c.moveToNext();
}
// update
values.clear();
values.put(COL_NAME, "lisi");
values.put(COL_TEL, "13599999999");
db.update(CONT_TABLE_NAME, values, "name = 'zhangshan'", null);
c = db.query(CONT_TABLE_NAME, columns, null, null, null, null, null);
c.moveToFirst();
while(!c.isAfterLast())
{
tv.append(c.getString(0));
tv.append(" - ");
tv.append(c.getString(1));
tv.append(" ");
c.moveToNext();
}
// delete
db.delete(CONT_TABLE_NAME, "name = 'lisi'", null);
}
}
更多精彩
赞助商链接