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

时间:2015-05-07 10:30:40

标签: android sqlite

我在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 个答案:

答案 0 :(得分:2)

ANDOR导致

之后添加空格
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");