ins*_*ode 1 sql database android android-sqlite
在创建数据库以保存标题,内容并按日期降序排序时,我没有得到这样的列异常,我已经多次检查并且无法知道原因.我的代码是
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);
}
Run Code Online (Sandbox Code Playgroud)
我的查询语句如下:
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");
}
Run Code Online (Sandbox Code Playgroud)
我在logcat中收到错误如下
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
`
Run Code Online (Sandbox Code Playgroud)
Caused by: android.database.sqlite.SQLiteException: no such column:
Run Code Online (Sandbox Code Playgroud)
一个SQLite异常,指示SQL解析或执行时出错.
您应该在Section中添加额外的SPACEDESC.
纠正SELECT语句.
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 ");
Run Code Online (Sandbox Code Playgroud)
然后Clean-Rebuild-Run.