数据库的操作,需用到数据库类SQLiteDatabase,重写管理员类SQLiteOpenHelper,用Cursor查询数据
一、操作数据库的辅助类,包含打开、关闭、增删改查方法
//自定义的数据库的接口,其中包含SQLiteHelper
public class NotesDbAdapter
{
//创建数据库所需语句及相关字符串
private static final String DATABASE_NAME = "note.db";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_TABLE = "note";
private static final String DATABASE_CREATE =
"create table note("
+"_id INTEGER PRIMARY KEY,"
+"note TEXT,"
+"date TEXT,"
+"created INTEGER,"
+"modified INTEGER"
+");";
//数据库对象
private SQLiteDatabase db;
//定义数据库管理员类
private static class DatabaseHelper extends
SQLiteOpenHelper
{
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
onCreate(db);
}
}
//抽象界面
private Context mCtx =
null;
//数据库管理员对象
private DatabaseHelper dbHelper ;
//数据库各列名称
public static final String KEY_ROWID = "_id";
public static final String KEY_NOTE = "note";
public static final String KEY_DATE = "date";
public static final String KEY_CREATED = "created";
public NotesDbAdapter(Context ctx) {
this.mCtx = ctx;
}
//打开数据库
public NotesDbAdapter open () throws SQLException {
dbHelper = new DatabaseHelper(mCtx);
db = dbHelper.getWritableDatabase();
return this;
}
//关闭数据库
public void close() {
dbHelper.close();
}
//查询所有项目,返回一个Cursor
public Cursor getall()
{
return db.query(DATABASE_TABLE,
new String[] {KEY_ROWID, KEY_NOTE, KEY_DATE,
KEY_CREATED},
null, null, null, null, null);
}
// 新增一项
public long create(String Note,String mDate) {
Date now = new Date();
ContentValues args = new ContentValues();
args.put(KEY_NOTE, Note);
args.put(KEY_DATE, mDate);
args.put(KEY_CREATED, now.getTime());
return db.insert(DATABASE_TABLE, null, args);
}
//删除一项
public boolean delete(long rowId) {
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null)
> 0;
}
//查询一项
public Cursor get(long rowId) throws SQLException
{
Cursor mCursor = db.query(true,
DATABASE_TABLE,
new String[] {KEY_ROWID, KEY_NOTE, KEY_DATE,
KEY_CREATED},
KEY_ROWID + "=" + rowId,
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//修改一项
public boolean update(long rowId, String note,String
date)
{
ContentValues args = new ContentValues();
args.put(KEY_NOTE, note);
args.put(KEY_DATE, date);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId,
null) > 0;
}
}
二、在主类中通过写好的辅助类操作数据库
private NotesDbAdapter mDbHelper;
private Cursor mNotesCursor;
//用自定义的数据库接口提供的方法打开数据库
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
//获取cursor,当需要表中数据时再通过cursor访问
mNotesCursor = mDbHelper.getall();
startManagingCursor(mNotesCursor);
//实例化各list存储数据库各列内容
aryListNoteContent=new
ArrayList();
aryListNoteDate=new
ArrayList();
aryListNoteID=new
ArrayList();
linkListNoteIcon=new
LinkedList();
//先用Cursor获取内容,装入ArrayList,最后关闭cursor
try
{
if(mNotesCursor.moveToFirst())
{
do
{
//获取数据库rowID,以便操作数据库
String noteId=mNotesCursor.getString(0);
String noteContent=mNotesCursor.getString(1);
String noteDate=mNotesCursor.getString(2);
aryListNoteID.add(noteId);
aryListNoteContent.add(noteContent);
aryListNoteDate.add(noteDate);
linkListNoteIcon.add(getApplicationContext().getResources().getDrawable(R.drawable.icon));
}while(mNotesCursor.moveToNext());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
mNotesCursor.close();
}
//删除一项
String RowId=aryListNoteID.get(info.position);
mDbHelper.delete(Long.parseLong(RowId));
//新增一项
mDbHelper.create(String,String);
//查询一项
//若该行id不为空,返回该行cursor,根据cursor读取内容,读取完毕关闭cursor
if (mRowId != null) {
Cursor cursor = mDbHelper.get(mRowId);
startManagingCursor(cursor);
try
{
mEditText.setText(cursor.getString(
cursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_NOTE)
));
} catch (IllegalArgumentExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
cursor.close();
}
}
//修改一项
mDbHelper.update(mRowId, String,String);