日期:2014-05-16 浏览次数:20472 次
package us.imnet.iceskysl.db.notepad; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; /** * 创建数据库对象 * @author xuzhenqin * */ public class DatabaseHelper extends SQLiteOpenHelper { //数据库名称 private final static String DATABASE_NAME = "notepad"; //数据库版本号 private final static int DATABASE_VERSION = 1; public static final String DATABASE_TABLE = "diary"; //数据表创建SQL语句 private static final String DATABASE_CREATE = "create table "+DATABASE_TABLE+" (_id integer primary key autoincrement, " + "title text not null, body text not null, created text not null);"; public DatabaseHelper(Context context) { //调用父类构造方法创建数据库 super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { //执行创建数据库SQL语句 db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //判断数据库是否存在 db.execSQL("DROP TABLE IF EXISTS diary"); onCreate(db); } }
package us.imnet.iceskysl.db.notepad; import java.util.Calendar; 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) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日" + calendar.get(Calendar.HOUR_OF_DAY) + "时" + calendar.get(Calendar.MINUTE) + "分"; content.put(KEY_CREATED, created); //content为插入表中的一条记录,类似与HASHMAP,是以键值对形式存储。 //insert方法第一参数:数据库表名,第二个参数如果CONTENT为空时则向表中插入一个NULL,第三个参数为插入的内容 return sqliteDatabase.insert(databaseHelper.DATABASE_TABLE, null, content); } /** * 删除表中符合条件的记录 * @param rowId 删除条件 * @return 是否删除成功 */ public boolean deleteDiary(long rowId) { //delete方法第一参数:数据库表名,第二个参数表示条件语句,第三个参数为条件带?的替代值 //返回值大于0表示删除成功 return sqliteDatabase.delete(databaseHelper.DATABASE_TABLE,KEY_ROW