2013-06-26 78 views
0

我在我的Android应用程序的方法insertOrUpdate,我不能做到这一点:如何在Android sqlite中使用IF和ELSE执行查询?

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:我使用煎茶触摸,但是sql通过插件在android中执行。 谢谢

+0

您未在插入中为'codigoAnimal'设置值,因此下次运行时不会有重复项。那是故意的吗? –

回答

0

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

A CASE expression serves a role similar to IF-THEN-ELSE in other programming languages.

否则,你可以做到这一点作为三个独立的SQLite的操作,在JavaScript中的if/else逻辑。请记住,SQLite是本地的,因此每个操作都没有网络往返开销,所以在JavaScript中执行if/else与在数据库中执行的代价不应该很大。

+0

'CASE'表达式在这种情况下不起作用。问题中的代码不使用JavaScript。 –

+0

@CL:“CASE表达式在这种情况下不起作用” - 请考虑解释“CASE”的问题。 “问题中的代码不使用JavaScript” - 根据OP的说法,因为OP通过Apache Cordova使用Sencha Touch。 – CommonsWare

0

SQLite does not haveIF声明。

你可以只尝试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); 
+0

OP使用JavaScript编写,而不是Java,因此不能直接访问'ContentValues'。 – CommonsWare