Android 学习 数据库表增加 删除 修改 查询
2010-07-13 21:57:00 来源:WEB开发网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.SQLiteException;
/**
* 数据库操作对象
* @author xuzhenqin
*
*/
public class DiaryDbAdapter {
public static final String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_ROWID = "_id";
public static final String KEY_CREATED = "created";
private DatabaseHelper databaseHelper;
private Context context;
private SQLiteDatabase sqliteDatabase;
public DiaryDbAdapter(Context context)
{
this.context = context;
}
/**
* 打开数据库连接
*/
public void open()
{
databaseHelper = new DatabaseHelper(context);
try
{
sqliteDatabase = databaseHelper.getWritableDatabase();
}catch(SQLiteException ex)
{
sqliteDatabase = databaseHelper.getReadableDatabase();
}
}
/**
* 关闭数据库连接
*/
public void close()
{
sqliteDatabase.close();
}
/**
* 向数据库表中插入一条数据
* @param title 字段值
* @param body 字段值
*/
public long createDiary(String title, String body)
{
ContentValues content = new ContentValues();
content.put(KEY_TITLE, title);
content.put(KEY_BODY, body);
Calendar calendar = Calendar.getInstance();
String created = calendar.get(Calendar.YEAR) + "年"
+ calendar.get(Calendar.MONTH) + "月"
更多精彩
赞助商链接