我想以字符串格式从SQLite中检索数据,该格式用于评级栏。但我得到RunTimeException和语法错误,我的应用程序崩溃。有人请帮助我。谢谢。提前谢谢。
以下是我的Log Cat信息:
07-25 15:51:06.386: E/AndroidRuntime(28493): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sqlitedemo/com.sqlitedemo.Rating_Details}: android.database.sqlite.SQLiteException: near "Two": syntax error: , while compiling: SELECT performance_rate_one, performance_rate_two, performance_rate_three, performance_rate_four, performance_rate_five FROM performance WHERE task_name =Task Two
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.os.Looper.loop(Looper.java:123)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-25 15:51:06.386: E/AndroidRuntime(28493): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 15:51:06.386: E/AndroidRuntime(28493): at java.lang.reflect.Method.invoke(Method.java:507)
07-25 15:51:06.386: E/AndroidRuntime(28493): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-25 15:51:06.386: E/AndroidRuntime(28493): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-25 15:51:06.386: E/AndroidRuntime(28493): at dalvik.system.NativeStart.main(Native Method)
07-25 15:51:06.386: E/AndroidRuntime(28493): Caused by: android.database.sqlite.SQLiteException: near "Two": syntax error: , while compiling: SELECT performance_rate_one, performance_rate_two, performance_rate_three, performance_rate_four, performance_rate_five FROM performance WHERE task_name =Task Two
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
07-25 15:51:06.386: E/AndroidRuntime(28493): at com.sqlitedemo.Rating_Details.showPerformanceRate(Rating_Details.java:63)
07-25 15:51:06.386: E/AndroidRuntime(28493): at com.sqlitedemo.Rating_Details.onCreate(Rating_Details.java:48)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-25 15:51:06.386: E/AndroidRuntime(28493): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
这是我的活动代码:
public class Rating_Details extends Activity
{
String emp_name;
String task_name;
String task_date;
TextView tvEmpNAme;
TextView tvTaskName;
TextView tvTaskDate;
SQLiteDatabase db;
DatabaseHelper databaseHelper;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.rating_details);
Intent intent = getIntent();
emp_name = intent.getExtras().getString("EMPNAME");
task_name = intent.getExtras().getString("TASKNAME");
task_date = intent.getExtras().getString("DATE");
System.out.println("____________________________________________________");
Log.e("emp_name in Rating_Details "," = " + emp_name);
Log.e("task_name in Rating_Details "," = " + task_name);
Log.e("task_date in Rating_Details "," = " + task_date);
tvEmpNAme = (TextView)findViewById(R.id.tv_empName);
tvTaskName = (TextView)findViewById(R.id.tv_taskName);
tvTaskDate = (TextView)findViewById(R.id.tv_taskDate);
System.out.println("____________________________________________________");
tvEmpNAme.setText(emp_name);
tvTaskName.setText(task_name);
tvTaskDate.setText(task_date);
showPerformanceRate();
}
private void showPerformanceRate()
{
DatabaseHelper databaseHelper = new DatabaseHelper(this.getApplicationContext());
db = databaseHelper.getWritableDatabase();
System.out.println("__________________________________________________________________________________");
String query = "SELECT performance_rate_one, performance_rate_two, performance_rate_three, performance_rate_four, performance_rate_five FROM performance WHERE task_name =" +task_name;
Cursor cursor = db.rawQuery(query,null);
if (cursor != null && cursor.getCount() != 0)
{
if (cursor.moveToFirst())
{
do
{
String strRate1 = cursor.getString(cursor.getColumnIndex("performance_rate_one"));
String strRate2 = cursor.getString(cursor.getColumnIndex("performance_rate_two"));
String strRate3 = cursor.getString(cursor.getColumnIndex("performance_rate_three"));
String strRate4 = cursor.getString(cursor.getColumnIndex("performance_rate_four"));
String strRate5 = cursor.getString(cursor.getColumnIndex("performance_rate_five"));
System.out.println("____________________________________________________");
Log.e("strRate1 "," is "+strRate1);
Log.e("strRate2 "," is "+strRate2);
Log.e("strRate3 "," is "+strRate3);
Log.e("strRate4 "," is "+strRate4);
Log.e("strRate5 "," is "+strRate5);
}while (cursor.moveToNext());
}
cursor.close();
}db.close();
}
}
答案 0 :(得分:2)
你应该这样写:
String query = "SELECT performance_rate_one, performance_rate_two, performance_rate_three, performance_rate_four, performance_rate_five FROM performance WHERE task_name =?";
Cursor cursor = db.rawQuery(query,new String[]{task_name});
答案 1 :(得分:1)
更改您的字符串查询: -
String query = "SELECT performance_rate_one, performance_rate_two, performance_rate_three, performance_rate_four, performance_rate_five FROM performance WHERE task_name =" +task_name;
到
String query = "SELECT performance_rate_one, performance_rate_two, performance_rate_three, performance_rate_four, performance_rate_five FROM performance WHERE task_name ='" +task_name+"'";