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

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

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

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

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 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);

}

// 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);

}

}

}

与Android数据库一起工作相关推荐

  1. Android数据库高手秘籍(三)——使用LitePal升级表

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/39151617 在上一篇文章中,我们学习了LitePal的基本使用方法,体验了使用框 ...

  2. Android系统Recovery工作原理之使用update.zip升级过程分析(五)

    Android系统Recovery工作原理之使用update.zip升级过程分析(五)---update.zip包从上层进入Recovery服务文章开头我们就提到update.zip包来源有两种,一个 ...

  3. Android数据库高手秘籍(二):创建表和LitePal的基本用法

    原文:http://blog.jobbole.com/77157/ 上一篇文章中我们学习了一些Android数据库相关的基础知识,和几个颇为有用的SQLite命令,都是直接在命令行操作的.但是我们都知 ...

  4. Android 数据库综述(二) 程序计算器与信号量来处理多线程并发问题

    Android 数据库综述(二) 程序计算器与信号量来处理多线程并发问题 多线程操作数据库,为处理并发问题,大家第一想到的是加锁操作 ,SQLite是文件级别的锁.SQLite3对于并发的处理机制是允 ...

  5. android 数据库 字节数组,java - 如何使用活动的android序列化字节数组并将其存储到数据库中? - 堆栈内存溢出...

    我有一个图像作为byte[] ,我需要将此图像保存在数据库中. 我为此使用Active Android库. 我知道db中用于此目的的数据类型应该是BLOB. 我知道byte[]不能直接存储,我知道它应 ...

  6. Android数据库操作-1

    Android采用关系型数据库SQLite3,它是一个支持SQL轻量级的嵌入式数据库,在嵌入式操作系统上有很广泛的应用,WM采用的也是SQLite3<?xml:namespace prefix ...

  7. Android数据库框架总结

    本文转自:http://blog.csdn.net/da_caoyuan/article/details/61414626 一:OrmLite 简述: 优点: 1.轻量级:2.使用简单,易上手:3.封 ...

  8. android 本地数据库持久化框架,android数据库持久化框架, ormlite框架,

    前言 Android中内置了SQLite,但是对于数据库操作这块,非常的麻烦.其实可以试用第3方的数据库持久化框架对之进行结构上调整, 摆脱了访问数据库操作的细节,不用再去写复杂的SQL语句.虽然这样 ...

  9. Android数据库高手秘籍(六)——LitePal的修改和删除操作

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/40083685 在上一篇文章中,我们学会了使用LitePal进行存储数据的功能.确实 ...

最新文章

  1. Mycat 月分片方法 - pursuer.chen - 博客园
  2. 51nod 1451 合法三角形 判斜率去重,时间复杂度O(n^2)
  3. JavaScript中的坐标
  4. 如何在win10+VS2017环境下新建一个简单的WDF示例程序
  5. 2020CCPC长春
  6. html5长按 排序,H5 长按 拖拽排序的实现
  7. python实例化次数怎么算_关于python多次实例化
  8. var a=function和function b有什么区别
  9. 别再售卖 5块钱 的 Win10 激活码了,后果很严重
  10. 使用POI在Excel单元格插入符号(Symbol)
  11. html列表自动无限循环滚动,js 无限循环垂直滚动列表
  12. 5.接口参数过滤(phalapi框架总结)
  13. 揭秘IBM架构设计方法论 —— Solution Design II
  14. 数据分析神器Alteryx
  15. 关于压缩工具7-zip的7z脚本用法
  16. macbook历代_苹果Mac历代重大变革
  17. PDF爱好者在线工具
  18. 中专计算机应用基础 名词解释,(word)中职计算机应用基础操作系统试题.doc
  19. batch / numpy for pytorch (lyh
  20. linux内核同步机制-RCU(3)

热门文章

  1. Laravel源码解析之Eloquent Model
  2. 插入排序算法(C实现)
  3. python多线程插入1万条数据
  4. 数据时代总结思维导图模板分享及绘制技巧
  5. Python Django 之 Views HttpRequest HttpReponse
  6. 【Linux】远程连接Linux系统及故障排查
  7. 从阿里、微软、AWS财报看评云计算发展
  8. [转]使用VS2010的Database 项目模板统一管理数据库对象
  9. StructureMap极速上手指南(翻译)
  10. 关羽第三方证书导入的CASE