関数 sqlEscapeString(String param) を使用して、Android アプリのデータベースに挿入する文字列内の特殊文字をエスケープしています。私はこれをいくつかのデータセットで行っていますが、特定のセットで問題が発生し続けています。保存しようとしている文字列は、Gson ライブラリで解析された JSON 文字列から取得されます。ここで読んでいるタイプのサンプル文字列は次のようになります。
{"group":"Adult 5","classes":[{"title":"Coed 1","teachers":"Hollis / Maxwell","callnum":"551","room":"B-213"},{"title":"Coed 2","teachers":"Thweatt","callnum":"552","room":"B-202"},{"title":"Coed 3","teachers":"Davis","callnum":"553","room":"E-102"}]}
ログ:
01-19 20:40:49.197: E/AndroidRuntime(1429): FATAL EXCEPTION: AsyncTask #1
01-19 20:40:49.197: E/AndroidRuntime(1429): java.lang.RuntimeException: An error occured while executing doInBackground()
01-19 20:40:49.197: E/AndroidRuntime(1429): at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-19 20:40:49.197: E/AndroidRuntime(1429): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-19 20:40:49.197: E/AndroidRuntime(1429): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-19 20:40:49.197: E/AndroidRuntime(1429): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-19 20:40:49.197: E/AndroidRuntime(1429): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-19 20:40:49.197: E/AndroidRuntime(1429): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
01-19 20:40:49.197: E/AndroidRuntime(1429): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
01-19 20:40:49.197: E/AndroidRuntime(1429): at java.lang.Thread.run(Thread.java:1096)
01-19 20:40:49.197: E/AndroidRuntime(1429): Caused by: java.lang.NullPointerException
01-19 20:40:49.197: E/AndroidRuntime(1429): at android.database.DatabaseUtils.appendEscapedSQLString(DatabaseUtils.java:216)
01-19 20:40:49.197: E/AndroidRuntime(1429): at android.database.DatabaseUtils.sqlEscapeString(DatabaseUtils.java:236)
01-19 20:40:49.197: E/AndroidRuntime(1429): at com.lakesidebaptist.lakesidelife.update.Updater.sant(Updater.java:104)
01-19 20:40:49.197: E/AndroidRuntime(1429): at com.lakesidebaptist.lakesidelife.update.Updater.sundaySchool(Updater.java:321)
01-19 20:40:49.197: E/AndroidRuntime(1429): at com.lakesidebaptist.lakesidelife.update.Updater.dataTypeExecutor(Updater.java:79)
01-19 20:40:49.197: E/AndroidRuntime(1429): at com.lakesidebaptist.lakesidelife.update.Updater.updateAll(Updater.java:62)
01-19 20:40:49.197: E/AndroidRuntime(1429): at com.lakesidebaptist.lakesidelife.update.UpdaterTask.doInBackground(UpdaterTask.java:12)
01-19 20:40:49.197: E/AndroidRuntime(1429): at com.lakesidebaptist.lakesidelife.update.UpdaterTask.doInBackground(UpdaterTask.java:1)
01-19 20:40:49.197: E/AndroidRuntime(1429): at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-19 20:40:49.197: E/AndroidRuntime(1429): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-19 20:40:49.197: E/AndroidRuntime(1429): ... 4 more
日曜学校():
private static void sundaySchool() {
List<SundaySchoolGroups> ssgList = Fetcher.sundaySchool();
Iterator<SundaySchoolGroups> ssgIt = ssgList.iterator();
dba.query("DROP TABLE IF EXISTS 'sunday_school';");
dba.query("CREATE TABLE 'sunday_school' ('id' integer primary key autoincrement,"
+ "'group' text, 'title' text, 'teachers' text, 'room' text);");
while (ssgIt.hasNext()) {
SundaySchoolGroups ssg = ssgIt.next();
List<SundaySchoolClasses> sscList = ssg.classes;
Iterator<SundaySchoolClasses> sscIt = sscList.iterator();
while (sscIt.hasNext()) {
SundaySchoolClasses ssc = sscIt.next();
String ssQuery = "INSERT INTO 'sunday_school' ('group', 'title', 'teachers', 'room')"
+ " VALUES ("
+ sant(ssg.group)
+ ", "
+ sant(ssc.title)
+ ", "
+ sant(ssc.teachers)
+ ", " + sant(ssc.room) + ")";
dba.query(ssQuery);
}
}
updateStatus[DataType.SUNDAY_SCHOOL.ordinal()] = true;
}
サント():
/**
* This method acts as an alias for the sqlEscapeString(str) method in
* DatabaseUtils.
*
* @param str
* @return
*/
private static String sant(String str) {
return DatabaseUtils.sqlEscapeString(str);
}
これについて何か助けてくれてありがとう!