我正在使用sqlite数据库浏览器来创建sqlite数据库。我在项目资产/数据库/文件夹中复制了数据库。
下面是我正在使用的dbhelper类,数据库被检查,它被打开,但是在查询时它说找不到alarms_info表。我在我的数据库中有这个表。
public class LatestHelper extends SQLiteOpenHelper {
private static String DB_NAME = "alarms";
private SQLiteDatabase db;
private final Context context;
private String DB_PATH;
private static final String TABLE_ALARMS ="alarms_info";
private static final String ALARM_ID ="_id";
private static final String ALARM_CODE ="alarm_code";
private static final String PRIORITY ="alarm_priority";
private static final String USER_MESSAGE ="user_message";
private static final String PROMPT_MESSAGE ="prompt_message";
public LatestHelper(Context context) {
super(context, DB_NAME, null, 3);
this.context = context;
DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException {
InputStream myInput = context.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
/* public Cursor getData() {
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
Cursor c = db.rawQuery("SELECT * FROM master", null);
// Note: Master is the one table in External db. Here we trying to access the records of table from external db.
return c;
} */
public Alarmsinfo getalarminfo(int id)
{
String myPath = DB_PATH + DB_NAME;
Alarmsinfo alarminfo = new Alarmsinfo();
SQLiteDatabase db = this.getReadableDatabase();
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
System.out.println("manav"+db);
String query = "SELECT "+ALARM_CODE+","+PRIORITY+","+USER_MESSAGE+","+PROMPT_MESSAGE+" FROM "+TABLE_ALARMS+" WHERE "+ALARM_ID+" = "+id;
Cursor cursor = db.rawQuery(query,null);
if(cursor!=null && cursor.getCount()>0){
cursor.moveToFirst();
do {
alarminfo.setAlarm_code(cursor.getString(0));
alarminfo.setPriority(cursor.getInt(1));
alarminfo.setMsg(cursor.getString(2));
alarminfo.setPrompt_msg(cursor.getString(3));
} while (cursor.moveToNext());
}
return alarminfo;
}
下面是logcat:
E/SQLiteLog(25136): (1) no such table: alarms_info
D/AndroidRuntime(25136): Shutting down VM
W/dalvikvm(25136): threadid=1: thread exiting with uncaught exception (group=0x40d92300)
E/AndroidRuntime(25136): FATAL EXCEPTION: main
E/AndroidRuntime(25136): java.lang.IllegalStateException: Could not execute method of the activity
E/AndroidRuntime(25136): at android.view.View$1.onClick(View.java:3595)
E/AndroidRuntime(25136): at android.view.View.performClick(View.java:4088)
E/AndroidRuntime(25136): at android.view.View$PerformClick.run(View.java:16984)
E/AndroidRuntime(25136): at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(25136): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(25136): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(25136): at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime(25136): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(25136): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(25136): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime(25136): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(25136): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(25136): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime(25136): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(25136): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(25136): at android.view.View$1.onClick(View.java:3590)
E/AndroidRuntime(25136): ... 11 more
E/AndroidRuntime(25136): Caused by: android.database.sqlite.SQLiteException: no such table: alarms_info (code 1): , while compiling: SELECT alarm_code,alarm_priority,user_message,prompt_message FROM alarms_info WHERE _id = 1
E/AndroidRuntime(25136): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
E/AndroidRuntime(25136): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
E/AndroidRuntime(25136): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
E/AndroidRuntime(25136): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
E/AndroidRuntime(25136): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
E/AndroidRuntime(25136): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
E/AndroidRuntime(25136): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
E/AndroidRuntime(25136): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
E/AndroidRuntime(25136): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
E/AndroidRuntime(25136): at com.example.mydialysis.handlers.LatestHelper.getalarminfo(LatestHelper.java:92)
E/AndroidRuntime(25136): at com.example.mydialysis.Dashboard.addalarm(Dashboard.java:95)
E/AndroidRuntime(25136): at com.example.mydialysis.Dashboard.oncritical(Dashboard.java:79)
E/AndroidRuntime(25136): ... 14 more