android开发之-数据库SQLite

public class dbHelper extends SQLiteOpenHelper{

	    private final static String Database_name="hyb_db";//数据库的名字
	    private final static String Table_name="book";//表的名字
	    private final static String Table_history="history";//表的名字
	    
	  //表 1 中的字段属性信息
	    public final static String Book_id="_id"; 
	    public final static String Book_image="image";
	    public final static String Book_name="name";
	    public final static String Book_path="path";
	    
	    
	    //表 2 中的字段属性信息
	    public final static String History_id="_id"; 
	    public final static String History_name="h_name";
	    public final static String History_path="h_path";
	    public final static String History_time="h_time";
		   
	    
	    
	    
	    //在构造函数中创建数据库
	    public dbHelper(Context context)
	    {
	        super(context,Database_name,null, 1);
	    }
	    @Override//创建表,重写抽象方法,当数据库第一次被创建时会调用该方法,若数据库已存在则不调用
	    public void onCreate(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方法在数据库版本升级时会被调用
	    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	        // 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);
	    }

	    //查询数据库中的数据
	    public Cursor select()
	    {
	        SQLiteDatabase db=this.getReadableDatabase();
	        Cursor cursor=db.query(Table_name, null, null, null, null, null,  " _id desc");
	        return cursor;
	    }
	    //查询数据库中的数据
	    public Cursor select_history()
	    {
	        SQLiteDatabase db=this.getReadableDatabase();
	        Cursor cursor=db.query(Table_history, null, null, null, null, null,  " _id desc");
	        return cursor;
	    }
	    //删除
	    public void delete(int id)
	    {
	        SQLiteDatabase db=this.getWritableDatabase();
	        String where=Book_id+"=?";//删除条件
	        String[] whereValue={Integer.toString(id)};//找到要删除的目标
	        db.delete(Table_name, where, whereValue);
	    }
	    //删除
	    public void delete_history(int id)
	    {
	        SQLiteDatabase db=this.getWritableDatabase();
	        String where=History_id+"=?";//删除条件
	        String[] whereValue={Integer.toString(id)};//找到要删除的目标
	        db.delete(Table_history, where, whereValue);
	    }
	    
	   
	    
	    public long insert(Book u)//u为准备要插入的数据
	    {

	        SQLiteDatabase db=this.getWritableDatabase();//以读写方式打开数据库
	        //使用类似map键值对映射的数据结构ContentValues来指定插入的数据
	        ContentValues cv=new ContentValues(); 
	        cv.put(Book_image,R.drawable.book_list);
	        cv.put(Book_name,u.getName());
	        cv.put(Book_path,u.getPath());
	     
	        
	        
	        long row=db.insert(Table_name, null, cv);
	        return row;
	    }
	    
	    public long insert_history(History u)//u为准备要插入的数据
	    {

	        SQLiteDatabase db=this.getWritableDatabase();//以读写方式打开数据库
	        //使用类似map键值对映射的数据结构ContentValues来指定插入的数据
	        ContentValues cv=new ContentValues(); 
	      
	        cv.put(History_name,u.getName());
	        cv.put(History_path,u.getPath());
	        cv.put(History_time,u.getTime());
	     
	        
	        
	        long row=db.insert(Table_history, null, cv);
	        return row;
	    }
	    
	    public Cursor query_name(String name) 
	    {
	        SQLiteDatabase db = this.getReadableDatabase();
	        return db.rawQuery("select* from "+Table_name+" where name like '%" + name + "%' limit 10",null);
	     }
	  //修改
	    public void update_history(int id,History u)
	    {
	        SQLiteDatabase db=this.getWritableDatabase();
	        String where=History_id+"=?";//修改条件
	        
	        String[] whereValue={Integer.toString(id)};//找到修改的目标
	        ContentValues cv=new ContentValues(); 
	        
	        //新的值
	        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);
	    }
	
	    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值