Android数据库工作方式解析

移动开发 Android
Android数据库在实际应用中占据着重要的角色。我们会通过本文给出的一段代码示例来对此进行详细的解读,方便大家学习。

在手机系统领域中,谷歌的Android操作系统算是一个新起之秀。但是其优秀的性能以及开源性,使其一经推出就伸手广大用户的好评。在这里我们可以从Android数据库的相关操作来体验这一系统给我们带来的好处。#t#

一个好的习惯是创建一个辅助类来简化你的数据库交互。

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

它还常用静态的Android数据库常量来定义表的名字、列的名字和列的索引。

下面的代码片段显示了一个标准数据库适配器类的框架。它包括一个SQLiteOpenHelper类的扩展类,用于简化打开、创建和更新Android数据库。

import android.content.Context;  
import android.database.*;  
import android.database.sqlite.*;  
import android.database.sqlite.SQLiteDatabase.CursorFactory;  
import android.util.Log;  
public class MyDBAdapter   
{  
private static final String DATABASE_NAME = “myDatabase.db”;  
private static final String DATABASE_TABLE = “mainTable”;  
private static final int DATABASE_VERSION = 1;  
// The index (key) column name for use in where clauses.  
public static final String KEY_ID=”_id”;  
// 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 helpe  
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);   }   // Called when there is a database version mismatch meaning that   // the version of the database on disk needs to be upgraded to   // the current version.   @Override   public void onUpgrade(SQLiteDatabase _db, int _oldVersion, 
int _newVersion) {  
// Log the version upgrade.   Log.w(“TaskDBAdapter”, “Upgrading from version “ +   _oldVersion + “ to “ + _newVersion +   “, which will destroy all old data”);   // Upgrade the existing database to conform to the new version.   // Multiple previous versions can be handled by comparing   // _oldVersion and _newVersion values.   // The simplest case is to drop the old table and create a   // new one.   _db.execSQL(“DROP TABLE IF EXISTS “ + DATABASE_TABLE);   // Create a new one.   onCreate(_db);   }   }  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.

Android数据库的相关操作就为大家介绍到这里。

责任编辑:曹凯 来源: 博客园
相关推荐

2010-08-06 10:41:59

Flex命名空间

2009-06-22 16:42:26

JSF的工作方式

2009-09-28 13:39:01

Hibernate工作

2009-07-14 12:47:07

WebWork工作方式

2009-07-10 13:55:48

Swing控件

2009-08-13 18:36:29

C#数组工作方式

2010-07-22 09:01:02

SQL Server镜

2011-04-19 10:23:00

路由器网桥

2011-04-19 10:29:57

路由器路由网关

2011-04-19 10:25:44

路由算法路由器

2016-10-27 17:49:07

群晖群晖科技NAS

2009-02-25 10:52:00

路由器原理工作方式协议

2012-11-27 09:12:39

思杰移动云计算

2023-03-20 15:34:00

ChatGPT人工智能

2024-03-27 14:51:23

2023-01-04 07:39:39

2021-12-07 10:18:06

首席信息官技术发展企业管理者

2011-07-14 13:20:49

Servlet过滤器

2011-11-04 09:52:24

Siri云计算苹果

2011-11-04 09:45:43

Siri
点赞
收藏

51CTO技术栈公众号