问题
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
`
回答1:
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
.
回答2:
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");
回答3:
Add an extra space between Constants.DATE_NAME + " DESC"
. If it still shows the same error
Uninstall and Install your application.
回答4:
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.
来源:https://stackoverflow.com/questions/46975068/sqlite-no-such-column