我收到此错误,当尝试在查询下执行查询时 - 我也从其他java类传递值(uname,pass)。我试图在表中搜索两个值的数据,即我使用两个参数来搜索数据。
public boolean validate(String uname, String pass) {
// TODO Auto-generated method stub
// SELECT
String[] columns = { "_id" };
// WHERE clause
String selection = "user_name = ? AND _password";
// WHERE clause arguments
String[] selectionArgs = { uname, pass };
Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns, selection,
selectionArgs, null, null, null);
try {
if (cursor != null && cursor.getCount() > 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
另外,请找到下面的logcat -
01-04 01:08:10.582: E/AndroidRuntime(1180): FATAL EXCEPTION: main
01-04 01:08:10.582: E/AndroidRuntime(1180): android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x2a0b88
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.database.sqlite.SQLiteProgram.native_bind_string(Native Method)
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:241)
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.database.sqlite.SQLiteQuery.bindString(SQLiteQuery.java:182)
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:48)
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
01-04 01:08:10.582: E/AndroidRuntime(1180): at com.example.retailapp.Database.validate(Database.java:139)
01-04 01:08:10.582: E/AndroidRuntime(1180): at com.example.retailapp.User.onClick(User.java:63)
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.view.View.performClick(View.java:2408)
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.view.View$PerformClick.run(View.java:8816)
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.os.Handler.handleCallback(Handler.java:587)
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.os.Handler.dispatchMessage(Handler.java:92)
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.os.Looper.loop(Looper.java:123)
01-04 01:08:10.582: E/AndroidRuntime(1180): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-04 01:08:10.582: E/AndroidRuntime(1180): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 01:08:10.582: E/AndroidRuntime(1180): at java.lang.reflect.Method.invoke(Method.java:521)
01-04 01:08:10.582: E/AndroidRuntime(1180): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-04 01:08:10.582: E/AndroidRuntime(1180): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-04 01:08:10.582: E/AndroidRuntime(1180): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
您提供了2个参数(uname,pass),但只指定了一个占位符("?")。
你可能想要这个:
// WHERE clause
String selection = "user_name = ? AND _password = ?";