编译时出现语法错误:来自Sqlite的SELECT语句

时间:2012-05-08 16:22:40

标签: android sqlite

Iam尝试在我的Android应用程序中从Sqlite数据库返回数据(存储的练习信息:用户在特定日期播放的名称,句点,燃烧的卡路里),但发生的异常是:

 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;
             }

3 个答案:

答案 0 :(得分:0)

尝试将日期换成引号:''。像这样:

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

答案 1 :(得分:0)

从您发布的内容中可以很难说出来,但您的where子句看起来很可疑:

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

尝试更改为:

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

答案 2 :(得分:0)

不是专家,但也许你需要一些报价:

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