我创建了一个静态方法,访问其中一个activity的数据库,但是在打开数据库时总是有错误。
MainActivity:
public static String getStudentData() {
SQLiteDatabase sampleDB = null;
try {
//NOT WORKING
sampleDB = SQLiteDatabase.openDatabase("studentDB",null, SQLiteDatabase.CREATE_IF_NECESSARY);
//Also not working
//sampleDB = SQLiteDatabase.openOrCreateDatabase("studentDB", null);
Cursor c = sampleDB.rawQuery("SELECT * FROM student where id='1'", null);
if (c != null ) {
if (c.moveToFirst()) {
do {
//...
}while (c.moveToNext());
}
}
c.close();
} catch (Exception se ) {
} finally {
sampleDB.close();
}
}
OtherActivity
String student = MainActivity.getStudentData();
但是显示sqlite3_open_v2("studentDB", &handle, 6, NULL) failed
.我没找到问题所在,我也使用了 MODE_WORLD_WRITEABLE
方法。现在使用的是 MODE_PRIVATE
创建数据库。
向大家请教问题之所在,谢谢!