1. 程式人生 > >android 簡單書庫操作 SQLite使用

android 簡單書庫操作 SQLite使用

Sqlite資料庫是一種輕量級資料庫,它具備跨平臺,多語言操作等優點,它廣泛用於包括瀏覽器、IOS,Android以及一些便攜需求的小型web應用系統。它具備佔用資源低,處理速度快等優點。

1.首先定義一個SQLiteOpenHelper幫助類

SQLiteOpenHelper是SQLiteDatabase的一個幫助類,主要用於操作資料庫和資料庫升級,一般需要自定義個DBHelper類來繼承SQLiteOpenHelper

定義了兩條sql語句,用於建立兩個表格

package com.nxm.sqlitedemo;

import android.content.Context;
import
android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * @Auther: muzi102 * @Date: 2018/5/2 15:12 27 * @Describe: the infor of the class */ public class MyDatabaseHelper extends SQLiteOpenHelper { private Context context; public static final String CREATE_BOOK = "create table Book ("
+ "id integer primary key autoincrement, " + "author text, " + "price real, " + "pages integer, " + "name text)"; public static final String CREATE_CATEGORY = "create table Category (" + "id integer primary key autoincrement, "
+ "category_name text, " + "category_code integer)"; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); this.context = context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_BOOK); db.execSQL(CREATE_CATEGORY); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exists Book"); db.execSQL("drop table if exists Category"); onCreate(db); } }

2.然後在使用到的地方new一個

    private MyDatabaseHelper dbHelper = new MyDatabaseHelper(this, "BookStore.db", null,1);

3.呼叫 dbHelper.getWritableDatabase();方法出發onCreate方法建立資料庫(資料庫不存在則建立)

dbHelper.getWritableDatabase();

4.對資料庫插入資料操作

 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); // 插入第二條資料

5.修改資料庫資料

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

6.查詢資料庫內容

SQLiteDatabase db = dbHelper.getWritableDatabase();
                // 查詢Book表中所有的資料
                Cursor cursor = db.query("Book", null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    do {
                        // 遍歷Cursor物件,取出資料並列印
                        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);
                    } while (cursor.moveToNext());
                }
                cursor.close();