首先呢,SQLite 並不是像一般大型資料庫一樣要先建立好資料庫的實體
ex : 建立資料庫、建立資料表、計設資料表欄位之類的~
然後再經由程式進行存取,SQLite 是經由 Android 幫你建立的 (十分不習慣)
所以要繼承 SQLiteOpenHelper 然後來建立一個資料表
以下是步驟
首先建立一個 class 叫做 DBHelper 然後繼承 SQLiteOpenHelper
package com.android.myapp; public class DBHelper extends SQLiteOpenHelper { }
然後滑鼠移到繼承的 SQLiteOpenHelper 時會出現
Import SQLiteOpenHelper
按下去就會自動產生 code
然後滑鼠在移到劃紅線的 DBHelper 這個 Class 名稱
會出現 Add constructor DBHelper(Context,.....................)
也是點下去就會自己產生 code
然後幾乎什麼 code 都還沒寫,程式碼就會長這樣
package com.android.myapp; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
其中覆寫的 onCreate 指的是如果 Android 載入時找不到生成的資料庫檔案,就會觸發 onCreate
onUpgrade 則是如果資料庫結構有改變了就會觸發 onUpgrade
所以很明顯的我們要把產生資料表的 SQL 寫在這 onCreate
然後首先我們要先定義資料庫名稱與版本,所以請看 line 8 ~ 10
package com.android.myapp; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { private final static int _DBVersion = 1; //<-- 版本 private final static String _DBName = "SampleList.db"; //<-- db name private final static String _TableName = "MySample"; //<-- table name public DBHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
然後程式中有一段 super(context, name, factory, version);
super 關鍵字指的是目前這個 class 的上一層,也就是 SQLiteOpenHelper
我們把剛剛定義的變數傳給 name 與 version
於是就變成
super(context, _DBName, factory, _DBVersion);
再來我們就分別在 onCreate 與 onUpgrade 放入SQL的命令
所以最後就會如下
package com.android.myapp; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { private final static int _DBVersion = 1; private final static String _DBName = "SampleList.db"; private final static String _TableName = "MySample"; public DBHelper(Context context) { super(context, _DBName, null, _DBVersion); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub final String SQL = "CREATE TABLE IF NOT EXISTS " + _TableName + "( " + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "_TITLE VARCHAR(50), " + "_CONTENT TEXT," + "_KIND VARCHAR(10)" + ");"; db.execSQL(SQL); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub final String SQL = "DROP TABLE " + _TableName; db.execSQL(SQL); } }
這邊要注意的是好像一定要把 primary key 的名字設定為 _id,不然好像會 error
到此就寫完了
然後再來是應用
package com.android.myapp; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.TextView; import android.widget.Toast; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.view.View; import android.widget.*; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class DataList extends Activity implements View.OnClickListener { private Button sample1; private Button viewcode1; private DBHelper DH = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.datalist); findControl(); setClick(); openDB(); } private void add(String Title,String Content,String Kind){ SQLiteDatabase db = DH.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("_TITLE", Title.toString()); values.put("_CONTENT", Content.toString()); values.put("_KIND", Kind.toString()); db.insert("MySample", null, values); } private void openDB(){ DH = new DBHelper(this); } private void closeDB(){ DH.close(); } private void findControl() { sample1 = (Button)findViewById(R.id.sample1); viewcode1 = (Button)findViewById(R.id.viewcode1); } private void setClick() { sample1.setOnClickListener(this); viewcode1.setOnClickListener(this); } @Override protected void onDestroy() { super.onDestroy(); closeDB(); } @Override public void onClick(View v) { //Toast popup = Toast.makeText(this, "hello", Toast.LENGTH_SHORT); //popup.show(); if (v.getId() == R.id.sample1) { add("A","test","1"); openDialog("test","1"); } else if (v.getId() == R.id.viewcode1) { add("B","test","2"); openDialog("","2"); } } private void openDialog(String Msg,String Mode) { if (Mode =="1") { TextView AlertMsg = new TextView(this); AlertMsg.setText(Msg); new AlertDialog.Builder(this).setTitle("效果").setView(AlertMsg).show(); } else if (Mode == "2") { String url = "file:///android_asset/Sample.html"; WebView AlertMsg = new WebView(this); WebSettings websettings = AlertMsg.getSettings(); websettings.setSupportZoom(true); websettings.setJavaScriptEnabled(true); AlertMsg.setWebViewClient(new WebViewClient()); AlertMsg.loadUrl("file:///android_asset/Sample.html"); new AlertDialog.Builder(this).setTitle("程式碼") .setView(AlertMsg).show(); } } }
這樣就可以寫入資料表了
下圖是用手機裝的 APP "SQLite Editor" 檢視 SQLite 的結果
結束
留言列表