在android中执行SQLite时出现语法错误

时间:2012-07-01 22:56:41

标签: android sqlite

我正在尝试使用SQLite数据库在android中本地存储电子邮件。当我第一次调用context.getWritableDatabase()时,数据库尚不存在并被创建。我在create语句中遇到语法错误...我的代码看起来像这样......

private static final String DATABASE_NAME = "views.db";
    private static final int DATABASE_VERSION = 1;

    public static final String TABLE_VIEWS = "views";

    private static final String KEY_ID = "id";
    private static final String KEY_INDEX = "index";
    private static final String KEY_FROM = "from";
    private static final String KEY_UNREAD = "unread";
    private static final String KEY_SUBJECT = "subject";
    private static final String KEY_BODY = "body";


    // Database creation sql statement
    private static final String CREATE_VIEWS_TABLE = "CREATE TABLE " + TABLE_VIEWS + " ("
            + KEY_ID + " INTEGER PRIMARY KEY," 
            + KEY_INDEX + " INTEGER NOT NULL,"
            + KEY_UNREAD + " TEXT NOT NULL," 
            + KEY_SUBJECT + " TEXT NOT NULL,"
            + KEY_FROM + " TEXT NOT NULL," 
            + KEY_BODY + " TEXT NOT NULL);";

我是SQLite语法的新手,我一直在寻找可能导致这种情况无效的原因。有什么想法导致这个?数据库确实是在本地存储此类数据的最佳方式。

确切的错误如下:

07-01 17:54:42.827: E/AndroidRuntime(3036): Caused by: android.database.sqlite.SQLiteException: near "index": syntax error: CREATE TABLE views (id INTEGER PRIMARY KEY,index INTEGER NOT NULL,unread TEXT NOT NULL,subject TEXT NOT NULL,from TEXT NOT NULL,body TEXT NOT NULL);

1 个答案:

答案 0 :(得分:6)

您不能使用关键字“index”作为表格列名,而是将其更改为其他内容,例如tbl_index。