0

我正在我的 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中执行的。谢谢

4

3 回答 3

0

这三个问题可能会帮助您找出语法问题。

SQLite 中的 IF 语句:更新还是插入?

SQLite 中的 IF() 语句替代

sqlite 是否支持 select 中的任何类型的 IF(condition) 语句

希望这可以帮助

于 2013-06-26T12:35:20.500 回答
0

您可以尝试使用 CASE 表达式。引用SQLite 文档

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

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

于 2013-06-26T12:35:43.080 回答
0

SQLite没有语句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);
于 2013-06-26T12:36:38.840 回答