我收到一条错误,指出我的分钟列不存在,即使我在创建数据库表时已经清楚地初始化了它
这是我的问题所在的数据库活动
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_ALARM_DATABASE = "CREATE TABLE " + Constants.TABLE_NAME +
" ( " + Constants.KEY_ID + " TEXT PRIMARY KEY, " + Constants.COLUMN_NAME +
" TEXT, " + Constants.COLUMN_DESCRIPTION + " TEXT, " + Constants.COLUMN_HOUR +
" INT, " + Constants.COLUMN_MINUTE + " INT, "+ Constants.COLUMN_YEAR +
" INT, " + Constants.COLUMN_MONTH +
" INT, " + Constants.COLUMN_DAY +
" INT, " +Constants.COLUMN_TYPE+ " TEXT);";
db.execSQL(CREATE_ALARM_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXIST " + Constants.TABLE_NAME);
onCreate(db);
}
public void addAlarms(MyAlarm alarm) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values= new ContentValues();
values.put(Constants.COLUMN_NAME , alarm.getTitle());
values.put(Constants.COLUMN_DESCRIPTION , alarm.getContent());
values.put(Constants.COLUMN_HOUR , alarm.getHours());
values.put(Constants.COLUMN_MINUTE , alarm.getMinutes());
values.put(Constants.COLUMN_YEAR,alarm.getYear());
values.put(Constants.COLUMN_MONTH,alarm.getMonth());
values.put(Constants.COLUMN_DAY,alarm.getDay());
values.put(Constants.COLUMN_TYPE, alarm.getType());
db.insert(Constants.TABLE_NAME, null, values);
db.close();
}
public ArrayList<MyAlarm> getAlarms(){
String selectQuery= "SELECT * FROM " + Constants.TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(Constants.TABLE_NAME, new String[]{Constants.KEY_ID, Constants.COLUMN_NAME
,Constants.COLUMN_DESCRIPTION, Constants.COLUMN_MINUTE, Constants.COLUMN_HOUR, Constants.COLUMN_YEAR
,Constants.COLUMN_MONTH,Constants.COLUMN_DAY,Constants.COLUMN_TYPE},null,null,null,null,Constants.COLUMN_YEAR
+ " DESC");
if(cursor.moveToFirst()){
do{
MyAlarm alarm = new MyAlarm();
alarm.setTitle(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_NAME)));
alarm.setContent(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_DESCRIPTION)));
alarm.setYear(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_YEAR)));
alarm.setMonth(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_MONTH)));
alarm.setDay(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_DAY)));
alarm.setHours(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_HOUR)));
alarm.setMinutes(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_MINUTE)));
alarm.setId(cursor.getString(cursor.getColumnIndex(Constants.KEY_ID)));
alarm.setType(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_TYPE)));
alarmList.add(alarm);
}while(cursor.moveToNext());
}
cursor.close();
return alarmList;
}
错误总是指向我的光标......任何帮助都会非常感激
编辑:添加了Logcat
05-20 00:53:45.977 13871-13871/seniorreminder.androidstudioprojects.com.seniorreminder E/AndroidRuntime: FATAL EXCEPTION: main
Process: seniorreminder.androidstudioprojects.com.seniorreminder, PID: 13871
java.lang.RuntimeException: Unable to start activity ComponentInfo{seniorreminder.androidstudioprojects.com.seniorreminder/seniorreminder.androidstudioprojects.com.seniorreminder.MainActivity}: android.database.sqlite.SQLiteException: near "EXIST": syntax error (code 1): , while compiling: DROP TABLE IF EXIST alarms
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "EXIST": syntax error (code 1): , while compiling: DROP TABLE IF EXIST alarms
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
at seniorreminder.androidstudioprojects.com.seniorreminder.Database.DatabaseActivity.onUpgrade(DatabaseActivity.java:45)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:257)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
at seniorreminder.androidstudioprojects.com.seniorreminder.Database.DatabaseActivity.getAlarms(DatabaseActivity.java:74)
at seniorreminder.androidstudioprojects.com.seniorreminder.MainActivity.refreshData(MainActivity.java:131)
at seniorreminder.androidstudioprojects.com.seniorreminder.MainActivity.onCreate(MainActivity.java:73)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
根据logcat,错误是'SQLiteException:near“EXIST”:语法错误(代码1):,编译时:DROP TABLE IF EXIST alarm'
原因是SQLite没有名为EXIST的关键字。关键字应为 EXISTS 。因此,要解决此问题,请进行以下更改: -
将 db.execSQL("DROP TABLE IF EXIST " + Constants.TABLE_NAME);
替换为
<强> db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_NAME);
强>
您可能还希望考虑简化
Cursor cursor = db.query(Constants.TABLE_NAME, new String[]{Constants.KEY_ID, Constants.COLUMN_NAME
,Constants.COLUMN_DESCRIPTION, Constants.COLUMN_MINUTE, Constants.COLUMN_HOUR, Constants.COLUMN_YEAR
,Constants.COLUMN_MONTH,Constants.COLUMN_DAY,Constants.COLUMN_TYPE},null,null,null,null,Constants.COLUMN_YEAR
+ " DESC");
可能是
`Cursor cursor = db.query(Constants.TABLE_NAME,null,null,null,null,null,Constants.COLUMN_YEAR
+ " DESC");`
null 而不是字符串列名称等于获取所有列。
您可以使用
简化游标处理循环while (cusror.moveToNext) {
// do your stuff here
}
cursor.close();
如果光标超过最后一个条目, moveToNext()
会返回false(因此会处理空光标)。