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)'.

我只是 android 新手,我找不到任何类似的错误。我已经检查了我的代码,这是唯一让我无法完成申请的事情。

我要提前感谢你们。

这是我的代码:

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


}

}

4

2 回答 2

3

1) 改变 ::

private static final String DATABASE_TABLE="Table";

到另一个不是SQLite 保留字的名称

例如 ::private static final String DATABASE_TABLE="myTable";

2) 在 onUpgrade 方法中在查询中添加一个空格:

@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);     
 } 
于 2013-09-04T14:50:57.980 回答
1

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).

于 2013-09-04T14:37:20.373 回答