此DBHelper代码适用于所有版本,但不适用于Android版本9
public class DBHelper extends SQLiteOpenHelper {
private static int db_version = 1;
private static String db_name = "quote_db";
private String db_path;
private SQLiteDatabase db;
private final Context con;
public DBHelper(Context con) {
super(con, db_name, null, db_version);
// TODO Auto-generated constructor stub
this.con = con;
db_path=con.getDatabasePath(db_name).getPath();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
public void createDB() throws IOException {
this.getReadableDatabase();
copyDB();
Log.d("Database", "copy databse");
}
private boolean checkDB() {
SQLiteDatabase cDB = null;
try {
cDB = SQLiteDatabase.openDatabase(db_path+db_name, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
}
if (cDB != null) {
cDB.close();
}
return cDB != null ? true : false;
}
private void copyDB() throws IOException {
InputStream inputFile = con.getAssets().open(db_name);
String outFileName = db_path + db_name;
OutputStream outFile = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = inputFile.read(buffer)) > 0) {
outFile.write(buffer, 0, length);
}
outFile.flush();
outFile.close();
inputFile.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS var_guj");
db.execSQL("DROP TABLE IF EXISTS var_eng");
onCreate(db);
Log.d("DB Upgrade", "Yes Upgrade");
}
//get category list from database
public Cursor get_categorydatabyid(String colum_name,int cateoryId) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
try {
cursor = db.rawQuery("SELECT id,date,month,"+colum_name+",day FROM quote where category_id="+cateoryId+" ORDER BY id",null);
if (cursor != null) {
cursor.moveToFirst();
db.close();
return cursor;
}
} catch (Exception e) {
db.close();
Log.d("Error-", ""+e);
Toast.makeText(con, "Compai-" + e, Toast.LENGTH_LONG).show();
}
cursor.close();
db.close();
return cursor;
}
public int getmaxid(int todate,int tomonth) {
int maxID = 0;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
try {
cursor = db.rawQuery("SELECT id FROM quote WHERE date="+todate+" and month="+tomonth+"", null);
if (cursor != null) {
cursor.moveToFirst();
maxID = cursor.getInt(0);
db.close();
return maxID;
}
} catch (Exception e) {
db.close();
Log.d("Error-", ""+e);
Toast.makeText(con, "Compai-" + e, Toast.LENGTH_LONG).show();
}
cursor.close();
db.close();
return maxID;
}
}
错误是
Compai-android.database.sqlite.SQLite Exception:no such table : quote(code 1 SQLITE_ERROR):,while compiling:SELECT id FROM quote WHERE date=14 and month=10
3条答案
按热度按时间zengzsys1#
这是因为使用了这个。getReadableDatabase()。
这将创建一个数据库,自Android 9以来,默认情况下,数据库以WAL模式打开。这将产生两个文件-wal和-shm(每个文件前面都有数据库文件名),这两个文件由被覆盖的数据库拥有,而不是由覆盖前者的数据库拥有。检测到此异常,并导致返回空数据库,因此找不到表。
有几种解决方法,但建议和最有效的方法是不打开数据库,而是检查文件是否存在。
例如:-
除此之外,还需要在createDB方法中删除/注解掉
this.getReadableDatabase();
。因为这也会导致同样的问题。更深入的回答
shyt4zoc2#
在
createDB()
中的this.getReadableDatabase();
之后添加this.close();
kgsdhlau3#
在Android studio中,转到View --〉Tool Windows --〉Device File Explorer Find this path --〉“/data/data/yourpackagename/databases/”,然后手动上传更新的数据库。很管用