如何进行Android数据库操作

移动开发 Android
当程序有一个消息希望发出去的时候,它需要将消息封装成一个Android数据库,并发送。这时候,应该是有一个统一的中心。

强烈建议,在自己Android数据库接收或发出一个系统action的时候,要名副其实。比如你响应一个view动作,做的确实edit的勾当,你发送一个pick消息,其实你想让别人做edit的事,这样都会造成混乱。

一个好的习惯是创建一个辅助类来简化你的Android数据库交互。考虑创建一个数据库适配器,来添加一个与数据库交互的包装层。它应该提供直观的、强类型的方法,如添加、删除和更新项目。数据库适配器还应该处理查询和对创建、打开和关闭数据库的包装。

它还常用静态的Android数据库常量来定义表的名字、列的名字和列的索引。下面的代码片段显示了一个标准数据库适配器类的框架。它包括一个SQLiteOpenHelper类的扩展类,用于简化打开、创建和更新数据库。

  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应用程序组建原理深入剖析 
  2. Android SMS短信服务相关概念简述 
  3. PythonAndroid数据库相关代码解读 
  4. PythonAndroid安装卸载程序具体操作方法解析 
  5. Android应用程序的四个关键点 
责任编辑:chenqingxiang 来源: 博客园
相关推荐

2010-02-05 16:35:35

Android操作系统

2010-05-24 14:57:03

MySQL数据库表

2009-07-15 18:01:53

Jython数据库

2011-05-25 00:00:00

数据库设计

2010-08-17 09:48:40

DB2 分区数据库

2009-02-02 13:43:19

故障检测数据库

2018-02-26 20:00:00

编程语言JavaMySQL

2021-07-28 15:44:52

Java开发数据库

2024-02-23 11:36:57

数据库Python

2010-05-18 11:04:11

MySQL数据库

2010-07-26 16:23:46

Telnet 110

2010-02-01 16:22:36

Python字符串操作

2010-01-15 17:31:18

C++Test

2009-12-29 13:31:55

ADO连接ACCESS

2020-09-07 12:59:10

NoSQL数据库数据

2010-07-02 14:46:20

SQL Server数

2018-09-17 16:12:03

数据库数据恢复SQL Server

2010-08-17 16:04:29

DB2数据库

2022-06-29 09:14:45

PolarDB云原生数据库

2010-02-06 14:52:53

Android平台
点赞
收藏

51CTO技术栈公众号