[java]view plaincopy
publicclassdbHelperextendsSQLiteOpenHelper{
privatefinalstaticString?Database_name="hyb_db";//數(shù)據(jù)庫(kù)的名字
privatefinalstaticString?Table_name="book";//表的名字
privatefinalstaticString?Table_history="history";//表的名字
//表?1?中的字段屬性信息
publicfinalstaticString?Book_id="_id";
publicfinalstaticString?Book_image="image";
publicfinalstaticString?Book_name="name";
publicfinalstaticString?Book_path="path";
//表?2?中的字段屬性信息
publicfinalstaticString?History_id="_id";
publicfinalstaticString?History_name="h_name";
publicfinalstaticString?History_path="h_path";
publicfinalstaticString?History_time="h_time";
//在構(gòu)造函數(shù)中創(chuàng)建數(shù)據(jù)庫(kù)
publicdbHelper(Context?context)
{
super(context,Database_name,null,1);
}
@Override//創(chuàng)建表,重寫(xiě)抽象方法,當(dāng)數(shù)據(jù)庫(kù)第一次被創(chuàng)建時(shí)會(huì)調(diào)用該方法,若數(shù)據(jù)庫(kù)已存在則不調(diào)用
publicvoidonCreate(SQLiteDatabase?db)?{
//?TODO?Auto-generated?method?stub
//自增長(zhǎng)類(lèi)型
String?sql="Create?table?"+Table_name+"("+Book_id+"?integer?primary?key?autoincrement,"+Book_image+"?varchar(50),"+Book_name+"?varchar(200),"+Book_path+"?varchar(200))";
db.execSQL(sql);
String?sql2="Create?table?"+Table_history+"("+History_id+"?integer?primary?key?autoincrement,"+History_name+"?varchar(50),"+History_path+"?varchar(200),"+History_time+"?varchar(200))";
db.execSQL(sql2);
}
@Override//重寫(xiě)抽象方法,OnUpgrade方法在數(shù)據(jù)庫(kù)版本升級(jí)時(shí)會(huì)被調(diào)用
publicvoidonUpgrade(SQLiteDatabase?db,intoldVersion,intnewVersion)?{
//?TODO?Auto-generated?method?stub
String?sql="?drop?table?if?exists?user?TABLE?IF?EXISTS?"+Table_name;
db.execSQL(sql);
String?sql2="?drop?table?if?exists?user?TABLE?IF?EXISTS?"+Table_history;
db.execSQL(sql2);
onCreate(db);
}
//查詢(xún)數(shù)據(jù)庫(kù)中的數(shù)據(jù)
publicCursor?select()
{
SQLiteDatabase?db=this.getReadableDatabase();
Cursor?cursor=db.query(Table_name,null,null,null,null,null,"?_id?desc");
returncursor;
}
//查詢(xún)數(shù)據(jù)庫(kù)中的數(shù)據(jù)
publicCursor?select_history()
{
SQLiteDatabase?db=this.getReadableDatabase();
Cursor?cursor=db.query(Table_history,null,null,null,null,null,"?_id?desc");
returncursor;
}
//刪除
publicvoiddelete(intid)
{
SQLiteDatabase?db=this.getWritableDatabase();
String?where=Book_id+"=?";//刪除條件
String[]?whereValue={Integer.toString(id)};//找到要?jiǎng)h除的目標(biāo)
db.delete(Table_name,?where,?whereValue);
}
//刪除
publicvoiddelete_history(intid)
{
SQLiteDatabase?db=this.getWritableDatabase();
String?where=History_id+"=?";//刪除條件
String[]?whereValue={Integer.toString(id)};//找到要?jiǎng)h除的目標(biāo)
db.delete(Table_history,?where,?whereValue);
}
publiclonginsert(Book?u)//u為準(zhǔn)備要插入的數(shù)據(jù)
{
SQLiteDatabase?db=this.getWritableDatabase();//以讀寫(xiě)方式打開(kāi)數(shù)據(jù)庫(kù)
//使用類(lèi)似map鍵值對(duì)映射的數(shù)據(jù)結(jié)構(gòu)ContentValues來(lái)指定插入的數(shù)據(jù)
ContentValues?cv=newContentValues();
cv.put(Book_image,R.drawable.book_list);
cv.put(Book_name,u.getName());
cv.put(Book_path,u.getPath());
longrow=db.insert(Table_name,null,?cv);
returnrow;
}
publiclonginsert_history(History?u)//u為準(zhǔn)備要插入的數(shù)據(jù)
{
SQLiteDatabase?db=this.getWritableDatabase();//以讀寫(xiě)方式打開(kāi)數(shù)據(jù)庫(kù)
//使用類(lèi)似map鍵值對(duì)映射的數(shù)據(jù)結(jié)構(gòu)ContentValues來(lái)指定插入的數(shù)據(jù)
ContentValues?cv=newContentValues();
cv.put(History_name,u.getName());
cv.put(History_path,u.getPath());
cv.put(History_time,u.getTime());
longrow=db.insert(Table_history,null,?cv);
returnrow;
}
publicCursor?query_name(String?name)
{
SQLiteDatabase?db?=this.getReadableDatabase();
returndb.rawQuery("select*?from?"+Table_name+"?where?name?like?'%"+?name?+"%'?limit?10",null);
}
//修改
publicvoidupdate_history(intid,History?u)
{
SQLiteDatabase?db=this.getWritableDatabase();
String?where=History_id+"=?";//修改條件
String[]?whereValue={Integer.toString(id)};//找到修改的目標(biāo)
ContentValues?cv=newContentValues();
//新的值
cv.put(History_name,u.getName());
cv.put(History_path,u.getPath());
cv.put(History_time,u.getTime());
db.update(Table_history,?cv,?where,?whereValue);
}
}