android 数据库sqlite的使用

android开发的过程中,不可避免的有需要存储数据的时候。

android的数据存取机制有很多,其中最重要的大概就是sqlite数据库了

sqlite 数据库是android自带的数据库。不需要任何别的插件。

sqlite的使用步骤大概有两步

1.编写SQLiteOpenHelper类

这个类用户封装数据库表的建立、数据库的获得等操作

2.编写TableManager类

这个类用户封装某个表的各种操作

1、SQLiteOpenHelper类

public class MySqliteOpenHelper extends SQLiteOpenHelper {

    public MySqliteOpenHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub
                //以下创建表的语句,执行括号中的sql语句,创建表。同一个数据库中的所有表都在这里创建,
        arg0.execSQL("CREATE TABLE IF NOT EXISTS tb_Depot"
                + "(id interger PRIMARY KEY,DepotSite varchar,DepotAcreage interger,DepotPrice interger,Linkman varchar,Phone varchar,Content varchar)");
        arg0.execSQL("CREATE TABLE IF NOT EXISTS tb_Freight"
                + "(id interger PRIMARY KEY,start varchar,terminal varchar,FreightType varchar,FreightWeight int,linkman varchar,phone varchar, content varchar)");
        arg0.execSQL("CREATE TABLE IF NOT EXISTS tb_User"
                + "(id interger PRIMARY KEY,UserName varchar,UserPass varchar,CompanyName varchar,address varchar,phone varchar,email varchar,permission varchar)");
        arg0.execSQL("CREATE TABLE IF NOT EXISTS tb_Chauffeur"
                + "(id interger primary key,ChauffeurName varchar,trucktype varchar,tel varchar,effectdate varchar,content varchar)");
        arg0.execSQL("CREATE TABLE IF NOT EXISTS tb_Truck"
                + "(id interger primary key,TruckNumber varchar,TruckWeight varchar,TruckPrice varchar,Start varchar,Whither varchar,linkman varchar,phone varchar)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub
        //如果需要用到更新数据库的话,在这里进行操作
    }

}

然后是封装了数据库表操作的manager类

public class DepotDBManager {
    private MySqliteOpenHelper helper ;
    private SQLiteDatabase db;

    public DepotDBManager(Context context){
        //实例化一个sqliteopenhelpr 类
        helper = new MySqliteOpenHelper(context,"depot.db",null,1);
        //获得一个可读写的数据库
        db = helper.getWritableDatabase();
    }

    public void add(DepotValue depotValue){
        //这是往表里增加数据的类,系统为我们封装好了一套工具,我们可以不用再写sql语句了
        ContentValues newValues = new ContentValues();
        newValues.put("Id", depotValue.getId());
        newValues.put("DepotSite", depotValue.getDepotSite());
        newValues.put("DepotAcreage", depotValue.getDepotAcreage());
        newValues.put("DepotPrice", depotValue.getDepotPrice());
        newValues.put("Linkman", depotValue.getLinkman());
        newValues.put("Phone", depotValue.getPhone());
        newValues.put("Content", depotValue.getContent());

        db.insert("tb_Depot",null, newValues);
    }

    public List query(){
        //这是查询操作 返回值是一个封装了数据库表信息的value类的链表
        ArrayList depotvalues = new ArrayList();
        Cursor c=queryTheCursor();

        while(c.moveToNext()){
            DepotValue depot = new DepotValue();
            depot.setId(c.getInt(c.getColumnIndex("id")));

            depot.setDepotSite(c.getString(c.getColumnIndex("DepotSite")));
            Log.i("chacuo","3332");
            depot.setDepotAcreage(c.getInt(c.getColumnIndex("DepotAcreage")));
            depot.setDepotPrice(c.getInt(c.getColumnIndex("DepotPrice")));
            depot.setLinkman(c.getString(c.getColumnIndex("Linkman")));
            depot.setPhone(c.getString(c.getColumnIndex("Phone")));
            depot.setContent(c.getString(c.getColumnIndex("Content")));

            depotvalues.add(depot);
            Log.i("hahaha",depot.getId()+depot.getContent());
        }
        c.close();
        return depotvalues;
    }
    public void deletebyid(int id){
        db.execSQL("delete from tb_Depot where id="+id);
    }
    public Cursor queryTheCursor(){
        Cursor c = db.rawQuery("select * from tb_Depot", null);
        return c;
    }
}

你可能感兴趣的:(android,数据库,sqlite,Android)