我想在listview中显示SQLite数据库中所有列的平均值。我在查询中使用了普通函数和group by子句,但是当我运行应用程序时,我的应用程序崩溃并获得 IllegalArgumentException ,并且绑定值索引1为空。
我不知道我的错误在哪里,即使我没有在SQLite db.SO中插入任何空值也可以帮助我一些。提前谢谢。
这是我的代码:
private void showPerformanceDetails()
{
ArrayList<Performance_Pojo> Performance_PojoList = new ArrayList<Performance_Pojo>();
Performance_PojoList.clear();
SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();
Cursor cursor = sqlDatabase.rawQuery("SELECT performance_month, AVG(performance_rate_one), AVG(performance_rate_two), AVG(performance_rate_three), AVG(performance_rate_four), AVG(performance_rate_five) FROM performance where "+ "Emp_id" + " = ? "
+" GROUP BY performance_month",new String[]{strSeparated_Id});
if (cursor != null && cursor.getCount() != 0)
{
if (cursor.moveToFirst())
{
do
{
Performance_Pojo Performance_PojoListItems = new Performance_Pojo();
Performance_PojoListItems.set_strPerformanceMonth(cursor.getString(cursor.getColumnIndex("performance_month")));
Performance_PojoListItems.set_strPerformance_rate_one(cursor.getString(cursor.getColumnIndex("performance_rate_one")));
Performance_PojoListItems.set_strPerformance_rate_two(cursor.getString(cursor.getColumnIndex("performance_rate_two")));
Performance_PojoListItems.set_strPerformance_rate_three(cursor.getString(cursor.getColumnIndex("performance_rate_three")));
Performance_PojoListItems.set_strPerformance_rate_four(cursor.getString(cursor.getColumnIndex("performance_rate_four")));
Performance_PojoListItems.set_strPerformance_rate_five(cursor.getString(cursor.getColumnIndex("performance_rate_five")));
Performance_PojoList.add(Performance_PojoListItems);
}while (cursor.moveToNext());
}
db.close();
cursor.close();
}
PerformanceList_Adapter performanceList_Adapter = new PerformanceList_Adapter(Performance_Details.this, Performance_PojoList);
list_PerformanceDetails.setAdapter(performanceList_Adapter);
}
这是我的Log cat错误:
07-01 12:15:15.366: E/AndroidRuntime(14850): FATAL EXCEPTION: main
07-01 12:15:15.366: E/AndroidRuntime(14850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sqlitedemo/com.sqlitedemo.Performance_Details}: java.lang.IllegalArgumentException: the bind value at index 1 is null
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.os.Looper.loop(Looper.java:123)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-01 12:15:15.366: E/AndroidRuntime(14850): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 12:15:15.366: E/AndroidRuntime(14850): at java.lang.reflect.Method.invoke(Method.java:507)
07-01 12:15:15.366: E/AndroidRuntime(14850): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-01 12:15:15.366: E/AndroidRuntime(14850): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-01 12:15:15.366: E/AndroidRuntime(14850): at dalvik.system.NativeStart.main(Native Method)
07-01 12:15:15.366: E/AndroidRuntime(14850): Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:237)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteQuery.bindString(SQLiteQuery.java:185)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:48)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
07-01 12:15:15.366: E/AndroidRuntime(14850): at com.sqlitedemo.Performance_Details.showPerformanceDetails(Performance_Details.java:79)
07-01 12:15:15.366: E/AndroidRuntime(14850): at com.sqlitedemo.Performance_Details.onCreate(Performance_Details.java:63)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
答案 0 :(得分:1)
&#34;绑定值&#34;显然是指选择的SelectionArgs,你选择进入rawQuery()。如果这样的selArgs值为null,那么就可以得到它。
编辑:
Cursor cursor = sqlDatabase.rawQuery("SELECT performance_month, AVG(performance_rate_one), AVG(performance_rate_two), AVG(performance_rate_three), AVG(performance_rate_four), AVG(performance_rate_five) FROM performance where "+ "Emp_id" + " = ? "
+" GROUP BY performance_month",new String[]{strSeparated_Id});
所有参数位置都应指定为?。查询的参数总是字符串,所以整数没有什么特别之处。这应该适合你:
变化:
new String[]{strSeparated_Id}
到
new String[]{String.valueOf(strSeparated_Id)}