SQlite-資料庫的訪問執行個體

來源:互聯網
上載者:User

標籤:

1、DBAdapter類:  1 package com.cnzcom.android.quickdial; import android.content.ContentValues; import android.content.Context;import android.database.Cursor;import android.database.SQLException;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;/** *  * @author zhangjie * * 資料庫相關操作的類 */public class DBAdapter {    /**     * 資料庫名     */    private static final String DATABASE_NAME = "quickdial.db";        /**     * 資料表名     */    private static final String DATABASE_TABLE = "quickdial";        /**     * 資料庫版本     */    private static final int DATABASE_VERSION = 1;        /**     * key_id :主鍵     */    @SuppressWarnings("unused")    private static final String KEY_ID = "key_id";        /**     * position :位置資訊,表示資料是第幾項     */    private static final String POSITION = "position";        /**     * name :姓名     */    private static final String NAME = "name";        /**     * phone_number :電話號碼     */    private static final String PHONE_NUMBER = "phone_number";        /**     * ip :是否ip播出     */    private static final String IP = "ip";        /**     *      */    private static final String DATABASE_CREATE =        "create table quickdial (key_id INTEGER PRIMARY KEY, "            + "position INTEGER, "             + "name TEXT, "             + "phone_number TEXT, "            + "ip INTEGER"            + ");";        /**     *      */    private final Context context;        /**     *      */    private DatabaseHelper DBHelper;        /**     *      */    private SQLiteDatabase db;        public DBAdapter(Context ctx) {        context = ctx;        DBHelper = new DatabaseHelper(context);                    }        private static class DatabaseHelper extends SQLiteOpenHelper {        public DatabaseHelper(Context context) {            super(context, DATABASE_NAME, null, DATABASE_VERSION);        }        @Override        public void onCreate(SQLiteDatabase db) {            // TODO Auto-generated method stub            db.execSQL(DATABASE_CREATE);        }        @Override        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {            // TODO Auto-generated method stub            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);            onCreate(db);        }            }        /**     * 開啟資料庫     * @return     * @throws SQLException     */    public SQLiteDatabase open() throws SQLException    {        db = DBHelper.getWritableDatabase();        Cursor cursor = getAll();                if(cursor.getCount() == 0) {                        Log.e("資料庫為空白", "插入資料");            /**             * 先使用假資料初始化資料庫             */            for(int i = 0; i < 10; i++) {                insert(i, "", "", 0);            }          } else {            Log.e("資料庫不為空白", "讀取資料");        }        cursor.close();                return db;    }        /**     * 關閉資料庫     */    public void close()    {        DBHelper.close();    }        /**     * 向資料庫中插入資料     */    public long insert(int position, String name, String phone_number, int ip) {                ContentValues initialValues = new ContentValues();        initialValues.put(POSITION, position);        initialValues.put(NAME, name);        initialValues.put(PHONE_NUMBER, phone_number);        initialValues.put(IP, ip);                return db.insert(DATABASE_TABLE, null, initialValues);    }        /**     * 刪除資料,其實不是真正意義上的刪除,而是將name = ""、phone_number = ""、ip = 0     */    public boolean delete(int position) {                ContentValues initialValues = new ContentValues();        initialValues.put(POSITION, position);        initialValues.put(NAME, "");        initialValues.put(PHONE_NUMBER, "");        initialValues.put(IP, 0);                return db.update(DATABASE_TABLE, initialValues, POSITION + "=" + position, null) > 0;            //        return db.delete(DATABASE_TABLE, POSITION + "=" + position, null) > 0;    }        /**     * 更改資料     */    public boolean update(int position, String name, String phone_number, int ip) {                ContentValues initialValues = new ContentValues();        //initialValues.put(POSITION, position);        initialValues.put(NAME, name);        initialValues.put(PHONE_NUMBER, phone_number);        initialValues.put(IP, ip);                return db.update(DATABASE_TABLE, initialValues, POSITION + "=" + position, null) > 0;            }        public Cursor getAll() {                Cursor cur = db.query(DATABASE_TABLE,                 null, null, null, null, null, null);        return cur;                }        public Cursor get(long rowId) throws SQLException {        Cursor cur = db.query(true, DATABASE_TABLE, new String[] {POSITION,                                                                        NAME,                                                                        PHONE_NUMBER,                                                                        IP},                                                                                                         POSITION + "=" + rowId, null, null, null, null, null);                if(cur != null) {            cur.moveToFirst();        }                return cur;    }    }2、Activity中調用:1 public DBAdapter m_DBAdapter;m_DBAdapter = new DBAdapter(this);m_DBAdapter.open();

文章出處

http://www.cnblogs.com/Jackeyzhang/archive/2011/06/02/2068459.html

SQlite-資料庫的訪問執行個體(轉)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.