Android SQLite操作類--封裝

來源:互聯網
上載者:User

在Android操作資料庫一般都是先繼承SQLiteOpenHelper類,下面直接貼上代碼:

public class DBHelper extends SQLiteOpenHelper {
    /**
     * @Fields name : 資料庫名稱
     */
    private final static String name = "info.db";
    /**
     * @Fields version :版本號碼
     */
    private final static int version = 1;
    private Class<?>[] clazzs = new Class<?>[] { DemoBean.class };//關聯資料庫的實體類
    public DBHelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
    }
    public DBHelper(Context context) {
        super(context, name, null, version);
    }

    /*
     * 用來建立資料庫中的表 <p>Title: onCreate</p> <p>Description: </p>
     * @param db
     * @see
     * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
     * .SQLiteDatabase)
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        for (int i = 0; i < clazzs.length; i++) {
            DBUtils.createTable(db, clazzs[i]);
        }
    }

    /*
     * 當資料庫的版本變更之後就會調用這個方法 <p>Title: onUpgrade</p> <p>Description: </p>
     *
     * @param db
     *
     * @param oldVersion
     *
     * @param newVersion
     *
     * @see
     * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
     * .SQLiteDatabase, int, int)
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}

此類中需要注意的是 DBUtils.createTable(db, clazzs[i]);這個靜態方法,該方法主要用來自動產生SQL語句(因為本人最不喜歡寫SQL語句了,使用的是Java反射技術)。這裡要用到 Java Annotation,不知道的可以百度一下)。

 

另外呢,資料庫的操作無非就是增刪改查—>為了不對每個表都寫個增刪改查的方法,這裡全部使用泛型方法,但是要注意的是:JavaBeen中的類名和欄位名都要和資料庫中表的欄位結構一樣。還有一點要注意的是:沒有盡量的使用介面,我認為沒有必要。

由於代碼過長:我只貼插入方法了:

 

public <T> void insertData(T t) throws NumberFormatException,
            IllegalArgumentException, IllegalAccessException {
        SQLiteDatabase db = null;
        synchronized (DriverManager.class) {
            db = helper.getWritableDatabase();
        }
        Field[] fields = t.getClass().getDeclaredFields();// 使用暴力
        ContentValues values = new ContentValues();
        for (Field field : fields) {
            // 允許訪問私人變數
            field.setAccessible(true);
            if ("double".equals(field.getType().toString())) {
                values.put(field.getName(),
                        Double.valueOf(String.valueOf(field.get(field))));
            }
            if ("int".equals(field.getType().toString())) {
                values.put(field.getName(),
                        Integer.valueOf(String.valueOf(field.get(field))));
            }
            if ("float".equals(field.getType().toString())) {
                values.put(field.getName(),
                        Float.valueOf(String.valueOf(field.get(field))));
            }
            if ("class java.lang.String".equals(field.getType().toString())) {
                values.put(field.getName(), String.valueOf(field.get(field)));
            }
        }
        db.insert(t.getClass().getSimpleName(), null, values);
        db.close();
    }

好了暫時就寫這麼多了,等我下次組織語言在寫 ,第一次寫嘛!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.