SQLite CREATE TABLE语法错误(代码1)

时间:2015-04-23 20:34:37

标签: android sqlite

我试图像这样创建SQLite数据库:

private static final String UID = "_id";
private static final String NAME = "Name";
private static final String TYPE = "Type";
private static final String UNITS = "Units";
private static final String CURRENCY = "Currency";
private static final String PRICE = "Price";
private static final String ADD = "Default";
private static final String WEATHER = "Depends";
private static final String CREATE_TABLE = 
     " CREATE TABLE "+TABLE_NAME+
     " ( "  +UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
     NAME+     " VARCHAR (255), "+
     TYPE+     " VARCHAR (255), "+
     UNITS+    " VARCHAR (255), "+
     CURRENCY+ " VARCHAR (255), "+
     PRICE+    " VARCHAR (255), "+
     ADD+      " VARCHAR (255), "+
     WEATHER+  " VARCHAR (255)  ); ";

public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
}

获取此错误日志:

Caused by: android.database.sqlite.SQLiteException: near "Default": 
syntax error (code 1): , while compiling: CREATE TABLE Meters ( _id INTEGER 
PRIMARY KEY AUTOINCREMENT, Name VARCHAR (255), Type VARCHAR (255), Units 
VARCHAR (255), Currency VARCHAR (255), Price VARCHAR (255), Default VARCHAR 
(255), Depends VARCHAR (255)  );

我真的没有看到问题: - /欢迎任何可能出错的建议,谢谢你们

3 个答案:

答案 0 :(得分:0)

DEFAULT是SQL中的关键字。将变量ADD中的列名更改为其他内容。

答案 1 :(得分:0)

ADD的值更改为Default,因为这是SQL中的保留字

答案 2 :(得分:0)

Default是SQLite的关键字。你可能不应该把它当作名字。如果要使用关键字作为名称,则需要引用它。

以下是您可能要检查的一些SQLite文档:https://www.sqlite.org/lang_keywords.html