每当我在更新光标后第二次尝试计数时,显示无法打开数据库文件
package com.example.soumyadeep.medicinealertapp;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.SystemClock;
import java.util.Calendar;
public class Background extends IntentService
{
SQLiteDatabase db;
public Background() {
super("");
}
public Background(String name) {
super(name);
}
@Override
public void onCreate() {
super.onCreate();
}
protected void onHandleIntent(Intent intent) {
db = openOrCreateDatabase("MEDICAL_ALERT", MODE_PRIVATE, null);
Cursor c = db.rawQuery("select * from profile;", null);
if (c.getCount() > 0) {
while (true)
{
Calendar cal = Calendar.getInstance();
String time = "";
int min = cal.get(Calendar.MINUTE);
int hour = cal.get(Calendar.HOUR);
int am_pm = cal.get(Calendar.AM_PM);
int day_of_week = cal.get(Calendar.DAY_OF_WEEK);
time = hour + ":";
if (min < 10)
time += "0";
time += min;
if (am_pm == 0)
time += " AM";
else
time += " PM";
c = db.rawQuery("select * from Medicinetime where time='" + time + "';", null);
if (c.getCount() > 0) {
c.moveToFirst();
do {
Cursor temp = db.rawQuery("select * from medicineday where medicineid='" + c.getString(2) + "';", null);
if (temp.getString(day_of_week + 1).equals("T")) {
Cursor temp2 = db.rawQuery("select * from medicinebasic where id='" + c.getString(2) + "';", null);
Intent i = new Intent(getBaseContext(), Reminder.class);
i.putExtra("TYPE", temp2.getString(4));
i.putExtra("URGENT", temp2.getString(2));
i.putExtra("TIME", c.getString(1));
i.putExtra("DOSAGE", temp2.getString(3));
i.putExtra("NAME", temp2.getString(1));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getBaseContext().startActivity(i);
}
} while (c.moveToNext());
}
}
} else
stopSelf();
}
@Override
public void onDestroy() {
db.close();
super.onDestroy();
}
@Override
public void onTaskRemoved(Intent rootIntent){
Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1,
restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartServicePendingIntent);
super.onTaskRemoved(rootIntent);
}
}
这是抛出以下错误的代码:
07-28 19:32:07.728 14700-14769/com.example.soumyadeep.medicinealertapp E/SQLiteLog: (14) cannot open file at line 31307 of [2ef4f3a5b1]
07-28 19:32:07.729 14700-14769/com.example.soumyadeep.medicinealertapp E/SQLiteLog: (14) os_unix.c:31307: (24) open(/data/user/0/com.example.soumyadeep.medicinealertapp/databases/MEDICAL_ALERT-journal) -
07-28 19:32:07.729 14700-14769/com.example.soumyadeep.medicinealertapp E/SQLiteLog: (14) cannot open file at line 31307 of [2ef4f3a5b1]
07-28 19:32:07.729 14700-14769/com.example.soumyadeep.medicinealertapp E/SQLiteLog: (14) os_unix.c:31307: (24) open(/data/user/0/com.example.soumyadeep.medicinealertapp/databases/MEDICAL_ALERT-journal) -
07-28 19:32:07.729 14700-14769/com.example.soumyadeep.medicinealertapp E/SQLiteLog: (14) statement aborts at 12: [select * from Medicinetime where time='7:32 PM';] unable to open database file
07-28 19:32:07.729 14700-14769/com.example.soumyadeep.medicinealertapp E/SQLiteQuery: exception: unable to open database file (code 14); query: select * from Medicinetime where time='7:32 PM';
07-28 19:32:07.729 14700-14769/com.example.soumyadeep.medicinealertapp E/AndroidRuntime: FATAL EXCEPTION: IntentService[]
Process: com.example.soumyadeep.medicinealertapp, PID: 14700
android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14)
at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:843)
at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:143)
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:132)
at com.example.soumyadeep.medicinealertapp.Background.onHandleIntent(Background.java:60)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
任何想法?
getCount()方法抛出错误:
c = db.rawQuery("select * from Medicinetime where time='" + time + "';", null);
if (c.getCount() > 0)