标注的data包 #10

Merged
p58j6v9mz merged 1 commits from niezhiwei_branch into develop 11 months ago

@ -16,58 +16,71 @@
package net.micode.notes.data; package net.micode.notes.data;
import android.content.Context; /**
import android.database.Cursor; *Android
import android.provider.ContactsContract.CommonDataKinds.Phone; */
import android.provider.ContactsContract.Data;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import java.util.HashMap; import android.content.Context; //用于获取应用程序的上下文。
import android.database.Cursor; //用于在查询结果中移动并访问特定列的数据。
import android.provider.ContactsContract.CommonDataKinds.Phone; //用于访问通讯录中电话号码相关的数据。
import android.provider.ContactsContract.Data; //通讯录数据的内容提供者,用于访问通讯录数据。
import android.telephony.PhoneNumberUtils; //包含用于处理电话号码的实用方法。
import android.util.Log; //用于在Android中记录调试信息的类。
import java.util.HashMap; //Java中的HashMap类用于存储联系人姓名和手机号码的映射关系。
public class Contact { public class Contact {
private static HashMap<String, String> sContactCache; private static HashMap<String, String> sContactCache;
private static final String TAG = "Contact"; private static final String TAG = "Contact"; //包含一个私有静态变量sContactCache
//用于缓存联系人信息以及一个私有静态常量TAG用于在日志中标识这个类。
//键值对数据结构电话号码可以作为键联系人姓名作为值TAG是用于日志记录的标签常量。
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER //PHONE_NUMBERS_EQUAL()是一个内置函数,用于比较两个电话号码是否相等。
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"//Phone.NUMBER是通讯录中电话号码的列名
+ " AND " + Data.RAW_CONTACT_ID + " IN " //Data.MIMETYPE是通讯录数据中的MIME类型列名用于指定查询电话号码的数据类型为电话号码。
+ "(SELECT raw_contact_id " //Phone.CONTENT_ITEM_TYPE是电话号码的MIME类型值。
+ " FROM phone_lookup" //Data.RAW_CONTACT_ID是通讯录数据中的原始联系人ID列名。
+ " WHERE min_match = '+')"; //phone_lookup是一个表名用于存储电话号码的查找结果。
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER /**
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" * .
+ " AND " + Data.RAW_CONTACT_ID + " IN " * @param context
+ "(SELECT raw_contact_id " * @param phoneNumber
+ " FROM phone_lookup" * @return
+ " WHERE min_match = '+')"; */
public static String getContact(Context context, String phoneNumber) { public static String getContact(Context context, String phoneNumber) {
if(sContactCache == null) { if(sContactCache == null) { //首先检查sContactCache是否为null如果是则初始化一个新的HashMap.
sContactCache = new HashMap<String, String>(); sContactCache = new HashMap<String, String>();
} }
if(sContactCache.containsKey(phoneNumber)) { if(sContactCache.containsKey(phoneNumber)) { //接下来如果sContactCache中包含给定电话号码的键值对则直接返回缓存中的联系人姓名。
return sContactCache.get(phoneNumber); return sContactCache.get(phoneNumber);
} }
String selection = CALLER_ID_SELECTION.replace("+", String selection = CALLER_ID_SELECTION.replace("+", //首先将给定的电话号码转换为最小匹配形式然后使用replace()方法将查询条件中的"+"替换为最小匹配的电话号码形式。
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
Cursor cursor = context.getContentResolver().query( Cursor cursor = context.getContentResolver().query( //执行了一个查询操作该方法会返回一个Cursor对象用于遍历查询结果
Data.CONTENT_URI, Data.CONTENT_URI, //指定了要查询的数据表的URI表示通讯录数据。
new String [] { Phone.DISPLAY_NAME }, new String [] { Phone.DISPLAY_NAME }, //是要查询的列名,这里是查询联系人的显示名称。
selection, selection, //是查询条件,根据前面处理过的最小匹配电话号码来查询匹配的联系人信息。
new String[] { phoneNumber }, new String[] { phoneNumber }, //是查询条件中的参数值,用于替换查询条件中的"?"占位符。
null); null); //最后一个参数是排序方式,这里为空,表示不进行排序。
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) { //这个条件语句检查Cursor对象是否不为空并且能够将游标移动到第一行。如果满足条件则表示查询结果非空且至少存在一个匹配项。
try { try {
String name = cursor.getString(0); String name = cursor.getString(0); //获取查询结果中第一列索引为0的数据即联系人的显示名称。
sContactCache.put(phoneNumber, name); sContactCache.put(phoneNumber, name); //将此信息存储在sContactCache缓存中
return name; return name;
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) { //如果在获取查询结果的过程中发生异常说明查询结果中没有有效数据此时会打印错误日志并返回null
Log.e(TAG, " Cursor get string error " + e.toString()); Log.e(TAG, " Cursor get string error " + e.toString());
return null; return null;
} finally { } finally { //无论是否出现异常最后都会通过cursor.close()方法关闭Cursor对象释放资源。
cursor.close(); cursor.close();
} }
} else { } else { //如果查询结果为空或者没有匹配项就会执行这个分支。它会打印调试日志指示没有找到与给定电话号码匹配的联系人并返回null。
Log.d(TAG, "No contact matched with number:" + phoneNumber); Log.d(TAG, "No contact matched with number:" + phoneNumber);
return null; return null;
} }
} }
} }

@ -18,9 +18,9 @@ package net.micode.notes.data;
import android.net.Uri; import android.net.Uri;
public class Notes { public class Notes {
public static final String AUTHORITY = "micode_notes"; public static final String AUTHORITY = "micode_notes"; //定义了一个字符串常量AUTHORITY代表了内容提供者的授权信息。
public static final String TAG = "Notes"; public static final String TAG = "Notes"; //定义了一个字符串常量TAG可能用于在日志中进行标记或调试。
public static final int TYPE_NOTE = 0; public static final int TYPE_NOTE = 0; //定义了三个整型常量,分别表示笔记、文件夹和系统类型。
public static final int TYPE_FOLDER = 1; public static final int TYPE_FOLDER = 1;
public static final int TYPE_SYSTEM = 2; public static final int TYPE_SYSTEM = 2;
@ -30,23 +30,23 @@ public class Notes {
* {@link Notes#ID_TEMPARAY_FOLDER } is for notes belonging no folder * {@link Notes#ID_TEMPARAY_FOLDER } is for notes belonging no folder
* {@link Notes#ID_CALL_RECORD_FOLDER} is to store call records * {@link Notes#ID_CALL_RECORD_FOLDER} is to store call records
*/ */
public static final int ID_ROOT_FOLDER = 0; public static final int ID_ROOT_FOLDER = 0; //定义了一些文件夹的标识符常量。ID_ROOT_FOLDER表示根文件夹的标识符为0
public static final int ID_TEMPARAY_FOLDER = -1; public static final int ID_TEMPARAY_FOLDER = -1; //表示临时文件夹的标识符为-1
public static final int ID_CALL_RECORD_FOLDER = -2; public static final int ID_CALL_RECORD_FOLDER = -2; //表示通话记录文件夹的标识符为-2
public static final int ID_TRASH_FOLER = -3; public static final int ID_TRASH_FOLER = -3; //ID_TRASH_FOLER表示回收站文件夹的标识符为-3
//定义了一些 Intent 传递参数的键名常量
public static final String INTENT_EXTRA_ALERT_DATE = "net.micode.notes.alert_date"; public static final String INTENT_EXTRA_ALERT_DATE = "net.micode.notes.alert_date";//日期
public static final String INTENT_EXTRA_BACKGROUND_ID = "net.micode.notes.background_color_id"; public static final String INTENT_EXTRA_BACKGROUND_ID = "net.micode.notes.background_color_id";//背景颜色
public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id"; public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id";//小部件ID
public static final String INTENT_EXTRA_WIDGET_TYPE = "net.micode.notes.widget_type"; public static final String INTENT_EXTRA_WIDGET_TYPE = "net.micode.notes.widget_type";//小部件类型
public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_id"; public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_id";//文件夹ID
public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date"; public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date";//通话日期
public static final int TYPE_WIDGET_INVALIDE = -1; public static final int TYPE_WIDGET_INVALIDE = -1; //无效小部件类型
public static final int TYPE_WIDGET_2X = 0; public static final int TYPE_WIDGET_2X = 0; //2x大小的小部件类型
public static final int TYPE_WIDGET_4X = 1; public static final int TYPE_WIDGET_4X = 1; //4x
public static class DataConstants { public static class DataConstants { //内部静态类(定义在一个类的内部,无法从内部静态类的实例中访问外部类的非静态成员)
public static final String NOTE = TextNote.CONTENT_ITEM_TYPE; public static final String NOTE = TextNote.CONTENT_ITEM_TYPE;
public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE;
} }
@ -54,12 +54,17 @@ public class Notes {
/** /**
* Uri to query all notes and folders * Uri to query all notes and folders
*/ */
public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note"); public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note");//可以用于在应用程序中执行查询操作,以获取笔记和文件夹的数据。
/** /**
* Uri to query data * Uri to query data
*/ */
public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data");//可以用于在应用程序中执行查询操作,以获取特定类型的数据。
/**
* JavaNoteColumns
*/
//Uri统一资源标识符是用来唯一标识资源的字符串格式。在Android中Uri经常被用于访问和操作应用程序中的数据
public interface NoteColumns { public interface NoteColumns {
/** /**
@ -167,6 +172,9 @@ public class Notes {
public static final String VERSION = "version"; public static final String VERSION = "version";
} }
/**
* JavaDataColumns
*/
public interface DataColumns { public interface DataColumns {
/** /**
* The unique ID for a row * The unique ID for a row
@ -246,15 +254,15 @@ public class Notes {
* Mode to indicate the text in check list mode or not * Mode to indicate the text in check list mode or not
* <P> Type: Integer 1:check list mode 0: normal mode </P> * <P> Type: Integer 1:check list mode 0: normal mode </P>
*/ */
public static final String MODE = DATA1; public static final String MODE = DATA1; //模式
public static final int MODE_CHECK_LIST = 1; public static final int MODE_CHECK_LIST = 1; //1是检查模式0是普通模式
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note"; public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note";//MIME类型指定返回的数据类型为目录类型
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note"; public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note";//单个项类型
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note"); public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note");//标示URI访问内容
} }
public static final class CallNote implements DataColumns { public static final class CallNote implements DataColumns {
@ -268,12 +276,12 @@ public class Notes {
* Phone number for this record * Phone number for this record
* <P> Type: TEXT </P> * <P> Type: TEXT </P>
*/ */
public static final String PHONE_NUMBER = DATA3; public static final String PHONE_NUMBER = DATA3; //通话记录的日期
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note"; public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note";//电话号码
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/call_note"; public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/call_note";//用于指定返回的数据类型为单个项类型。
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note"); public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note");//表示通话记录的内容URI用于访问通话记录的内容。
} }
} }

@ -27,50 +27,57 @@ import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
public class NotesDatabaseHelper extends SQLiteOpenHelper { public class NotesDatabaseHelper extends SQLiteOpenHelper { //类的继承,前是后的子集
private static final String DB_NAME = "note.db"; private static final String DB_NAME = "note.db"; //私有变量
private static final int DB_VERSION = 4; private static final int DB_VERSION = 4; //数据库版本号
public interface TABLE { public interface TABLE { //内部接口table
public static final String NOTE = "note"; public static final String NOTE = "note"; //笔记本的名字叫note
public static final String DATA = "data"; public static final String DATA = "data"; //数据表的名字叫data
} }
private static final String TAG = "NotesDatabaseHelper"; private static final String TAG = "NotesDatabaseHelper"; //日志标签,用于在日志中标记该类的信息
private static NotesDatabaseHelper mInstance; private static NotesDatabaseHelper mInstance; //mInstance会在NotesDatabaseHelper类中作为单例模式的实现以确保在应用程序中只有一个NotesDatabaseHelper类的实例被创建并被全局范围内所使用。
/**
*
*/
private static final String CREATE_NOTE_TABLE_SQL = private static final String CREATE_NOTE_TABLE_SQL =
"CREATE TABLE " + TABLE.NOTE + "(" + "CREATE TABLE " + TABLE.NOTE + "(" +
NoteColumns.ID + " INTEGER PRIMARY KEY," + NoteColumns.ID + " INTEGER PRIMARY KEY," + //主键(保证表中的每一行都有唯一性)
NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + //父级的id
NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," + //用于标识的标识符
NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," + //背景颜色
NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + //笔记的创建时间
NoteColumns.HAS_ATTACHMENT + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.HAS_ATTACHMENT + " INTEGER NOT NULL DEFAULT 0," + //表示是否包含部件
NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + //笔记的修改时间
NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," + //子笔记的数量
NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," + NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," + //笔记的摘要或片段内容
NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," + //类型
NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," + //关联的小部件的id
NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," + NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," + //小部件的类型
NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," + //用于同步的id与远程同步服务有关
NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," + //笔记是否在本地被修改
NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + //原始父级的id
NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," + NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," + //google任务服务的id
NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" + NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" + //笔记的版本号
")"; ")";
/**
* SQLSQL
*/
private static final String CREATE_DATA_TABLE_SQL = private static final String CREATE_DATA_TABLE_SQL =
"CREATE TABLE " + TABLE.DATA + "(" + "CREATE TABLE " + TABLE.DATA + "(" +
DataColumns.ID + " INTEGER PRIMARY KEY," + DataColumns.ID + " INTEGER PRIMARY KEY," + //DataColumns.ID表示一个名为"ID"的列数据类型为INTEGER(整数),并且被定义为主键。
DataColumns.MIME_TYPE + " TEXT NOT NULL," + DataColumns.MIME_TYPE + " TEXT NOT NULL," + //版本
DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," + DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," + //笔记的名称
NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + //创建时间
NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + //上一次修改时间
DataColumns.CONTENT + " TEXT NOT NULL DEFAULT ''," + DataColumns.CONTENT + " TEXT NOT NULL DEFAULT ''," + //摘要
DataColumns.DATA1 + " INTEGER," + DataColumns.DATA1 + " INTEGER," +
DataColumns.DATA2 + " INTEGER," + DataColumns.DATA2 + " INTEGER," +
DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," + DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," +
@ -206,10 +213,18 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
" END"; " END";
/**
* cotext
* @param context
*/
public NotesDatabaseHelper(Context context) { public NotesDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION); super(context, DB_NAME, null, DB_VERSION);
} }
/**
* note
* @param db
*/
public void createNoteTable(SQLiteDatabase db) { public void createNoteTable(SQLiteDatabase db) {
db.execSQL(CREATE_NOTE_TABLE_SQL); db.execSQL(CREATE_NOTE_TABLE_SQL);
reCreateNoteTableTriggers(db); reCreateNoteTableTriggers(db);
@ -235,6 +250,10 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER); db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER);
} }
/**
*
* @param db
*/
private void createSystemFolder(SQLiteDatabase db) { private void createSystemFolder(SQLiteDatabase db) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
@ -270,6 +289,10 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
db.insert(TABLE.NOTE, null, values); db.insert(TABLE.NOTE, null, values);
} }
/**
* SQL
* @param db
*/
public void createDataTable(SQLiteDatabase db) { public void createDataTable(SQLiteDatabase db) {
db.execSQL(CREATE_DATA_TABLE_SQL); db.execSQL(CREATE_DATA_TABLE_SQL);
reCreateDataTableTriggers(db); reCreateDataTableTriggers(db);
@ -277,6 +300,10 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
Log.d(TAG, "data table has been created"); Log.d(TAG, "data table has been created");
} }
/**
*
* @param db
*/
private void reCreateDataTableTriggers(SQLiteDatabase db) { private void reCreateDataTableTriggers(SQLiteDatabase db) {
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_insert"); db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_insert");
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_update"); db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_update");
@ -287,37 +314,47 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER); db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER);
} }
static synchronized NotesDatabaseHelper getInstance(Context context) { static synchronized NotesDatabaseHelper getInstance(Context context) { //static synchronized 是Java中的修饰符用于同时指定一个方法或代码块为静态和同步。
if (mInstance == null) { if (mInstance == null) {
mInstance = new NotesDatabaseHelper(context); mInstance = new NotesDatabaseHelper(context);
} }
return mInstance; return mInstance;
} }
/**
*
* @param db
*/
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
createNoteTable(db); createNoteTable(db);
createDataTable(db); createDataTable(db);
} }
/**
*
* @param db
* @param oldVersion
* @param newVersion
*/
@Override @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //db是要升级的数据库对象oldVersion是当前数据库版本号newVersion是要升级到的新版本号。
boolean reCreateTriggers = false; boolean reCreateTriggers = false; //是否重新创建触发器和是否跳过V2版本的升级。
boolean skipV2 = false; boolean skipV2 = false;
if (oldVersion == 1) { if (oldVersion == 1) { //执行升级到V2版本的操作。
upgradeToV2(db); upgradeToV2(db);
skipV2 = true; // this upgrade including the upgrade from v2 to v3 skipV2 = true; // this upgrade including the upgrade from v2 to v3
oldVersion++; oldVersion++;
} }
if (oldVersion == 2 && !skipV2) { if (oldVersion == 2 && !skipV2) { //执行升级到V3版本的操作。
upgradeToV3(db); upgradeToV3(db);
reCreateTriggers = true; reCreateTriggers = true;
oldVersion++; oldVersion++;
} }
if (oldVersion == 3) { if (oldVersion == 3) { //执行升级到V4版本的操作。
upgradeToV4(db); upgradeToV4(db);
oldVersion++; oldVersion++;
} }
@ -327,36 +364,36 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
reCreateDataTableTriggers(db); reCreateDataTableTriggers(db);
} }
if (oldVersion != newVersion) { if (oldVersion != newVersion) { //判断升级后的数据库版本号是否与目标版本号相等。如果不相等,则抛出一个异常。
throw new IllegalStateException("Upgrade notes database to version " + newVersion throw new IllegalStateException("Upgrade notes database to version " + newVersion
+ "fails"); + "fails");
} }
} }
private void upgradeToV2(SQLiteDatabase db) { private void upgradeToV2(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE); db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE); //删除名为Note和Data的表。
db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA); db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA);
createNoteTable(db); createNoteTable(db); //创建新的表
createDataTable(db); createDataTable(db);
} }
private void upgradeToV3(SQLiteDatabase db) { private void upgradeToV3(SQLiteDatabase db) {
// drop unused triggers // drop unused triggers
db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_insert"); db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_insert"); //删除三个触发器
db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_delete"); db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_delete");
db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_update"); db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_update");
// add a column for gtask id // add a column for gtask id
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID //向Note表中添加一个名为gtask_id的列。该列的数据类型为TEXT非空并设置默认值为空字符串。
+ " TEXT NOT NULL DEFAULT ''"); + " TEXT NOT NULL DEFAULT ''");
// add a trash system folder // add a trash system folder
ContentValues values = new ContentValues(); ContentValues values = new ContentValues(); //创建一个ContentValues对象用于存储待插入到数据库的键值对。
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); //系统级别的文件夹,用于存放已经删除的笔记。
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); //表示向Note表中插入的记录类型为Notes.TYPE_SYSTEM即系统级别的记录
db.insert(TABLE.NOTE, null, values); db.insert(TABLE.NOTE, null, values);
} }
private void upgradeToV4(SQLiteDatabase db) { private void upgradeToV4(SQLiteDatabase db) {
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION //向Note表中添加一个名为version的列。该列的数据类型为INTEGER
+ " INTEGER NOT NULL DEFAULT 0"); + " INTEGER NOT NULL DEFAULT 0");
} }
} }
Loading…
Cancel
Save