From e7deb0e3accac81a1eebba3423df7ccf4752ad5c Mon Sep 17 00:00:00 2001 From: dingxinnan <1175943266@qq.com> Date: Thu, 21 Dec 2023 00:30:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notes/data/NotesDatabaseHelper.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Notes-master1/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java b/src/Notes-master1/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java index ffe5d57..846b2b5 100644 --- a/src/Notes-master1/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java +++ b/src/Notes-master1/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java @@ -28,10 +28,13 @@ import net.micode.notes.data.Notes.NoteColumns; public class NotesDatabaseHelper extends SQLiteOpenHelper { + // 数据库名称 private static final String DB_NAME = "note.db"; + // 数据库版本号 private static final int DB_VERSION = 4; + // 表名 public interface TABLE { public static final String NOTE = "note"; @@ -42,6 +45,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { private static NotesDatabaseHelper mInstance; + // 创建note表的sql语句 private static final String CREATE_NOTE_TABLE_SQL = "CREATE TABLE " + TABLE.NOTE + "(" + NoteColumns.ID + " INTEGER PRIMARY KEY," + @@ -63,7 +67,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" + ")"; - private static final String CREATE_DATA_TABLE_SQL = + private static final String CREATE_DATA_TABLE_SQL = "CREATE TABLE " + TABLE.DATA + "(" + DataColumns.ID + " INTEGER PRIMARY KEY," + DataColumns.MIME_TYPE + " TEXT NOT NULL," + @@ -106,7 +110,6 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID + " AND " + NoteColumns.NOTES_COUNT + ">0" + ";" + " END"; - /** * Increase folder's note count when insert new note to the folder */ @@ -206,6 +209,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + " END"; + // 创建NotesDatabaseHelper类,用于创建数据库,并创建note表,触发器,系统文件夹 public NotesDatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @@ -216,8 +220,8 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { createSystemFolder(db); Log.d(TAG, "note table has been created"); } - - private void reCreateNoteTableTriggers(SQLiteDatabase db) { + private void reCreateNoteTableTriggers(SQLiteDatabase db) { + // 删除已存在的触发器,如果它们存在 db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_update"); db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_update"); db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_delete"); @@ -226,6 +230,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { db.execSQL("DROP TRIGGER IF EXISTS folder_delete_notes_on_delete"); db.execSQL("DROP TRIGGER IF EXISTS folder_move_notes_on_trash"); + // 创建新的触发器,使用新的触发器创建触发器函数 db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER); db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER); db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER); @@ -288,6 +293,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { } static synchronized NotesDatabaseHelper getInstance(Context context) { + // 获取NotesDatabaseHelper实例,如果mInstance为空,则创建一个新的实例 if (mInstance == null) { mInstance = new NotesDatabaseHelper(context); } @@ -296,33 +302,40 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { @Override public void onCreate(SQLiteDatabase db) { + // 创建note表和data表 createNoteTable(db); createDataTable(db); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + // 判断是否需要重新创建触发器 boolean reCreateTriggers = false; + // 是否跳过v2升级 boolean skipV2 = false; if (oldVersion == 1) { + // 升级到v2 upgradeToV2(db); skipV2 = true; // this upgrade including the upgrade from v2 to v3 oldVersion++; } if (oldVersion == 2 && !skipV2) { + // 升级到v3 upgradeToV3(db); reCreateTriggers = true; oldVersion++; } if (oldVersion == 3) { + // 升级到v4 upgradeToV4(db); oldVersion++; } if (reCreateTriggers) { + // 重新创建触发器 reCreateNoteTableTriggers(db); reCreateDataTableTriggers(db); }