<column definition =“” name =“”>或预期的结果为“索引”

时间:2019-05-24 23:54:34

标签: android sqlite android-sqlite

当我尝试创建表时收到错误消息“或期望,得到'Index'”,但我真的不明白为什么代码在此行中期望列定义或表约束

我尝试过更改空格,但是只能更改提示错误的位置。错误消息的内容不会改变

这是我声明字符串的部分

 public class TaskEntry implements BaseColumns {
        public static final String TABLE = "Users";
        public static final String INDEX = "Index";
        public static final String COL_TASK_TITLE = "title";
    }

以下是我创建表部分的代码

 public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + Item_contract.TaskEntry.TABLE + " ( " +
                Item_contract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,  " +
                Item_contract.TaskEntry.INDEX + " INTEGER NOT NULL, " +
                Item_contract.TaskEntry.COL_TASK_TITLE + " TEXT NOT NULL" + ");";

        db.execSQL(createTable);
    }

1 个答案:

答案 0 :(得分:0)

您不能将INDEX用作列名,因为它是关键字。

  

SQL标准指定了大量关键字,这些关键字可能不是   用作表,索引,列,数据库的名称,用户定义   函数,排序规则,虚拟表模块或任何其他命名的   宾语。关键字列表很长,几乎没人记得   商场。对于大多数SQL代码,最安全的选择是永远不要使用任何   英语单词作为用户定义对象的名称。

SQL As Understood By SQLite - SQLite Keywords

所以改变

 public static final String INDEX = "Index";

也许是

public static final String INDEX = "IX";

如果您确实希望将列名设为INDEX,则可以将其括起来

public static final String INDEX = "[Index]";

按照:-

If you want to use a keyword as a name, you need to quote it. There are four ways of quoting keywords in SQLite:

'keyword'       A keyword in single quotes is a string literal.
"keyword"       A keyword in double-quotes is an identifier.
[keyword]       A keyword enclosed in square brackets is an identifier. This is not standard SQL. This quoting mechanism is used by MS Access and SQL Server and is included in SQLite for compatibility.
`keyword`       A keyword enclosed in grave accents (ASCII code 96) is an identifier. This is not standard SQL. This quoting mechanism is used by MySQL and is included in SQLite for compatibility.

SQL As Understood By SQLite - SQLite Keywords

注意

您必须执行以下一项操作才能运行onCreate方法,从而更改架构:-

  • 删除应用程序的数据。
  • 卸载应用程序。