使用" SELECT * FROM"时出错

时间:2014-04-28 09:10:28

标签: android android-sqlite

    android.database.sqlite.SQLiteException: near "SELECT ": syntax error (code 1): , while compiling: SELECT  * FROM stats

我搜遍了所有的stackoverflow,上面的错误的大多数解决方案是由于在放置查询时没有放入空格。我一遍又一遍地检查,找不到间距的问题,我正在使用的其他命令似乎工作正常。表创建成功,我可以插入数据(并使用id检索它),但我无法选择所有行。这是我的数据库代码:

public class Statdb extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "fat_tracker";
private static final String TABLE_NAME = "stats";

private static final String KEY_ID = "id";
private static final String KEY_BF = "bf";
private static final String KEY_WEIGHT = "weight";
private static final String KEY_DATE = "stat_date";

public Statdb(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_BF + " TEXT,"
            + KEY_WEIGHT + " TEXT," + KEY_DATE  +" TEXT);";
    db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

    // Create tables again
    onCreate(db);
}

public int addStat(Stat stat){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_BF, String.valueOf(stat.getBody_fat()));
    values.put(KEY_WEIGHT,String.valueOf(stat.getWeight()));
    values.put(KEY_DATE, stat.getDbDate());
    int id  = (int) db.insert(TABLE_NAME,null,values);
    db.close();
    return id;

}

public Stat getStat(int id){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, new String[] {KEY_ID,
            KEY_BF,KEY_WEIGHT,KEY_DATE}, KEY_ID + "=?",
            new String[] {String.valueOf(id)},null,null,null,null );
    if (cursor != null)
        cursor.moveToFirst();

    String date = cursor.getString(3);
    String[] date_tokens = date.split("-");
    int year = Integer.valueOf(date_tokens[0]);
    int month =  Integer.valueOf(date_tokens[1]);
    int day = Integer.valueOf(date_tokens[2]);

    double bf = Double.valueOf(cursor.getString(1));
    double wt = Double.valueOf(cursor.getString(2));

    Stat stat = new Stat(id, bf, wt, year, month, day);
    return stat;
}

public List<Stat> getAllStats(){
    List<Stat> statList = new ArrayList<Stat>();
    String selectQuery = "SELECT  * FROM " + TABLE_NAME;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor != null) {

        if (cursor.moveToFirst()) {
            do {
            int db_id = Integer.parseInt(cursor.getString(0));
            double bf = Double.valueOf(cursor.getString(1));
            double wt = Double.valueOf(cursor.getString(2));

            String date = cursor.getString(3);
            String[] date_tokens = date.split("-");
            int year = Integer.valueOf(date_tokens[0]);
            int month =  Integer.valueOf(date_tokens[1]);
            int day = Integer.valueOf(date_tokens[2]);

            Stat stat = new Stat(db_id,bf,wt,year,month,day);
            statList.add(stat);
            }
            while (cursor.moveToNext());
        }
    }


    //cursor.close();
    //db.close();
    return statList;

}

}

这是使用数据库的段。

public class StatListFragment extends ListFragment {

private Statdb db;// = new Statdb(getActivity());

[...]

private void showStats(){
    db = new Statdb(getActivity().getApplicationContext());

    db.getAllStats();

我希望我只是累了,错过了一些小事。任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:1)

试试这个:

 // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_NAME;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

答案 1 :(得分:0)

如果我没有弄错,那么您的查询字符串末尾可能缺少;

String selectQuery = "SELECT * FROM " + TABLE_NAME + ";";

但是,我的建议是使用String.format,这也有助于构建复杂的查询。例如:

String selectQuery = String.format("SELECT * FROM %s;", TABLE_NAME);