SQLiteException :(靠近“表”:语法错误)

时间:2013-09-04 12:41:34

标签: android sqlite

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


}

}

2 个答案:

答案 0 :(得分:3)

1)改变::

private static final String DATABASE_TABLE="Table";

到另一个不是SQLite reserved word

的名称

例如:: 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);     
 } 

答案 1 :(得分:1)

TableSQL keyword;你必须引用它:

String DATABASE_TABLE = "\"Table\"";

但是,给它一个不同的名称(一个更好地描述表格内容的名称)可能是一个更好的主意。