Frage

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

I've searched all over stackoverflow and most of the solutions for the error above have been due to not putting in whitespace when putting together a query. I've checked over and over again and can't find any problems with the spacing and the other commands I'm using seem to be working fine. The table is created successfully and I can insert data (and retrieve it using the id) but I'm unable to select all rows. Here's my DB code:

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;

}

}

This is a segment where the database is being used.

public class StatListFragment extends ListFragment {

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

[...]

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

    db.getAllStats();

I'm hoping I'm just tired and missing something small. Any help is appreciated!

War es hilfreich?

Lösung

Try this:

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

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

Andere Tipps

If I'm not mistaken then perhaps you are missing ; at the end of your query string.

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

However, my recommendation is to use String.format which is helpful in building complex queries too. For example:

String selectQuery = String.format("SELECT * FROM %s;", TABLE_NAME);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top