數(shù)據(jù)庫(kù)
第一步
創(chuàng)建一個(gè)新工程净薛,創(chuàng)建一個(gè)class這個(gè)class繼承于SQLiteOpenHelper這個(gè)類,并實(shí)現(xiàn)兩個(gè)方法和一個(gè)構(gòu)造方法第二步:調(diào)用SQLiteOpenHelper這個(gè)類的構(gòu)造方法啊胶,先創(chuàng)建一個(gè)對(duì)象,然后調(diào)用創(chuàng)建數(shù)據(jù)庫(kù)的方法 參數(shù)1:許你一個(gè)上下文 參數(shù)2:數(shù)據(jù)庫(kù)名稱 參數(shù)3:null 參數(shù)四:不能小于1垛贤;
MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);
SQLiteDatabase db = oh.getWritableDatabase();//如果數(shù)據(jù)庫(kù)不存在创淡,先創(chuàng)建數(shù)據(jù)庫(kù) ,獲取一個(gè)可讀可寫數(shù)據(jù)庫(kù)對(duì)象第三步:創(chuàng)建數(shù)據(jù)庫(kù)表
@Override
public void onCreate(SQLiteDatabase arg0) {
//創(chuàng)建數(shù)據(jù)庫(kù)表
arg0.execSQL("create table person(_id integer primary key autoincrement, name char(10),salary char(20),phone integer(20) )");
}-
第四步:創(chuàng)建表之后就可以進(jìn)行表的增刪改查了
public void insert(){
MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);
SQLiteDatabase db = oh.getWritableDatabase();
db.execSQL("insert into person(name,salary,phone)values(?,?,?)", new Object[]{"小志志卡","13000",138438});
db.close();
}
public void delete(){
MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);
SQLiteDatabase db = oh.getWritableDatabase();
db.execSQL("delete from person where name = ?", new Object[]{"小志"});
db.close();
}public void updata(){
MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);
SQLiteDatabase db = oh.getWritableDatabase();
db.execSQL("update person set phone = ? where name = ?", new Object[]{123,"小志志卡"});
db.close();
} -
上面的方法容易出錯(cuò)南吮,可以用封裝好的api來進(jìn)行增刪改查
public void deleteApi(){
MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);
SQLiteDatabase db = oh.getWritableDatabase();
int i = db.delete("person", "name = ?", new String[]{"游天龍"});
db.close();
}public void updateApi(){
ContentValues values = new ContentValues();
values.put("salary", 1300);
MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);
SQLiteDatabase db = oh.getWritableDatabase();
db.update("person", values, "name = ?", new String[]{"游天龍"});
db.close();
}public void selectApi(){
ContentValues values = new ContentValues();
values.put("salary", 1300);
MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);
SQLiteDatabase db = oh.getWritableDatabase();
Cursor cursor = db.query("person", null, null, null, null, null, null, null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
String salary = cursor.getString(cursor.getColumnIndex("salary"));
}
db.close();
} 事務(wù):要成功一起成功,要失敗都失敗
public void transaction(){
MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);
SQLiteDatabase db = oh.getWritableDatabase();
db.beginTransaction();
ContentValues values = new ContentValues();
values.put("salary", 12000);
db.update("person", values, "name = ?", new String[]{"小志卡"});
values.clear();
values.put("salary", 14000);
db.update("person", values, "name = ?", new String[]{"小志志卡"});
db.setTransactionSuccessful();
db.endTransaction();
db.close();
}