Android数据库SQLite操作详解及LitePal用法详解(二)

本篇开始学习使用数据库的增删改查,作为数据库最基本以及最核心的四项功能。

(一)增加数据

同样的添加一个按钮:

    <Button
        android:id="@+id/btn_add_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加数据"/>

在SQLiteDatabase中直接调用insert方法即可,该方法有三个参数
String table, String nullColumnHack, ContentValues values
其中values就是需要传入的值,这里给我们的Book表添加两本书

SQLiteDatabase db = mDbOpenHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                //组装第一条数据
                values.put("name","金瓶梅");
                values.put("author", "东方不败");
                values.put("pages", 300);
                values.put("price", 250);
                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);

(二)删除数据

添加按钮

    <Button
        android:id="@+id/btn_delete_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除数据"/>

删除数据需要调用SQLiteDatabase中的delete方法,该方法的三个参数为String table表名, String whereClause限定条件, String[] whereArgs占位符的值,这里我们删除页数大于500的书

SQLiteDatabase db = mDbOpenHelper.getWritableDatabase();
                db.delete("Book", "pages > ?", new String[] { "500" });

(三)修改数据

创建按钮:

   <Button
        android:id="@+id/btn_update_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="修改数据"/>

使用SQLiteDatabase中的updata方法,该方法有四个参数
String table, ContentValues values, String whereClause, String[] whereArgs
我们尝试来把金瓶梅的价格改到500一本,知识就是金钱。

SQLiteDatabase db = mDbOpenHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("price", 500);
                db.update("Book",values,"name=?",new String[]{"金瓶梅"});

(四)查询数据

废话不多说,先加一个按钮

    <Button
        android:id="@+id/btn_query_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询数据"/>

我们需要调用SQLiteDatabase中的query方法,我的妈妈咪呀,这个参数太多了,String table, String[] columns, String selection,String[] selectionArgs, String groupBy, String having,String orderBy,他们都是什么含义呢?
这里写图片描述

来个简单的,直接查询Book表中所有的数据,query方法会返回Cursor对象,我们需要对Cursor对象进行操作,最后记得关闭Cursor哦~

 SQLiteDatabase db = mDbOpenHelper.getWritableDatabase();
                //查询Book表中所有的数据
                Cursor cursor = db.query("Book", null, null, null, null, null, null);
                while (cursor.moveToNext()){
                    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("MainActivity", "book name is " + name);
                    Log.d("MainActivity", "book author is " + author);
                    Log.d("MainActivity", "book pages is " + pages);
                    Log.d("MainActivity", "book price is " + price);
                }
                cursor.close();

Android数据库SQLite操作详解及LitePal用法详解(三)
将学习使用LitePal

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值