Android Sqlite创建语法错误

时间:2015-11-30 10:13:38

标签: sqlite

我似乎无法找出我在这里做错了什么。似乎在createNewTable方法中的create命令中出现语法错误。

  

Android SQLite插入错误(语法错误代码1)

我已添加了我的数据库代码:

private static final String KEY_DATE = "date";
private static final String KEY_TIME_IN = "timeCheckIn";
private static final String KEY_TIME_OUT = "timeCheckOut";
private static final String KEY_LATITUDE_IN = "latCheckIn";
private static final String KEY_LONGITUDE_IN = "longCheckIn";
private static final String KEY_LATITUDE_OUT = "latCheckOut";
private static final String KEY_LONGITUDE_OUT = "longCheckOut";

public void createNewTable(){
    SQLiteDatabase db = this.getWritableDatabase(int _id);
        Log.v("Database", "Adding Check in table: " + _id);
        String CREATE_TABLE = " CREATE TABLE " + Integer.toString(_id) + "("
                + KEY_DATE + " TEXT," + KEY_TIME_IN + " TEXT," + KEY_TIME_OUT + " TEXT,"
                + KEY_LATITUDE_IN + " REAL," + KEY_LONGITUDE_IN + " REAL," + KEY_LATITUDE_OUT + " REAL,"
                + KEY_LONGITUDE_OUT + " REAL" + ")";
        db.execSQL(CREATE_TABLE);}

我的logcat错误如下:

 Caused by: android.database.sqlite.SQLiteException: near "1": syntax error (code 1): , while compiling: CREATE TABLE 1(date TEXT,timeCheckIn TEXT,timeCheckOut TEXT,latCheckIn REAL,longCheckIn REAL,latCheckOut REAL,longCheckOut REAL)

2 个答案:

答案 0 :(得分:0)

您的表名以数字开头。不确定SQLite是否允许它,但在MySQL中,如果您的表名是数字,那么对于每个查询,您需要使用双引号指定表号。尝试为表名提供前缀

答案 1 :(得分:0)

SQLite告诉你,不允许未转义的表名以数字开头,在本例中为数字1.如果要使用数字,必须使用括号转义表名,如此

String CREATE_TABLE = " CREATE TABLE [" + Integer.toString(_id) + "] ("
        + KEY_DATE + " TEXT," + KEY_TIME_IN + " TEXT," + KEY_TIME_OUT + " TEXT,"
        + KEY_LATITUDE_IN + " REAL," + KEY_LONGITUDE_IN + " REAL," + KEY_LATITUDE_OUT + " REAL,"
        + KEY_LONGITUDE_OUT + " REAL" + ")";