Android 台灣中文網

標題: 初學關於資料庫SQLite的問題 [打印本頁]

作者: tt60815    時間: 2012-4-8 17:04
標題: 初學關於資料庫SQLite的問題
public class WInfoModel {
        private static SQLiteDatabase db = null;

        public static void setDb(SQLiteDatabase db) {
                WInfoModel.db = db;
        }
        public static boolean save(WinFoData wid){
                // 新增一- SQL語法
                                // 建立INSERT SQL語法
                                try {
                                        String sql = String.format(
                                                        "INSERT INTO winfo values('%s',%f,%f,%f,%d)",
                                                        wid.getIN001(), wid.getIN002(), wid.getIN003(),
                                                        wid.getIN004(), wid.isIN005() ? 1 : 0);
                                        db.execSQL(sql);
                                        // 新增二- ContentValue物件
                                        ContentValues cvs = new ContentValues();
                                        cvs.put("IN001", wid.getIN001());
                                        cvs.put("IN002", wid.getIN002());
                                        cvs.put("IN003", wid.getIN003());
                                        cvs.put("IN004", wid.getIN004());
                                        cvs.put("IN005", wid.isIN005());
                                        db.insertOrThrow("winfo", null, cvs);
                                } catch (SQLException ex) {
                                        return false;
                                }
                                return true;
                        }
}






======================================================================

public class DBConnection extends SQLiteOpenHelper {
        private final static String  DB_NAME = "myDB";
        private final static int    DB_VERSION = 1;

        public DBConnection(Context context) {
                super(context, DB_NAME, null, DB_VERSION);
                // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
               
                // TODO Auto-generated method stub
                String sql = "create table winfo(" +
                                "IN001 DATE PRIMARY KEY,"+ //測量日期
               "IN002 DOUBLE,"+                                //身高
               "IN003 DOUBLE,"+                                //體重
               "IN004 DOUBLE,"+                                //腰圍
               "IN005 BOOLEAN)";                        //性別
        db.execSQL(sql);

        }

        @Override
        public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
                // TODO Auto-generated method stub

        }

}
======================================================================
我就是看不懂這兩個類別的敘述 想請教大家

作者: ploglin    時間: 2012-4-9 09:32
第2個類別是繼承SDK中的SQLite去實作onCreate以及onUpgrade
onCreate 是當 sqlite 第1個次產生時所執行的動作
onUpgrate 是當傳入的 version 有變動時,所執行的動作

第1個類別則是自訂義的一個類別,在 init 的時候,需要將 db object 傳入當參數。
類似寫法為

  1. DBConnection db = new DBConnection(); // 這邊是初始代 SQLiteOpenHelper
  2. WInfoModel.setDb(db); // 定義WinfoModel所使用的DB
  3. WInfoModel.save(.....);
複製代碼
這樣說不知道看的懂嗎?
作者: tt60815    時間: 2012-4-9 11:06
public DBConnection(Context context) {
                super(context, DB_NAME, null, DB_VERSION);
                // TODO Auto-generated constructor stub
        }

這一段為什麼上面 DBConnection(Context context)  括號裡面為什麼可以化簡成這樣@@?

作者: ploglin    時間: 2012-4-12 09:32
DBConnection 是這個 Class 的建構式。
基本上可以什麼都不要像是

  1. publif DBConnection(){
  2.     super(context, DB_NAME, null, DB_VERSION);
  3. }
複製代碼
,或是可以根本就不要這個建構式。
但是如果你有 class 裡面有需要用到的參數,像是 context 或其它變數,就可以在傳入的變數中加入。
作者: tt60815    時間: 2012-4-12 12:55
好的 非常謝謝你:")
作者: ztazta8888    時間: 2012-5-21 23:55
Thank you for your share ~~~~~~~~~~~~~~~~




歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1