創(chuàng)建數(shù)據(jù)步驟
1唧瘾、創(chuàng)建一個類 繼承 SqliteOpenHelper 實現(xiàn) onCreate?onUpgrade
2片部、創(chuàng)建這個helper類的一個對象打颤,調(diào)用getReadableDatabase()方法桨武,會幫我們創(chuàng)建打開一個數(shù)據(jù)庫
DBHelper helper =new DBHelper(mContext);
//調(diào)用getReadableDatabase方法财骨,初始化數(shù)據(jù)庫SQLiteDatabase db = helper.getReadableDatabase();
3镐作、增加 刪除 ?修改 查詢
DBHelper helper =new DBHelper(mContext);
//調(diào)用getReadableDatabase方法,初始化數(shù)據(jù)庫
SQLiteDatabase db = helper.getReadableDatabase();
String name = "張三";
db.execSQL ( "insert into user(name) values(?)",(new Object[]{name}));
db.close();
DBHelper helper =new DBHelper(mContext);
//調(diào)用getReadableDatabase方法隆箩,初始化數(shù)據(jù)庫
SQLiteDatabase db = helper.getReadableDatabase();
String name = "沈震";
db.execSQL ( "delete from user where id=?",(new Object[]{1}));
db.close();
DBHelper helper =new DBHelper(mContext);
//調(diào)用getReadableDatabase方法该贾,初始化數(shù)據(jù)庫
SQLiteDatabase db = helper.getReadableDatabase();
String name = "沈震1";
int id = 1;
db.execSQL ( "update user set name=? where id=?",(new Object[]{name,id}));
db.close();
DBHelper helper =new DBHelper(mContext);
//調(diào)用getReadableDatabase方法,初始化數(shù)據(jù)庫
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery ("select * from user",(new String[]{}));
if (cursor!=null&&cursor.getCount ()>0){
? ? ? ? ? ? ? ? ?while (cursor.moveToNext ()){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String name = cursor.getString (1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Log.i ( "queryDB", "queryDB: "+name );
? ? ? ? ? ? ? ? ? }
? ? ?}
? db.close ();