[java]view plaincopy
print?
publicclassdbHelperextendsSQLiteOpenHelper{
privatefinalstaticString Database_name="hyb_db";//数据库的名字
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";
//在构造函数中创建数据库
publicdbHelper(Context context)
{
super(context,Database_name,null,1);
}
@Override//创建表,重写抽象方法,当数据库第一次被创建时会调用该方法,若数据库已存在则不调用
publicvoidonCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//自增长类型
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//重写抽象方法,OnUpgrade方法在数据库版本升级时会被调用
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);
}
//查询数据库中的数据
publicCursor select()
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.query(Table_name,null,null,null,null,null," _id desc");
returncursor;
}
//查询数据库中的数据
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)};//找到要删除的目标
db.delete(Table_name, where, whereValue);
}
//删除
publicvoiddelete_history(intid)
{
SQLiteDatabase db=this.getWritableDatabase();
String where=History_id+"=?";//删除条件
String[] whereValue={Integer.toString(id)};//找到要删除的目标
db.delete(Table_history, where, whereValue);
}
publiclonginsert(Book u)//u为准备要插入的数据
{
SQLiteDatabase db=this.getWritableDatabase();//以读写方式打开数据库
//使用类似map键值对映射的数据结构ContentValues来指定插入的数据
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为准备要插入的数据
{
SQLiteDatabase db=this.getWritableDatabase();//以读写方式打开数据库
//使用类似map键值对映射的数据结构ContentValues来指定插入的数据
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)};//找到修改的目标
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);
}
}