天天看點

Android資料庫的使用(增删改查)

寫一個Demo,大概如下,5個button點選事件(最後有源碼)

Android資料庫的使用(增删改查)

第一步,建立一個據庫輔助類:隻做三件事 (創庫,創表,更新/更新)

先來個沒寫操作的

Android資料庫的使用(增删改查)

然後如下操作

Android資料庫的使用(增删改查)

第二步:在Activity檔案中初始化輔助類

Android資料庫的使用(增删改查)

第三步:建立資料庫(第一個button點選事件)

Android資料庫的使用(增删改查)

第四步(增删改查,另外四個點選事件);

插入資料–增

Android資料庫的使用(增删改查)

删除資料–删

Android資料庫的使用(增删改查)

更改資料–改

Android資料庫的使用(增删改查)

查詢資料–查

Android資料庫的使用(增删改查)

下面是源碼

DataBaseHelp如下:

//建立資料庫輔助類:創庫,創表,更新/更新
public class DataBaseHelp extends SQLiteOpenHelper {
    //資料庫名稱
    public static final String DB_NAME = "my_database.db";
    //資料庫版本
    private static final int DB_VERSION = 1;
    //表名
    public static final String TABLE_NAME = "table_demo";

    /*context:上下文
     *name:要被建立的資料庫名稱(檔案名)
     *factory:遊标工廠
     * version:資料庫版本
     * */
    public DataBaseHelp(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    //建立表
    @Override
    public void onCreate(SQLiteDatabase db) {
        //用SQL語句來建立表
        String sql = "create table if not exists " + TABLE_NAME + "(_id integer primary key autoincrement,name varchar(20),age integer)";
        //執行語句
        db.execSQL(sql);
        Log.e("sql", "onCreate: " + "表建立成功");
    }

    //用于更新資料庫
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //當目前版本和老版本不一樣的時候
        if (newVersion != oldVersion) {

        }
    }
}

           

DataBaseActivity如下:

public class DataBaseActivity extends AppCompatActivity {

    private DataBaseHelp helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_base);
        //初始化輔助類
        helper = new DataBaseHelp(this);
    }

    //建立資料庫
    public void btncreate(View view) {
        //getWritableDatabase()打開資料庫
        SQLiteDatabase db = helper.getWritableDatabase();
        //當資料庫不用時關閉資料庫(為了防止部落客Demo出錯。。。。。)
        db.close();

    }

    //添加資料
    public void btninsert(View view) {
        SQLiteDatabase db = helper.getWritableDatabase();
        //Android為了友善程式員SQL開發,提供了一個賊好的API: ContentValues(添加資料交給它)
        ContentValues values = new ContentValues();
        //放值
        values.put("name", "張三");
        values.put("age", 18);
        db.insert(DataBaseHelp.TABLE_NAME, null, values);
        values.put("name", "李四");
        values.put("age", 19);
        db.insert(DataBaseHelp.TABLE_NAME, null, values);
        values.put("name", "王五");
        values.put("age", 20);
        db.insert(DataBaseHelp.TABLE_NAME, null, values);
        //關閉資料庫
        db.close();

    }

    //删除資料
    public void btndelete(View view) {
        SQLiteDatabase db = helper.getWritableDatabase();
        /*table :表名
         * whereClause:滿足删除條件 格式:例子: "name=?"
         * whereArgs:滿足删除的條件*/
        db.delete(DataBaseHelp.TABLE_NAME, "name=?", new String[]{"張三"});
        //關閉資料庫
        db.close();
    }

    //修改資料
    public void btnupdate(View view) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", "BoRe");
        values.put("age", 7);
        /*
         * table:表名
         * values:更新的資料
         * whereClaues:更新條件 格式:例子: "name=?"
         * whereArgs:滿足更新的條件*/
        db.update(DataBaseHelp.TABLE_NAME, values, "name=?", new String[]{"李四"});
        //關閉資料庫
        db.close();
    }

    //查找資料
    public void btnselect(View view) {
        SQLiteDatabase db = helper.getWritableDatabase();
        /*
         *table :表名
         * colums:被查詢的列(字段)
         * selection :查詢條件
         * selectionArgs:滿足查詢條件
         * group:指定分組
         * having:分組篩選資料的關鍵字
         * orderby:排序*/
        Cursor cursor = db.query(DataBaseHelp.TABLE_NAME, new String[]{"name", "age"}, null, null, null, null, null);
        //資料拿到了,已經在cursor遊标中,現在要周遊遊标
        //先拿到我們關心的列的索引
        int nameIndex = cursor.getColumnIndex("name");
        int ageIndex = cursor.getColumnIndex("age");
        while (cursor.moveToNext()) {
            //通過列的索引去擷取單個格子的值
            String name = cursor.getString(nameIndex);
            int age = cursor.getInt(ageIndex);
            Log.e("Select", "btnselect: " + name + age);
        }
        //關閉資料庫
        db.close();

    }
}

           

xml檔案如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".DataBaseActivity">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="btncreate"
    android:text="建立"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btninsert"
        android:text="插入資料"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btndelete"
        android:text="删除資料"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btnupdate"
        android:text="更改資料"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btnselect"
        android:text="查詢資料"/>
</LinearLayout>
           

繼續閱讀