android日记本数据库源码,基于android的个人日记本程序(Sqlite数据库)
时间: 2024-05-03 17:19:12 浏览: 114
基于android的个人日记本程序(Sqlite数据库).zip
以下是一个基于 Android 平台的个人日记本程序的 SQLite 数据库源代码示例:
```java
public class DiaryDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "diary.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_DIARY = "diary";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_CONTENT = "content";
public static final String COLUMN_DATE = "date";
private static final String CREATE_TABLE_DIARY = "CREATE TABLE " +
TABLE_DIARY + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_TITLE + " TEXT," +
COLUMN_CONTENT + " TEXT," +
COLUMN_DATE + " INTEGER" +
")";
public DiaryDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_DIARY);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DIARY);
onCreate(db);
}
public long addDiary(String title, String content, long date) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_TITLE, title);
values.put(COLUMN_CONTENT, content);
values.put(COLUMN_DATE, date);
long id = db.insert(TABLE_DIARY, null, values);
db.close();
return id;
}
public List<Diary> getAllDiaries() {
List<Diary> diaries = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_DIARY;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Diary diary = new Diary();
diary.setId(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));
diary.setTitle(cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)));
diary.setContent(cursor.getString(cursor.getColumnIndex(COLUMN_CONTENT)));
diary.setDate(cursor.getLong(cursor.getColumnIndex(COLUMN_DATE)));
diaries.add(diary);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return diaries;
}
public Diary getDiary(long id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_DIARY, new String[]{COLUMN_ID,
COLUMN_TITLE, COLUMN_CONTENT, COLUMN_DATE}, COLUMN_ID + "=?",
new String[]{String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Diary diary = new Diary(
cursor.getInt(cursor.getColumnIndex(COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)),
cursor.getString(cursor.getColumnIndex(COLUMN_CONTENT)),
cursor.getLong(cursor.getColumnIndex(COLUMN_DATE))
);
cursor.close();
db.close();
return diary;
}
public void updateDiary(Diary diary) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_TITLE, diary.getTitle());
values.put(COLUMN_CONTENT, diary.getContent());
values.put(COLUMN_DATE, diary.getDate());
db.update(TABLE_DIARY, values, COLUMN_ID + " = ?",
new String[]{String.valueOf(diary.getId())});
db.close();
}
public void deleteDiary(Diary diary) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_DIARY, COLUMN_ID + " = ?",
new String[]{String.valueOf(diary.getId())});
db.close();
}
}
```
这段代码定义了一个名为 DiaryDatabaseHelper 的类,它继承自 SQLiteOpenHelper 类,用于创建和管理日记应用程序的 SQLite 数据库。在 onCreate() 方法中,它执行了一条 SQL 语句来创建一个名为 diary 的表,该表包含了标题、内容和日期三个字段。在 addDiary() 方法中,它向 diary 表中插入一条新的日记记录,并返回插入数据的 ID。在 getAllDiaries() 方法中,它查询 diary 表中的所有记录,并将它们转换成 Diary 对象的列表返回。在 getDiary() 方法中,它根据给定 ID 查询 diary 表中的一条记录,并将其转换为 Diary 对象返回。在 updateDiary() 和 deleteDiary() 方法中,它分别用于更新和删除给定的 Diary 对象对应的记录。
阅读全文