一想到Android到數據庫,只需要想到一個類 SQLiteOpenHelper,然后寫一個類繼承 SQLiteOpenHelper,重寫構造方法,對數據庫進行配置
public class MySQLiteOpenHelper extends SQLiteOpenHelper { public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } }
SqliteDatabase數據庫,所寫的所有SQL語句都是通過系統sqlite.c文件執行成數據庫文件的,SQlite數據庫是關系型數據庫,輕量級,體積小等特點
實現MySQLiteOpenHelper數據庫幫助類:
package liudeli.datastorage.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MySQLiteOpenHelper extends SQLiteOpenHelper { public static MySQLiteOpenHelper mySQLiteOpenHelper; private static final String DB_NAME = "person_info.db"; private static final int VERSION = 1; public synchronized static MySQLiteOpenHelper getInstance(Context context) { if (null == mySQLiteOpenHelper) { mySQLiteOpenHelper = new MySQLiteOpenHelper(context, DB_NAME, null, VERSION); } return mySQLiteOpenHelper; } /** * 當開發者調用 getReadableDatabase(); 或者 getWritableDatabase(); * 就會通過此構造方法配置的信息 來創建 person_info.db 數據庫 * 此方法的另外作用是,如果存着數據庫就打開數據庫,不存着數據庫就創建數據庫 * @param context 上下文 * @param name 數據庫名 * @param factory 游標工廠 * @param version 版本,最低為1 */ private MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } /** * 此方法是何時調用? ,是需要開發者調用 getReadableDatabase(); 或者 getWritableDatabase(); * 此方法的作用是,如果沒有表就創建打開,如果有表就打開 * @param db 可執行SQL語句 */ @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table student_table(_id integer primary key autoincrement, name text, age integer);"); } /** * 此方法用於數據庫升級 * @param db 可執行SQL語句 * @param oldVersion 以前舊版本的版本號 * @param newVersion 現在目前最新的版本號 */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
寫SQL語句進行增刪改查操作:
package liudeli.datastorage; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import liudeli.datastorage.db.MySQLiteOpenHelper; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private final String TAG = MainActivity.class.getSimpleName(); private MySQLiteOpenHelper mySQLiteOpenHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mySQLiteOpenHelper = MySQLiteOpenHelper.getInstance(this); // person_info.db創建成功 table表創建成功 還有一個很重要的作用:刷新數據庫 刷新表數據等 mySQLiteOpenHelper.getReadableDatabase(); mySQLiteOpenHelper.getWritableDatabase(); initViewListener(); } private void initViewListener() { Button btQuery = findViewById(R.id.bt_query); Button btInsert = findViewById(R.id.bt_insert); Button btUpdate = findViewById(R.id.bt_update); Button btDelete = findViewById(R.id.bt_delete); btQuery.setOnClickListener(this); btInsert.setOnClickListener(this); btUpdate.setOnClickListener(this); btDelete.setOnClickListener(this); } @Override protected void onStart() { super.onStart(); } /** * 雖然之需要一次 getWritableDatabase getReadableDatabase 就可以來,為什么還需要每次點擊都執行一次? * 答:getWritableDatabase getReadableDatabase 還有一個很重要的作用:刷新數據庫 刷新表數據等 * @param v */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_query: { SQLiteDatabase db = mySQLiteOpenHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from "+MySQLiteOpenHelper.TABLE_NAME+";", null); while (cursor.moveToNext()) { int _id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); Log.d(TAG, "_id:" + _id + " name:" + name + " age:" + age); } db.close(); break; } case R.id.bt_insert: { SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase(); db.execSQL("insert into " + MySQLiteOpenHelper.TABLE_NAME + "(name,age) values('劉德利',19);"); db.close(); break; } case R.id.bt_update: { SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase(); db.execSQL("update student_table set name='德利' where _id = 1;"); db.close(); break; } case R.id.bt_delete: { SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase(); db.execSQL("delete from student_table where _id = 1;"); db.close(); break; } default: break; } } }
布局代碼:
<?xml version="1.0" encoding="utf-8"?> <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=".MainActivity"> <Button android:id="@+id/bt_query" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查詢" /> <Button android:id="@+id/bt_insert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="插入" /> <Button android:id="@+id/bt_update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改" /> <Button android:id="@+id/bt_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="刪除" /> </LinearLayout>