Odhran
Odhran

Reputation: 95

App keeps crashing when accessing sqlite database

I'm try to access a database which was working fine (until i added more fields and updated the code accordingly).

I get this error:

Caused by: android.database.sqlite.SQLiteException: near ",": syntax error: , while compiling: SELECT _id, title, description, locations, datesTimes, added, provider, url, cast, director, runningTime, certification, imageUrl, trailerUrl FROM films ORDER BY _id DESC

Here is the code for the query:

public Cursor getAllTitles()
    {
    return myDataBase.query("films", new String[] {
    KEY_ROWID,
    KEY_TITLE,
    KEY_DESCRIPTION,
    KEY_LOCATIONS,
    KEY_DATESTIMES,
    KEY_ADDED,
    KEY_PROVIDER,
    KEY_URL,
    KEY_CAST,
    KEY_DIRECTOR,
    KEY_RUNNINGTIME,
    KEY_CERTIFICATION,
    KEY_IMAGEURL,
    KEY_TRAILERURL},
    null,
    null,
    null,
    null,
    KEY_ROWID + " DESC"); //Sort the data using the Primary Key in descending order . . meaning last added is shown first.
    }

And here the code for extracting data from the database:

    //---Get all records from database---
    myDbHelper.openDataBase();
    cursorPosition = myDbHelper.getAllTitles();

    if (cursorPosition.moveToFirst())
    {
    do {
        AddRecordToArray(cursorPosition);
    } while (cursorPosition.moveToNext());
    }
        myDbHelper.close();
    }

    public void AddRecordToArray(Cursor cursorPosition)
    {

        //Add new record to array (which gets data from database using cursorPosition (for current row) and .getstring( for column number).
        final FilmRecord film1 = new FilmRecord(cursorPosition.getString(1), cursorPosition.getString(4), cursorPosition.getString(5), cursorPosition.getString(3), cursorPosition.getString(2), cursorPosition.getString(6), cursorPosition.getString(7), cursorPosition.getString(8), cursorPosition.getString(9), cursorPosition.getString(10), cursorPosition.getString(11), cursorPosition.getString(12), cursorPosition.getString(13));
        films.add(film1);

        myDbHelper.close();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myDbHelper != null) {
            myDbHelper.close();
        }

    }

If there is any more information required to solve this error just let me know what you need. I'm really confused with the ",".

Upvotes: 0

Views: 270

Answers (2)

Yaqub Ahmad
Yaqub Ahmad

Reputation: 27659

Please change the value of the key KEY_CAST from cast to some other value because the cast is a reserve word.

Upvotes: 1

George
George

Reputation: 2200

"cast" is a reserved word. Try changing that column name, or quoting it. www.sqlite.org/lang_keywords.html

Upvotes: 1

Related Questions