2012-05-08 98 views
0

Iam嘗試從我的android應用程序中的Sqlite數據庫返回數據(存儲的練習信息:名稱,句號,由用戶在特定日期播放的卡路里)發生的是:編譯時出現語法錯誤:Sqlite中的SELECT語句

android.database.sqlite.SQLiteException: near ",": syntax error: , while compiling: SELECT Name, Period, Burned_Calories FROM Exercise WHERE Date = Tuesday, 8/May/2012 

爲什麼會發生這種異常,我該如何解決它?請幫助我..

這是我的代碼:

DBAdapter db = new DBAdapter(this); 
db.open(); 
String selected_date= date_btn.getText().toString(); 

Cursor c = db.getExerciseInfo(selected_date); 

if (c.moveToFirst()) 
{ 
    do 
    { 
    Toast.makeText(this ,"Name: "+ c.getString(0) + "\n" + "Period:" +c.getFloat(1)+ "\n" + " burned cals:" +c.getFloat(2)+ "\n" ,Toast.LENGTH_LONG).show(); 
    } 
    while (c.moveToNext()); 

    } 
    db.close(); 

這是我在將對DBAdapter CALSS方法

public Cursor getExerciseInfo(String date) throws SQLException 
     { 
     Cursor C_excer = db.query(EXERCISE_TABLE_NAME, new String[]  {KEY_NAME,KEY_PERIOD,KEY_BURNEDCALS},KEY_DATE +" = " + date, null, null, null, null); 
      if (C_excer != null) { 
     C_excer.moveToFirst(); 
      } 
     return C_excer; 
      } 

回答

0

嘗試包裝你周圍的最新報價: '' 。像這樣:

Cursor C_excer = db.query(EXERCISE_TABLE_NAME, new String[] { KEY_NAME,KEY_PERIOD, KEY_BURNEDCALS}, KEY_DATE + " = '" + date + "'", null, null, null, null); 
+0

謝謝,但是當我嘗試它時,發生錯誤是無效的字符常量,爲什麼呢? – user

+0

@ user88請發佈整個新的錯誤消息。 –

+0

無效的字符常量顯示爲錯誤消息,不在日誌中,因爲我不能運行我的應用程序,因爲這個錯誤「當我嘗試運行我的應用程序它說你的應用程序包含錯誤修復它們」,我不知道如何解釋這種類型的錯誤,你喲瞭解我嗎? – user

0

這是一種艱難的從你張貼怎麼說,但你的where子句看起來很可疑:

WHERE Date = Tuesday, 8/May/2012 -- boom! 

嘗試改變這樣的:

WHERE Date = 'Tuesday, 8/May/2012' 
0

不專家,但也許你需要一些報價:

android.database.sqlite.SQLiteException: near ",": syntax error: , while compiling: SELECT Name, Period, Burned_Calories FROM Exercise WHERE Date = "Tuesday, 8/May/2012"