2
Failure 1 (near "Table": syntax error) on 0x1d2440 when preparing 'CREATE TABLE Table(_id INTEGER PRIMARY KEY, Pname TEXT NOT NULL,Gender TEXT NOT NULL,Age TEXT NOT NULL,Weight TEXT NOT NULL,Height TEXT NOT NULL)'.

Im only new to android and I can't find any similar errors. I've already checked my code and this is the only thing that's been pestering me from finishing my application.

I would like to thank you guys in advance.

Here's my code:

public class Data {
public static final String KEY_ROWID="_id";
public static final String KEY_NAME="Pname";
public static final String KEY_GENDER="Gender";
public static final String KEY_AGE="Age";
public static final String KEY_HEIGHT="Height";
public static final String KEY_WEIGHT="Weight";

private static final String DATABASE_NAME="Data";
private static final String DATABASE_TABLE="Table";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

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

    }

    @Override
    public void onCreate(SQLiteDatabase db) {


        String CREATE_DATABASE_TABLE = "CREATE TABLE " + DATABASE_TABLE + "("
                + KEY_ROWID + " INTEGER PRIMARY KEY," +
                  KEY_NAME  + " TEXT NOT NULL,"
                + KEY_GENDER + " TEXT NOT NULL," 
                + KEY_AGE + " TEXT NOT NULL," 
                + KEY_WEIGHT + " TEXT NOT NULL," 
                + KEY_HEIGHT + " TEXT NOT NULL"
                 + ")";

     db.execSQL(CREATE_DATABASE_TABLE);



    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
        onCreate(db);

    }

}
public Data(Context c){
    ourContext = c;
}
public Data open(){
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}
public void close(){
    ourHelper.close();
}
public long createEntry(String Pname, String age, String gender,
        String height, String weight) {

    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, Pname);
    cv.put(KEY_GENDER, gender);
    cv.put(KEY_AGE, age);
    cv.put(KEY_HEIGHT, height);
    cv.put(KEY_WEIGHT, weight);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);


}

}

2 Answers 2

3

1) Change ::

private static final String DATABASE_TABLE="Table";

to another name that is not a SQLite reserved word

for example :: private static final String DATABASE_TABLE="myTable";

2) Inside onUpgrade method add an space in your query:

@Override 
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
         //db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
          db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
          onCreate(db);     
 } 
Sign up to request clarification or add additional context in comments.

Comments

2

Table is an SQL keyword; you have to quote it:

String DATABASE_TABLE = "\"Table\"";

However, it might be a better idea to give it a different name (one that better describes the table's contents).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.