insearchofcode
insearchofcode

Reputation: 448

SQLite no such column

I'm getting no such column exception when creating database to save title, content & sort it by date descending order, i've checked many times and unable to know the reason. My code is

public void onCreate(SQLiteDatabase sqLiteDatabase) {
    //create our table
    String CREATE_WISHES_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "(" +
                                  Constants.KEY_ID + " INTEGER PRIMARY KEY, " +
                                Constants.TITLE_NAME + " TEXT, " +
                                Constants.CONTENT_NAME + " TEXT, " +
                                Constants.DATE_NAME + "  LONG);";
    sqLiteDatabase.execSQL(CREATE_WISHES_TABLE);

}

My query statement is as follows:

public ArrayList<MyWish> getWishes() {

    String selectQuery = "SELECT * FROM " + Constants.TABLE_NAME;

    SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();

    Cursor cursor = sqLiteDatabase.query(Constants.TABLE_NAME, new String[]{Constants.KEY_ID, Constants.TITLE_NAME, Constants.CONTENT_NAME,
                    Constants.DATE_NAME}, null, null, null, null, Constants.DATE_NAME + "DESC");
}

I'm getting the error in logcat as follows

10-27 17:18:04.272  E/SQLiteLog: (1) no such column: recorddateDESC
10-27 17:18:04.274  E/AndroidRuntime: FATAL EXCEPTION: main`
Caused by: android.database.sqlite.SQLiteException: no such column: recorddateDESC (code 1): , while compiling: SELECT _id, title, content, recorddate FROM wishes ORDER BY recorddateDESC
                                                                          `

Upvotes: 1

Views: 3216

Answers (5)

Paul Efford
Paul Efford

Reputation: 265

As per today, with the updated version of Sqlite string literals are no longer allowed to be entered with doble quotes "mystringValue" but only with single quotes.

Found here SQLite error 1 "no such column"

Upvotes: 0

Ant
Ant

Reputation: 1708

Apart from the other answers here, this can also occur when the column is defined in a trigger. For example, if you see it on trying to delete a row look for erroneous delete triggers in that table.

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Caused by: android.database.sqlite.SQLiteException: no such column:

A SQLite exception that indicates there was an error with SQL parsing or execution.

You should add extra SPACE in DESC Section.

Rectify SELECT Statement.

 Cursor cursor = sqLiteDatabase.query(Constants.TABLE_NAME, new String[]{Constants.KEY_ID, Constants.TITLE_NAME, Constants.CONTENT_NAME,
                Constants.DATE_NAME}, null, null, null, null, Constants.DATE_NAME + " DESC ");

Then Clean-Rebuild-Run.

Upvotes: 5

Sushin Pv
Sushin Pv

Reputation: 1894

Add an extra space between Constants.DATE_NAME + " DESC". If it still shows the same error

Uninstall and Install your application.

Upvotes: 2

Antoine Stas
Antoine Stas

Reputation: 229

You must add a space between the table name and the "DESC"

public ArrayList<MyWish> getWishes() {

String selectQuery = "SELECT * FROM " + Constants.TABLE_NAME;

SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();

Cursor cursor = sqLiteDatabase.query(Constants.TABLE_NAME, new String[]{Constants.KEY_ID, Constants.TITLE_NAME, Constants.CONTENT_NAME,
                Constants.DATE_NAME}, null, null, null, null, Constants.DATE_NAME + " DESC");

Upvotes: 2

Related Questions