SQL中的单引号

时间:2017-03-29 16:53:22

标签: android sqlite

我收到此错误消息:

03-30 00:51:50.373 30267-30267/com.example.text E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
03-30 00:51:54.273 30267-30267/com.example.text E/SQLiteLog: (1) near "hand": syntax error
03-30 00:51:54.273 30267-30267/com.example.text E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.example.text, PID: 30267
                                                                  android.database.sqlite.SQLiteException: near "hand": syntax error (code 1): , while compiling: INSERT INTO Result (Date, Name, Content) VALUES ('20170330_005134', 'Next, adjust the collar.', 'Next, adjust the collar.

                                                                  First, put your left'hand into miyatsukuchi

                                                                  and adjust the collar on the right side

                                                                  so It lies flat. Then, adjust the left collar

                                                                  so it lies flat. There should be a fist-sized space between

                                                                  the back of your neck and the collar.');
                                                                      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
                                                                      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
                                                                      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
                                                                      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                      at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1798)
                                                                      at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1729)
                                                                      at seeto.fyp.textcamera.ResultHelper.addResult(ResultHelper.java:101)
                                                                      at seeto.fyp.textcamera.ResultActivity$2.onClick(ResultActivity.java:74)
                                                                      at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:145)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5938)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

我知道文本中的单引号出现问题,而在某些情况下我需要单引号(或双引号)。

我尝试了content = content.replace("'", "\'");,但似乎无法正常工作。

如何使用单引号和双引号将数据插入数据库?

public void addResult(String name, String date, String content) {
    database.execSQL("INSERT INTO " + tbName + " (" + keyDate + ", " + keyName + ", " + keyContent + ") VALUES ('" + date + "', '" + name + "', '" + content + "');");
}

2 个答案:

答案 0 :(得分:2)

在SQL中,单引号必须通过加倍来转义。 会有helper function来执行此操作,但只使用参数更容易:

public void addResult(String name, String date, String content) {
    database.execSQL("INSERT INTO " + tbName +
                     " (" + keyDate + ", " + keyName + ", " + keyContent + ")" +
                     " VALUES (?, ?, ?);",
                     new Object[]{ date, name, content });
}

更容易使用insert(),它为您创建SQL命令:

public void addResult(String name, String date, String content) {
    ContentValues cv = new ContentValues();
    cv.put(keyDate, date);
    cv.put(keyName, name);
    cv.put(keyContent, content);
    database.insertOrThrow(tbName, null, cv);
}

答案 1 :(得分:-2)

通常情况下,您可以通过将单引号加倍并通过插入反斜杠来双引号来转义单引号。

所以,使用你的例子:

content = content.replace("'", "''")
content = content.replace("\"", "\\\"")