1. 程式人生 > >學習筆記:Android SQLite,並實現SQLite基本CRUD操作的Demo

學習筆記:Android SQLite,並實現SQLite基本CRUD操作的Demo

package com.steven.mydatabasetest;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnCreateDatabase;
    private Button btnInsertData;
    private Button btnUpdateData;
    private Button btnDeleteData;
    private Button btnQueryData;
    private MySQLiteOpenHelper dbHelper;
    private static final String TAG = "Steven";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //呼叫建構函式,指明要建立的資料庫名字以及版本號
        dbHelper = new MySQLiteOpenHelper(MainActivity.this,"BookStore.db", null, 5);

        //1.建立資料庫
        btnCreateDatabase = (Button)findViewById(R.id.btn_create_database);
        btnCreateDatabase.setOnClickListener(this);
        //2.插入資料
        btnInsertData = (Button)findViewById(R.id.btn_insert_data);
        btnInsertData.setOnClickListener(this);
        //3.修改資料
        btnUpdateData = (Button)findViewById(R.id.btn_update_data);
        btnUpdateData.setOnClickListener(this);
        //4.刪除資料
        btnDeleteData = (Button)findViewById(R.id.btn_delete_data);
        btnDeleteData.setOnClickListener(this);
        //5.查詢資料
        btnQueryData = (Button)findViewById(R.id.btn_query_data);
        btnQueryData.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

            // 1.建立並開啟資料庫BookStore.db,回撥MySQLiteOpenHelper類中的onCreate()方法建立表
            case R.id.btn_create_database:
                dbHelper.getWritableDatabase();
                break;

            // 2.向表中插入資料: insert
            case R.id.btn_insert_data:
                SQLiteDatabase db = dbHelper.getWritableDatabase(); //獲取返回的SQLiteDatabase物件

                ContentValues contentValues = new ContentValues();

                //組裝資料
                contentValues.put("name","第一行程式碼");
                contentValues.put("author","郭霖");
                contentValues.put("pages", 552);
                contentValues.put("price",79.0);
                db.insert("Book", null, contentValues); //向表Book中插入一條資料
                contentValues.clear();

                //組裝資料
                contentValues.put("name","第二行程式碼");
                contentValues.put("author","郭霖");
                contentValues.put("pages", 662);
                contentValues.put("price",89.0);
                db.insert("Book", null, contentValues); //向表Book中插入第二條資料
                Toast.makeText(this, "成功插入資料", Toast.LENGTH_SHORT).show();
                break;

            // 3.修改資料: update
            case R.id.btn_update_data:
                SQLiteDatabase db1 = dbHelper.getWritableDatabase();
                ContentValues contentValues1 = new ContentValues();
                contentValues1.put("name", "第三行程式碼");
                db1.update("Book", contentValues1,"name = ?",new String[]{"第二行程式碼"});
                Toast.makeText(this, "成功修改資料", Toast.LENGTH_SHORT).show();
                break;

            // 4.刪除資料:delete
            case R.id.btn_delete_data:
                SQLiteDatabase db2 = dbHelper.getWritableDatabase();
                db2.delete("Book", "pages > ?", new String[]{"600"});
                Toast.makeText(this, "成功刪除一條資料", Toast.LENGTH_SHORT).show();
                break;

            // 5.查詢資料: query
            case R.id.btn_query_data:
                SQLiteDatabase db3 = dbHelper.getWritableDatabase();
                Cursor cursor = db3.query("Book", null, null, null, null, null, null);
                //查詢表中所有資料
                if(cursor.moveToFirst()){
                    //遍歷Cursor物件,取出資料
                    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(TAG,"Book name: " + name);
                        Log.d(TAG,"Book author: " + author);
                        Log.d(TAG,"Book pages: " + pages);
                        Log.d(TAG,"Book price: " + price);
                    }while(cursor.moveToNext());
                    cursor.close();
                }
                Toast.makeText(this, "查詢出表中全部資料", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}