SQLite資料庫淺談

來源:互聯網
上載者:User

標籤:

1.編寫類繼承SQLitHelper;

public class SQLiteHelper extends SQLiteOpenHelper {}

 

2.實現其建構函式;

    public SQLiteHelper(Context context){        this(context, TABLE_NAME, null, DB_VERSION);    }        public SQLiteHelper(Context context, String name, CursorFactory factory,            int version) {        super(context, TABLE_NAME, factory, DB_VERSION);        }

3.定義資料庫的名稱、表名、版本以及表的所有列明

    private static final String DB_NAME = "playerdatas";//資料庫名稱    private static final String TABLE_NAME = "datas";//表名    private static final int DB_VERSION = 1;//資料庫版本    //表的列名    private static final String TABLE_ID = "_ID";     private static final String TABLE_VIDEOID = "videoId";    private static final String TABLE_URL = "url";    private static final String TABLE_TITLE = "title";    private static final String TABLE_CONTENT = "content";    private static final String TABLE_ACTORS = "actors";    private static final String TABLE_DIRECTOR = "director";    private static final String TABLE_TYPE = "type";    private static final String TABLE_ICON = "icon";

4.實現onCreate方法,編寫SQL語句,建立表

    @Override    public void onCreate(SQLiteDatabase db) {        this.db = db;        StringBuilder builder = new StringBuilder();        builder.append("create table if not exists ");        builder.append(TABLE_NAME);        builder.append("(");        builder.append(TABLE_ID).append(" Integer primary key autoincrement, ");        builder.append(TABLE_VIDEOID).append(" Integer, ");        builder.append(TABLE_ICON).append(" Integer, ");        builder.append(TABLE_URL).append(" varchar(50), ");        builder.append(TABLE_TITLE).append(" varchar(30), ");        builder.append(TABLE_CONTENT).append(" varchar(50) ");        builder.append(" ) ");                db.execSQL(builder.toString());    }

5 實現onUpgrade方法,當版本升級時調用

    /**     * 版本更新     */    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        db.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME );        onCreate(db);    }

6.編寫查詢、刪除、更新等操作方法。

/**     * 查詢所有資料     * @return     */    public Cursor query(){        SQLiteDatabase db = getReadableDatabase();        //擷取Corsor        Cursor c = db.query(TABLE_NAME, null, null, null, null, null, null);        return c;            }    /**     * 根據id尋找資料     * @param id     * @return     */    public Cursor queryById(int videoId){        SQLiteDatabase db = getReadableDatabase();        Cursor c = db.query(TABLE_NAME, new String[]{TABLE_VIDEOID}, "videoId=?",                 new String[]{String.valueOf(videoId)}, null, null, null);        return c;    }        /**     * 根據唯一標識_ID  來刪除資料       * @param id     */    public void delete(int id){        SQLiteDatabase db = getWritableDatabase();        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>"+db);        db.delete(TABLE_NAME, "_ID=?", new String[]{String.valueOf(id)});    }    /**     * 插入方法     * @param values插入值     */    public void insert(ContentValues values){        SQLiteDatabase db = getWritableDatabase();        db.insert(TABLE_NAME, null, values);        db.close();    }

7.關閉資料庫的方法

    /**     * 關閉資料庫     */    public void close(){        if(db != null){            db.close();        }    }

 

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.