Android SQLite数据库 《第一行代码》

SQLite是Android自带的数据库,为了方便管理,Android提供了一个SQLiteOpenHelper的类来帮助管理数据库。但是SQLiteOpenHelper是一个抽象类,我们使用的时候需要创建自己的一个类去继承,并且实现抽象类中的两个方法onCreate()和onUpgrade()方法,onCreate()方法在创建数据库表的时候调用,onUpgrade()方法在更新数据库表到时候调用。

一、创建数据库表

public class MyDatabaseHelper extends SQLiteOpenHelper {

    public static final String CREATE_BOOK = "create table Book(" +
            "id integer primary key autoincrement," +
            "author text," +
            "price real," +
            "pages integer," +
            "name text)";
    
    private Context mContext;

    public MyDatabaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version){
        super(context,name,factory,version);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(CREATE_BOOK);
        //Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
        
    }
}
二、升级数据库

public class MyDatabaseHelper extends SQLiteOpenHelper {

    public static final String CREATE_BOOK = "create table Book(" +
            "id integer primary key autoincrement," +
            "author text," +
            "price real," +
            "pages integer," +
            "name text," +
            "category_id integer)";
    public static final String CREATE_CATEGORY = "create table Category(" +
            "id integer primary key autoincrement," +
            "category_name text," +
            "category_code integer)";
    
    private Context mContext;

    public MyDatabaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version){
        super(context,name,factory,version);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(CREATE_BOOK);
        db.execSQL(CREATE_CATEGORY);
        //Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
        switch (oldVersion){
            case 1:
                db.execSQL(CREATE_CATEGORY);
            case 2:
                db.execSQL("alert table Book add column category_id integer");
            default:

        }
    }
}
三、添加数据

SQLiteDatabase提供来一个insert()方法用于添加数据,需要三个参数,第一个参数:表名,第二个参数:自动赋值,一般传入null,第三个参数:ContentValues对象,保存着添加的数据,用put方法来添加数据

SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("name","The Da Vinci Code");
                values.put("author","Dan Brown");
                values.put("pages",454);
                values.put("price",16.96);
                db.insert("Book", null, values);
                values.clear();
                values.put("name","The Lost Symbol");
                values.put("author","Dan Brown");
                values.put("pages",510);
                values.put("price",19.95);
                db.insert("Book", null, values);
四、更新数据

SQLiteDatabase提供来一个update()方法用于对数据进行更新,需要四个参数,第一个参数:表名,第二个参数:ContentValue对象,保存着要更新的数据,第三第四个数据是对表中的记录进行筛选过,第三个参数,相对应的是查询语句的where部分,第四个参数为第三个参数中的占位符提供数据

SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("price",10.99);
                db.update("Book", values, "name = ?", new String[]{"The Da Vinci Code"});
五、删除数据

SQLiteDatabase中提供了delete方法用于删除数据,需要三个参数,第一个参数:表名,第二第三个参数作用于update()方法中第三第四个参数的作用一致。如果不指定的话表示删除所有行。

SQLiteDatabase db = dbHelper.getWritableDatabase();
                db.delete("Book", "pages > ?", new String[]{"500"});
六、查询数据

SQLiteDatabase中提供来query()方法用于查询数据。


SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor cursor = db.query("Book",null,null,null,null,null,null);
                if(cursor.moveToFirst()){
                    do{
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String author = cursor.getString(cursor.getColumnIndex("author"));
                        int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                        double price = cursor.getDouble(cursor.getColumnIndex("price"));
                        Log.d("SQLiteActivity","book name is " + name);
                        Log.d("SQLiteActivity","book author is " + author);
                        Log.d("SQLiteActivity","book pages is " + pages);
                        Log.d("SQLiteActivity","book price is " + price);
                    }while(cursor.moveToNext());
                }
                cursor.close();

以上操作也可以直接使用sql语句完成。

七、事务

为了保证数据库的同步,需要使用事务来提交地数据库的操作。

SQLiteDatabase db = dbHelper.getWritableDatabase();
                db.beginTransaction();
                try{
                    做一些事情
                    db.setTransactionSuccessful();
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    db.endTransaction();
                }





  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值