cursor query为where子句和/或where子句提供语法错误

时间:2020-11-28 23:11:10

I get an SQLite exception in log cat stating that there is a syntax error near "ANDcontraYesOrNo".

我在log cat中得到一个SQLite异常,声明“ANDcontraYesOrNo”附近存在语法错误。

Cursor c = db.query( VivzHelper.TABLE_NAME, columns, helper.TX_IDT+"     
='"+name+"' AND" +helper.CONTRA_YES_OR_NO+ "='"+contraY+"' 
OR"+helper.CONTRA_YES_OR_NO+"='"+contraN+"'" +"",null,null, null, NAME + " 
ASC");

Logcat:

 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.database.sqlite.SQLiteException: near "ANDcontraYesOrNo":     
    syntax error (code 1): , while compiling: SELECT _id, name FROM outtable 
    WHERE txidt ='I' ANDcontraYesOrNo='Y' ORcontraYesOrNo='N' ORDER BY name ASC
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native 
    Method)
   at                        android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnect    ion.java:893)
    at      
    android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)

1 个解决方案

#1


Add whitspace after AND and OR cause

在AND和OR之后添加whitspace

Cursor c = db.query( VivzHelper.TABLE_NAME, columns, helper.TX_IDT+"     
='"+name+"' AND " +helper.CONTRA_YES_OR_NO+ "='"+contraY+"' 
OR "+helper.CONTRA_YES_OR_NO+"='"+contraN+"'" +"",null,null, null, NAME + " 
ASC");

#1


Add whitspace after AND and OR cause

在AND和OR之后添加whitspace

Cursor c = db.query( VivzHelper.TABLE_NAME, columns, helper.TX_IDT+"     
='"+name+"' AND " +helper.CONTRA_YES_OR_NO+ "='"+contraY+"' 
OR "+helper.CONTRA_YES_OR_NO+"='"+contraN+"'" +"",null,null, null, NAME + " 
ASC");