在android中使用substring函数运行sql查询时出错

5lhxktic  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(444)
cursor = db.rawQuery("SELECT _id as id, amount as amt, category, date from expense where userid=? and substr(date,0,4)=? and substr(date, 5, 1) order by _id desc", new String[]{String.valueOf(id), ySelected, mSelected});

获取此查询的致命错误查询在没有substr函数的情况下运行正常。我需要在列中文本的子部分添加条件。有人能帮忙吗?
错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.smdproj, PID: 13094
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.smdproj/com.example.smdproj.viewExp}: java.lang.IllegalArgumentException: Cannot bind argument at index 3 because the index is out of range.  The statement has 2 parameters.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3408)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3547)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2080)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:264)
        at android.app.ActivityThread.main(ActivityThread.java:7581)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
     Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 3 because the index is out of range.  The statement has 2 parameters.
        at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:215)
        at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:169)
        at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:203)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1443)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1382)
        at com.example.smdproj.viewExp.onCreate(viewExp.java:171)
        at android.app.Activity.performCreate(Activity.java:7805)
        at android.app.Activity.performCreate(Activity.java:7794)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3378)
vaqhlq81

vaqhlq811#

查询中的第三个条件中缺少“=?”。

cursor = db.rawQuery("SELECT _id as id, amount as amt, category, date from expense where userid=? and substr(date,0,4)=? and substr(date, 5, 1)=? order by _id desc", new String[]{String.valueOf(id), ySelected, mSelected});

相关问题