0

I am attempting to use a SQLite database to store emails locally in android. When I first call context.getWritableDatabase(), the database does not yet exist and is created. I am getting a syntax error on the create statement... My code looks like this...

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

I am new to SQLite syntax and I have been searching for what could cause this with no avail. Any idea whats causing this? A database is really the best way to store such data locally.

The exact error reads as follows..

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 Answer 1

6

You cannot use the keyword 'index' as your table column name, change it to something else like tbl_index instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Might have a problem with KEY_FROM = "from" too. Double quoting the offending names (everywhere!) is an option as well, not a very pleasant option but an option nonetheless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.