您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)Android中怎么對數(shù)據(jù)庫進(jìn)行操作,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
一個好的習(xí)慣是創(chuàng)建一個輔助類來簡化你的Android數(shù)據(jù)庫交互??紤]創(chuàng)建一個數(shù)據(jù)庫適配器,來添加一個與數(shù)據(jù)庫交互的包裝層。它應(yīng)該提供直觀的、強(qiáng)類型的方法,如添加、刪除和更新項(xiàng)目。數(shù)據(jù)庫適配器還應(yīng)該處理查詢和對創(chuàng)建、打開和關(guān)閉數(shù)據(jù)庫的包裝。
它還常用靜態(tài)的Android數(shù)據(jù)庫常量來定義表的名字、列的名字和列的索引。下面的代碼片段顯示了一個標(biāo)準(zhǔn)數(shù)據(jù)庫適配器類的框架。它包括一個SQLiteOpenHelper類的擴(kuò)展類,用于簡化打開、創(chuàng)建和更新數(shù)據(jù)庫。
import android.content.Context; import android.database.*; import android.database.sqlite.*; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.util.Log; public class MyDBAdapter { // The name and column index of each column in your database. public static final String KEY_NAME=”name”; public static final int NAME_COLUMN = 1; // TODO: Create public field for each column in your table. // SQL Statement to create a new database. private static final String DATABASE_CREATE = “create table “ + DATABASE_TABLE + “ (“ + KEY_ID + “ integer primary key autoincrement, “ + KEY_NAME + “ text not null);”; // Variable to hold the database instance private SQLiteDatabase db; // Context of the application using the database. private final Context context; // Database open/upgrade helper private myDbHelper dbHelper; public MyDBAdapter(Context _context) { context = _context; dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION); } public MyDBAdapter open() throws SQLException { db = dbHelper.getWritableDatabase(); return this; } public void close() { db.close(); } public long insertEntry(MyObject _myObject) { ContentValues contentValues = new ContentValues(); // TODO fill in ContentValues to represent the new row return db.insert(DATABASE_TABLE, null, contentValues); } public boolean removeEntry(long _rowIndex) { return db.delete(DATABASE_TABLE, KEY_ID + “=” + _rowIndex, null) > 0; } public Cursor getAllEntries () { return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME}, null, null, null, null, null); } public MyObject getEntry(long _rowIndex) { MyObject objectInstance = new MyObject(); // TODO Return a cursor to a row from the database and // use the values to populate an instance of MyObject return objectInstance; } public int updateEntry(long _rowIndex, MyObject _myObject) { String where = KEY_ID + “=” + _rowIndex; ContentValues contentValues = new ContentValues(); // TODO fill in the ContentValue based on the new object return db.update(DATABASE_TABLE, contentValues, where, null); } private static class myDbHelper extends SQLiteOpenHelper { public myDbHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } // Called when no database exists in // disk and the helper class needs // to create a new one. @Override public void onCreate(SQLiteDatabase _db) { _db.execSQL(DATABASE_CREATE); }
以上就是Android中怎么對數(shù)據(jù)庫進(jìn)行操作,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。