问题
Can you help my?
I have created and published the app on Google play store that contains a database, I have tested it on different android phones. My app works perfect on android version (Android 10,8,5 etc...) except on Android 9, do you know how can I fix that problem? I find that problem on Huawei Smart Z, Honor P20, Samsung Y6,Xiaomi Mi. Play store console says that my app can run on that models. I am beginner in app development.
This happening on mine Huawei Smart: when I install the app it opens and crashes when I want to enter in database, and when I want to enter in app again it crashes and on logcat:
E/Path 1: data/data/com.gema/databases/
E/SQLiteLog: (14) cannot open file at line 36906 of [c255889bd9]
E/SQLiteLog: (14) os_unix.c:36906: (2) open(//data/data/com.gematranslate/databases/gema_recnik.db) -
E/SQLiteDatabase: Failed to open database 'data/data/com.gematranslate/databases/gema_recnik.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (Sqlite code 14 SQLITE_CANTOPEN): Could not open database, (OS error - 2:No such file or directory)
In android manifest I put:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
This is my code for check db:
public boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);
myContext.getDatabasePath(DB_NAME).getPath();
checkDB.disableWriteAheadLogging();
} catch (SQLException e)
{
//
}
if (checkDB!= null){
checkDB.close();
}
return checkDB != null? true : false;
This is my code for opening db:
public void openDataBase() {
String myPath = DB_PATH + DB_NAME;
myContext.getDatabasePath(DB_NAME).getPath();
db = SQLiteDatabase.openDatabase(myPath,null, SQLiteDatabase.OPEN_READWRITE);
db.disableWriteAheadLogging();
If anyone can help me, thanks a lot.
回答1:
This is a solution that is supported on all versions of Android
public class DataBase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "database.db";
private static String DATABASE_PATH = "";
private static String LAST_DATABASE_PATH = "";
private SQLiteDatabase mDataBase;
private final Context ctx;
public DataBase2(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
DATABASE_PATH = context.getDatabasePath(DATABASE_NAME).getPath();
this.ctx = context;
if (!checkIfDBExists()) {
createDataBase();
}
File dbFile = new File(Environment.getDataDirectory().getPath()+"/data/"+ BuildConfig.APPLICATION_ID +"/databases");
if (dbFile.isDirectory()) {
String[] child = dbFile.list();
assert child != null;
for (int i = 0; i < child.length; i++) {
if (!(child[i].equals("database.db") || child[i].equals(DATABASE_NAME))) {
new File(dbFile, child[i]).delete();
}
}
}
}
public void createDataBase() {
this.getReadableDatabase();
this.close();
try {
copyDataBase();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
File DbFile = ctx.getDatabasePath(DATABASE_NAME);
if (DbFile.exists()) {
copyDataBase();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean checkIfDBExists() {
File dbFile = new File(DATABASE_PATH);
return dbFile.exists();
}
public void openDataBase() {
close();
mDataBase = SQLiteDatabase.openDatabase(DATABASE_PATH, null, CREATE_IF_NECESSARY);
}
private void copyDataBase() throws IOException {
InputStream mInput = ctx.getAssets().open("database.db");
String outfileName = DATABASE_PATH;
OutputStream mOutput = new FileOutputStream(outfileName);
byte[] buffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(buffer)) > 0) {
mOutput.write(buffer, 0, mLength);
}
mOutput.flush();
mInput.close();
mOutput.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public synchronized void close() {
if (mDataBase != null)
mDataBase.close();
SQLiteDatabase.releaseMemory();
super.close();
}
}
来源:https://stackoverflow.com/questions/62106093/my-developer-app-problem-with-opening-database-on-android-9