自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何進(jìn)行Android數(shù)據(jù)庫(kù)操作

移動(dòng)開(kāi)發(fā) Android
當(dāng)程序有一個(gè)消息希望發(fā)出去的時(shí)候,它需要將消息封裝成一個(gè)Android數(shù)據(jù)庫(kù),并發(fā)送。這時(shí)候,應(yīng)該是有一個(gè)統(tǒng)一的中心。

強(qiáng)烈建議,在自己Android數(shù)據(jù)庫(kù)接收或發(fā)出一個(gè)系統(tǒng)action的時(shí)候,要名副其實(shí)。比如你響應(yīng)一個(gè)view動(dòng)作,做的確實(shí)edit的勾當(dāng),你發(fā)送一個(gè)pick消息,其實(shí)你想讓別人做edit的事,這樣都會(huì)造成混亂。

一個(gè)好的習(xí)慣是創(chuàng)建一個(gè)輔助類來(lái)簡(jiǎn)化你的Android數(shù)據(jù)庫(kù)交互??紤]創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)適配器,來(lái)添加一個(gè)與數(shù)據(jù)庫(kù)交互的包裝層。它應(yīng)該提供直觀的、強(qiáng)類型的方法,如添加、刪除和更新項(xiàng)目。數(shù)據(jù)庫(kù)適配器還應(yīng)該處理查詢和對(duì)創(chuàng)建、打開(kāi)和關(guān)閉數(shù)據(jù)庫(kù)的包裝。

它還常用靜態(tài)的Android數(shù)據(jù)庫(kù)常量來(lái)定義表的名字、列的名字和列的索引。下面的代碼片段顯示了一個(gè)標(biāo)準(zhǔn)數(shù)據(jù)庫(kù)適配器類的框架。它包括一個(gè)SQLiteOpenHelper類的擴(kuò)展類,用于簡(jiǎn)化打開(kāi)、創(chuàng)建和更新數(shù)據(jù)庫(kù)。

  1. import android.content.Context;  
  2.  
  3. import android.database.*;  
  4.  
  5. import android.database.sqlite.*;  
  6.  
  7. import android.database.sqlite.SQLiteDatabase.CursorFactory;  
  8.  
  9. import android.util.Log;  
  10.  
  11. public class MyDBAdapter   
  12.  
  13. {  // The name and column index of each column in your database.  
  14.  
  15. public static final String KEY_NAME=”name”;  
  16.  
  17. public static final int NAME_COLUMN = 1;  
  18.  
  19.    
  20.  
  21. // TODO: Create public field for each column in your table.  
  22.  
  23. // SQL Statement to create a new database.  
  24.  
  25. private static final String DATABASE_CREATE = “create table “ +  
  26.  
  27. DATABASE_TABLE + “ (“ + KEY_ID + “ integer primary key autoincrement, “ +  
  28.  
  29. KEY_NAME + “ text not null);”;  
  30.  
  31.    
  32.  
  33. // Variable to hold the database instance  
  34.  
  35. private SQLiteDatabase db;  
  36.  
  37.    
  38.  
  39. // Context of the application using the database.  
  40.  
  41. private final Context context;  
  42.  
  43.    
  44.  
  45. // Database open/upgrade helper  
  46.  
  47. private myDbHelper dbHelper;  
  48.  
  49.    
  50.  
  51. public MyDBAdapter(Context _context) {  
  52.  
  53. context = _context;  
  54.  
  55. dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);  
  56.  
  57. }  
  58.  
  59.    
  60.  
  61. public MyDBAdapter open() throws SQLException {  
  62.  
  63. db = dbHelper.getWritableDatabase();  
  64.  
  65. return this;  
  66.  
  67. }  
  68.  
  69.    
  70.  
  71. public void close() {  
  72.  
  73. db.close();  
  74.  
  75. }  
  76.  
  77.    
  78.  
  79. public long insertEntry(MyObject _myObject) {  
  80.  
  81. ContentValues contentValues = new ContentValues();  
  82.  
  83. // TODO fill in ContentValues to represent the new row  
  84.  
  85. return db.insert(DATABASE_TABLE, null, contentValues);  
  86.  
  87. }  
  88.  
  89.    
  90.  
  91. public boolean removeEntry(long _rowIndex) {  
  92.  
  93. return db.delete(DATABASE_TABLE, KEY_ID + “=” + _rowIndex, null) > 0;  
  94.  
  95. }  
  96.  
  97. public Cursor getAllEntries () {  
  98.  
  99. return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME},  
  100.  
  101. null, null, null, null, null);  
  102.  
  103. }  
  104.  
  105. public MyObject getEntry(long _rowIndex) {  
  106.  
  107. MyObject objectInstance = new MyObject();  
  108.  
  109. // TODO Return a cursor to a row from the database and  
  110.  
  111. // use the values to populate an instance of MyObject  
  112.  
  113. return objectInstance;  
  114.  
  115. }  
  116.  
  117. public int updateEntry(long _rowIndex, MyObject _myObject) {  
  118.  
  119. String where = KEY_ID + “=” + _rowIndex;  
  120.  
  121. ContentValues contentValues = new ContentValues();  
  122.  
  123. // TODO fill in the ContentValue based on the new object  
  124.  
  125. return db.update(DATABASE_TABLE, contentValues, where, null);  
  126.  
  127. }  
  128.  
  129.    
  130.  
  131. private static class myDbHelper extends SQLiteOpenHelper   
  132.  
  133. {  
  134.  
  135. public myDbHelper(Context context, String name, CursorFactory factory, int version) {  
  136.  
  137. super(context, name, factory, version);  
  138.  
  139. }  
  140.  
  141.    
  142.  
  143. // Called when no database exists in  
  144.  
  145. // disk and the helper class needs  
  146.  
  147. // to create a new one.  
  148.  
  149. @Override  
  150.  
  151. public void onCreate(SQLiteDatabase _db) {  
  152.  
  153. _db.execSQL(DATABASE_CREATE);  
  154.  
  155. }  

【編輯推薦】

  1. Android應(yīng)用程序組建原理深入剖析 
  2. Android SMS短信服務(wù)相關(guān)概念簡(jiǎn)述 
  3. PythonAndroid數(shù)據(jù)庫(kù)相關(guān)代碼解讀 
  4. PythonAndroid安裝卸載程序具體操作方法解析 
  5. Android應(yīng)用程序的四個(gè)關(guān)鍵點(diǎn) 
責(zé)任編輯:chenqingxiang 來(lái)源: 博客園
相關(guān)推薦

2010-02-05 16:35:35

Android操作系統(tǒng)

2010-05-24 14:57:03

MySQL數(shù)據(jù)庫(kù)表

2011-05-25 00:00:00

數(shù)據(jù)庫(kù)設(shè)計(jì)

2009-07-15 18:01:53

Jython數(shù)據(jù)庫(kù)

2010-08-17 09:48:40

DB2 分區(qū)數(shù)據(jù)庫(kù)

2009-02-02 13:43:19

故障檢測(cè)數(shù)據(jù)庫(kù)

2018-02-26 20:00:00

編程語(yǔ)言JavaMySQL

2021-07-28 15:44:52

Java開(kāi)發(fā)數(shù)據(jù)庫(kù)

2024-02-23 11:36:57

數(shù)據(jù)庫(kù)Python

2010-05-18 11:04:11

MySQL數(shù)據(jù)庫(kù)

2009-12-29 13:31:55

ADO連接ACCESS

2010-01-15 17:31:18

C++Test

2010-02-01 16:22:36

Python字符串操作

2010-07-26 16:23:46

Telnet 110

2010-07-02 14:46:20

SQL Server數(shù)

2020-09-07 12:59:10

NoSQL數(shù)據(jù)庫(kù)數(shù)據(jù)

2010-08-17 16:04:29

DB2數(shù)據(jù)庫(kù)

2018-09-17 16:12:03

數(shù)據(jù)庫(kù)數(shù)據(jù)恢復(fù)SQL Server

2024-04-03 10:05:02

2022-06-29 09:14:45

PolarDB云原生數(shù)據(jù)庫(kù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)