这是使用WHERE及其参数形成SQLite查询的正确方法,因为我收到错误,索引4处的绑定值为null。
public Cursor getBankomatLocation(){
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String [] sqlSelect = {"0 _id", "Naslov", "Kraj"};
String sqlTables="Bankomati_db";
String sqlWhere = "Banka=? AND(Posta BETWEEN ? AND ? OR Kraj = ?)";
String [] args ={selectedBank, selectedPostaPod, selectedPostaNad, selectedKraj};
Log.d("getBankomatLocation", "selectedBank" + selectedBank);
qb.setTables(sqlTables);
Cursor c =qb.query(db, sqlSelect, sqlWhere, args, null, null, null);
c.moveToFirst();
return c;
}
SQL语句:SELECT Naslov,Kraj FROM Bankomati_db其中Banka = SelectedBank AND(Posta BETWEEN selectedPostaPod AND SelectedPostaNad OR kraj = selectedKraj)
错误消息:
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: FATAL EXCEPTION: main
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jenko.bojan.a2/com.jenko.bojan.a2.BankomatiActivity}: java.lang.IllegalArgumentException: the bind value at index 4 is null
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:134)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4867)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: Caused by: java.lang.IllegalArgumentException: the bind value at index 4 is null
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:400)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:294)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at com.jenko.bojan.a2.MyDatabase.getBankomatLocation(MyDatabase.java:81)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at com.jenko.bojan.a2.BankomatiActivity.Location(BankomatiActivity.java:61)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at com.jenko.bojan.a2.BankomatiActivity.onCreate(BankomatiActivity.java:47)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5047)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:134)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4867)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
01-15 13:26:19.738 2682-2682/com.jenko.bojan.a2 E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)