0

我正在使用函数 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);
}

提前感谢您对此的任何帮助!

4

1 回答 1

0

似乎问题是,我正在使用我的方法查询(字符串命令)提交查询,这是我为方法 execSQL() 创建的别名。显然,我不应该对大多数 SQL 操作使用该方法,而应该使用 rawQuery()。

于 2013-01-21T17:14:21.473 回答