【问题标题】:How to execute a query with IF and ELSE in Android sqlite?如何在 Android sqlite 中使用 IF 和 ELSE 执行查询?
【发布时间】:2013-06-23 14:06:43
【问题描述】:

我正在我的 android 应用程序中编写方法 insertOrUpdate,但我不能这样做:

database.execSQL('IF (select count(*) from RegNascimento where codigoAnimal = ?) > 0 then begin update RegNascimento set cgnRegNascimento = ? where codigoAnimal = ?; end else begin insert into RegNascimento(cgnRegNascimento, dataInspecaoRegNascimento) values (?,?); end;');

我收到此错误:

06-26 09:24:58.835: E/SQLiteLog(3924): (1) near "if": syntax error
06-26 09:24:58.835: W/System.err(3924): android.database.sqlite.SQLiteException: near "if": syntax error (code 1): , while compiling: if (select count(*) from RegDefinitivo where codigoAnimal = ?) > 0 then begin update RegDefinitivo set cgdRegDefinitivo = ?, seloRegDefinitivo = ? where codigoAnimal = ?; end else begin insert into RegDefinitivo(cgdRegDefinitivo, seloRegDefinitivo, dataInspecaoRegDefinitivo) values (?,?,?); end;
06-26 09:24:58.835: W/System.err(3924):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-26 09:24:58.835: W/System.err(3924):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1013)
06-26 09:24:58.835: W/System.err(3924):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:624)
06-26 09:24:58.835: W/System.err(3924):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-26 09:24:58.835: W/System.err(3924):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-26 09:24:58.835: W/System.err(3924):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
06-26 09:24:58.835: W/System.err(3924):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
06-26 09:24:58.835: W/System.err(3924):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
06-26 09:24:58.835: W/System.err(3924):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
06-26 09:24:58.840: W/System.err(3924):     at org.apache.cordova.Storage.executeSql(Storage.java:173)
06-26 09:24:58.840: W/System.err(3924):     at org.apache.cordova.Storage.execute(Storage.java:83)
06-26 09:24:58.840: W/System.err(3924):     at org.apache.cordova.api.CordovaPlugin.execute(CordovaPlugin.java:66)
06-26 09:24:58.840: W/System.err(3924):     at org.apache.cordova.api.PluginManager.exec(PluginManager.java:224)
06-26 09:24:58.840: W/System.err(3924):     at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:51)
06-26 09:24:58.840: W/System.err(3924):     at android.webkit.JWebCoreJavaBridge.sharedTimerFired(Native Method)
06-26 09:24:58.840: W/System.err(3924):     at android.webkit.JWebCoreJavaBridge.sharedTimerFired(Native Method)
06-26 09:24:58.840: W/System.err(3924):     at android.webkit.JWebCoreJavaBridge.fireSharedTimer(JWebCoreJavaBridge.java:92)
06-26 09:24:58.840: W/System.err(3924):     at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:108)
06-26 09:24:58.840: W/System.err(3924):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 09:24:58.840: W/System.err(3924):     at android.os.Looper.loop(Looper.java:137)
06-26 09:24:58.840: W/System.err(3924):     at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:1064)
06-26 09:24:58.840: W/System.err(3924):     at java.lang.Thread.run(Thread.java:856)

PS:我正在使用 sencha touch,但 sql 正在通过插件在 android 中执行。 谢谢

【问题讨论】:

  • 您没有在插入中为codigoAnimal 设置值,因此下次运行时不会重复。这是故意的吗?

标签: android sql sqlite if-statement conditional-statements


【解决方案1】:

【讨论】:

    【解决方案2】:

    您可以尝试使用 CASE 表达式。引用the SQLite documentation:

    CASE 表达式的作用类似于其他编程语言中的 IF-THEN-ELSE。

    否则,您可以使用 JavaScript 中的 if/else 逻辑将其作为三个单独的 SQLite 操作来执行。请记住,SQLite 是本地的,因此每个操作没有网络往返开销,因此在 JavaScript 中执行 if/else 与在数据库中执行的成本应该不会太大。

    【讨论】:

    • CASE 表达式在这种情况下没有帮助。问题中的代码不使用 JavaScript。
    • @CL:“CASE 表达式在这种情况下没有帮助”——请考虑解释 CASE 的问题所在。 “问题中的代码不使用 JavaScript”——根据它所做的 OP,因为 OP 正在通过 Apache Cordova 使用 Sencha Touch。
    【解决方案3】:

    SQLite does not have IF 语句。

    你可以试试UPDATE,如果没有实际更新记录,执行INSERT

    ContentValues cv = new ContentValues();
    cv.put("cgnRegNascimento", ...);
    if (db.update("RegNascimento", cv, "codigoAnimal = ?", new String[] { ... }) < 1) {
        cv.put("dataInspecaoRegNascimento", ...);
        long newID = db.insert("RegNascimento", null, cv);
    }
    

    或者,如果您在 codigoAnimal 列上有唯一索引,则可以使用 INSERT OR REPLACE 语句:

    ContentValues cv = new ContentValues();
    cv.put("codigoAnimal", ...);
    cv.put("cgnRegNascimento", ...);
    cv.put("dataInspecaoRegNascimento", ...);
    db.insertWithOnConflict("RegNascimento", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
    

    【讨论】:

    • OP 是用 JavaScript 而不是 Java 编写的,因此无法直接访问 ContentValues
    猜你喜欢
    • 2022-01-17
    • 2012-11-02
    • 2021-01-02
    • 2010-11-17
    • 2015-11-27
    • 2017-11-30
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多