作者:empty 出版社:empty |
Sqlite操作实例
使用Eclipse创建一个Android项目,取名为Database,如图1所示:
使用Eclipse创建你的Android新项目
创建DBAdapter辅助类
操作数据库的最佳实践是创建一个辅助类,由它封装所有对数据库的复杂访问,对于调用代码而言它是透明的,因此我创建了一个DBAdapter的辅助类,由它创建、打开、关闭和使用SQLite数据库。
首先,在src/文件夹(在这个例子中是src/cn.huamei.Database)下添加一个DBAdapter.java文件,导入所有你要使用到的命名空间:
package cn.huamei.Database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
}
接下来创建一个数据库,取名为books,字段有:“id”,”isbn”,”title”,”publisher”
在DBAdapter.java文件中,定义一些常量:
public class DBAdapter {
public static final String KEY_ROWID = id ;
public static final String KEY_ISBN = isbn ;
public static final String KEY_TITLE = title ;
public static final String KEY_PUBLISHER = publisher ;
private static final String TAG = DBAdapter ;
private static final String DATABASE_NAME = books ;
private static final String DATABASE_TABLE = titles ;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = create table titles (id integer primary key autoincrement, + isbn text not null, title text not null, + publisher text not null); ;
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{db.execSQL(DATABASE_CREATE); }
}
你可以扩展SQLiteOpenHelper类,它是一个Android辅助类,主要用于数据库创建和版本管理。实际上,你可以覆盖onCreate()和onUpgrade()方法:
public class DBAdapter {
public static final String KEY_ROWID = id ;
public static final String KEY_ISBN = isbn ;
public static final String KEY_TITLE = title ;
public static final String KEY_PUBLISHER = publisher ;
private static final String TAG = DBAdapter ;
private static final String DATABASE_NAME = books ;
private static final String DATABASE_TABLE = titles ;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = create table titles (id integer primary key autoincrement, + isbn text not null, title text not null, + publisher text not null); ;
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) {
Log.w(TAG, Upgrading database from version + oldVersion+ to + newVersion + , which will destroy all old data );
db.execSQL( DROP TABLE IF EXISTS titles );
onCreate(db);
}
}
}
你还可以定义打开和关闭数据库以及增加/编辑/删除表中行的方法:
public class DBAdapter {
public static final String KEY_ROWID = id ;
public static final String KEY_ISBN = isbn ;
public static final String KEY_TITLE = title ;
public static final String KEY_PUBLISHER = publisher ;
private static final String TAG = DBAdapter ;
private static final String DATABASE_NAME = books ;
private static final String DATABASE_TABLE = titles ;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = create table titles (id integer primary key autoincrement, + isbn text not null, title text not null, + publisher text not null); ;
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion)
{
Log.w(TAG, Upgrading database from version + oldVersion+ to + newVersion + , which will destroy all old data );
db.execSQL( DROP TABLE IF EXISTS titles );
onCreate(db);
}
}
}
//---打开数据库---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---关闭数据库---
public void close()
{
DBHelper.close();
}
//---向数据库中插入一个数据---
public long insertTitle(String isbn, String title, String publisher)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ISBN, isbn);
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_PUBLISHER, publisher);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---删除一个指定数据---
public boolean deleteTitle(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + = + rowId, null) > 0;
}
//---检索所有数据---
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID,KEY_ISBN,KEY_TITLE,KEY_PUBLISHER},null,null,null,null,null);
}
//---检索一个指定数据---
public Cursor getTitle(long rowId) throws SQLException
{
Cursor mCursor =db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,KEY_ISBN,KEY_TITLE,KEY_PUBLISHER},KEY_ROWID + = + rowId,null,null,null,null,null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---更新一个数据---
public boolean updateTitle(long rowId, String isbn,String title, String publisher)
{
ContentValues args = new ContentValues();
args.put(KEY_ISBN, isbn);
args.put(KEY_TITLE, title);
args.put(KEY_PUBLISHER, publisher);
return db.update(DATABASE_TABLE, args,KEY_ROWID + = + rowId, null) > 0;
}
}
使用数据库
现在你已经可以利用创建的辅助类来使用数据库,DatabaseActivity.java文件中,创建一个DBAdapter类的实例:
package cn.huamei.Database;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
public class DatabaseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBAdapter db = new DBAdapter(this);
---添加两条数据---
db.open();
long id;
id = db.insertTitle( 0470285818 , C# 2008 Programmer's Reference , Wrox );
id = db.insertTitle( 047017661X , Professional Windows Vista Gadgets Programming , Wrox );
---取出所有数据---
Cursor c = db.getAllTitles();
if (c.moveToFirst())
{
do {
DisplayTitle(c);
} while (c.moveToNext());
}
db.close();
}
---显示数据---
public void DisplayTitle(Cursor c)
{ Toast.makeText(this, id: + c.getString(0) + n + ISBN: + c.getString(1) + n + TITLE: + c.getString(2) + n + PUBLISHER: + c.getString(3),Toast.LENGTH_LONG).show();
}
}