我正在努力解决这个问题。任何人都可以帮我解决这个问题。请在查询语句的where子句中告诉我出错的地方。我想从所有列中读取值,然后将它们显示出来。
这是我的查询代码:
public Task getTask1(String id) {
db = helper.getReadableDatabase();
String selection = SQLiteHelper.UID;
String[] columns = { SQLiteHelper.UID,
SQLiteHelper.TASK_NAME, SQLiteHelper.CONTACTS, SQLiteHelper.DESCRIPTION, SQLiteHelper.REMARKS, SQLiteHelper.DATETIME, SQLiteHelper.ESTIMATED_COMPLETION_TIME,
SQLiteHelper.ACTUAL_COMPLETION_TIME, SQLiteHelper.NOTIFY_TIME};
String[] selectionArgs = new String[]{String.valueOf(id)};
Cursor cursor = db.query(
SQLiteHelper.TABLE_NAME,
columns,
selection,
selectionArgs,
null,
null,
null
);
if (cursor != null) {
cursor.moveToNext();
task = new Task(
cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5),
cursor.getString(6), cursor. getString(7), cursor.getString(8));
Log.d("Pana", "The value of Task is" + task);
}
return task;
}
根据建议更新了方法:
public Task getTask1(String id) {
db = helper.getReadableDatabase();
String selection = SQLiteHelper.UID;
String[] columns = { SQLiteHelper.UID,
SQLiteHelper.TASK_NAME, SQLiteHelper.CONTACTS, SQLiteHelper.DESCRIPTION, SQLiteHelper.REMARKS, SQLiteHelper.DATETIME, SQLiteHelper.ESTIMATED_COMPLETION_TIME,
SQLiteHelper.ACTUAL_COMPLETION_TIME, SQLiteHelper.NOTIFY_TIME};
String[] selectionArgs = new String[]{SQLiteHelper.TABLE_NAME, String.valueOf(id)};
Cursor cursor = db.rawQuery("select * from " + SQLiteHelper.TABLE_NAME + " where _id = ?", selectionArgs);
// if (cursor != null && cursor.moveToFirst()) {
//
// task = new Task(
// cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5),
// cursor.getString(6), cursor. getString(7), cursor.getString(8));
//
// Log.d("Pana", "The value of Task is" + task);
//
// cursor.close();
//Don't forget to close your cursor, otherwise you will have a memory leak!
if (cursor != null) {
while (cursor.moveToNext()) {
task = new Task(
cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5),
cursor.getString(6), cursor. getString(7), cursor.getString(8));
Log.d("Pana", "The value of Task is" + task);
}
cursor.close();
}
return task;
}
这是我的logcat:
11:23:06.955 1142-1142/com.ms.t.tms D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
02-27 11:23:06.970 1142-1142/com.ms.t.tms E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ms.t.tms, PID: 1142
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ms.t.tms/com.ms.t.tms.EditScreen}: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.
at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212)
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166)
at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at com.ms.t.tms.SQLiteDataBaseAdapter.getTask1(SQLiteDataBaseAdapter.java:143)
at com.ms.t.tms.EditScreen.onCreate(EditScreen.java:70)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我在一行中共有9列,包括_id作为我尝试根据_id查询的第一列,我最终得到了这个错误。请帮我。我能够插入记录,将记录显示为列表,读取插入记录的详细信息,但是当我尝试编辑这些记录时,我遇到了这个问题。当我将null传递给所有参数时,我总是得到第一行表。请提供建议。如果需要更多信息,请与我们联系。提前谢谢。
答案 0 :(得分:2)
您的selection
参数应该像
String selection = SQLiteHelper.UID + " = ?";
需要?
来定义占位符,其中SQLite将绑定selectionArgs
的值。
您已从这里提供的答案中混合并匹配您的代码选择代码段。那不行。将整个方法更改为
public Task getTask1(String id) {
db = helper.getReadableDatabase();
String[] columns = { SQLiteHelper.UID, SQLiteHelper.TASK_NAME,
SQLiteHelper.CONTACTS, SQLiteHelper.DESCRIPTION,
SQLiteHelper.REMARKS, SQLiteHelper.DATETIME,
SQLiteHelper.ESTIMATED_COMPLETION_TIME,
SQLiteHelper.ACTUAL_COMPLETION_TIME, SQLiteHelper.NOTIFY_TIME };
String selection = SQLiteHelper.UID + " = ?"; // MISSING in your update!!
String[] selectionArgs = new String[] { id };
Cursor cursor = db.query(SQLiteHelper.TABLE_NAME, columns, selection,
selectionArgs, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
task = new Task(cursor.getString(1), cursor.getString(2),
cursor.getString(3), cursor.getString(4),
cursor.getString(5), cursor.getString(6),
cursor.getString(7), cursor.getString(8));
Log.d("Pana", "The value of Task is" + task);
cursor.close();
}
return task;
}
答案 1 :(得分:1)
这可能会有所帮助:
public Task getTask1(String id) {
db = helper.getReadableDatabase();
String selection = SQLiteHelper.UID;
String[] columns = { SQLiteHelper.UID,
SQLiteHelper.TASK_NAME, SQLiteHelper.CONTACTS, SQLiteHelper.DESCRIPTION, SQLiteHelper.REMARKS, SQLiteHelper.DATETIME, SQLiteHelper.ESTIMATED_COMPLETION_TIME,
SQLiteHelper.ACTUAL_COMPLETION_TIME, SQLiteHelper.NOTIFY_TIME};
String[] selectionArgs = new String[]{id};
Cursor cursor = db.rawQuery("select * from " + SQLiteHelper.TABLE_NAME + " where _id = ?", selectionArgs);
if (cursor != null && cursor.moveToFirst()) {
task = new Task(
cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5),
cursor.getString(6), cursor. getString(7), cursor.getString(8));
Log.d("Pana", "The value of Task is" + task);
cursor.close(); //Don't forget to close your cursor, otherwise you will have a memory leak!
}
return task;
}