У меня есть хранилище уведомлений в базе данных SQLITE, и я работаю android 10 ниже версии нормально, но android версия 10 не работает. У меня есть ошибка android .database.sqlite.SQLiteException: нет такой таблицы: FredUserActivityLog (код 1 SQLITE_ERROR): при компиляции: INSERT INTO FredUserActivityLog (имя, телефон) VALUES (?,?). Пожалуйста, помогите мне, в чем ошибка.
public class DatabaseHelpers extends SQLiteOpenHelper {
private static String DB_NAME = AppConstants.DATABASE;
// private static String DB_NAME1 = Environment.getExternalStorageDirectory() + "/Fred360/database/"
+ DB_NAME;//"
private static DatabaseHelpers dataBaseRef;
private static SQLiteDatabase mDatabase;
private final Context mContext;
private final String TAG = "DatabaseHelpers class";
private int mOpenCounter = 0;
public DatabaseHelpers(Context context) {
super(context, Environment.getExternalStorageDirectory() + File.separator +
context.getPackageName() + File.separator + "database" + File.separator + DB_NAME, null, 2);
this.mContext = context;
}
public synchronized static DatabaseHelpers getInstance(Context mContext) {
if (dataBaseRef != null) {
return dataBaseRef;
} else {
return new DatabaseHelpers(mContext);
}
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// Log.d(TAG, "On Create is calling...");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
mDatabase = SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory() +
File.separator + mContext.getPackageName() + File.separator + "database" +
File.separator + DB_NAME, null,
SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE);
// Log.i( TAG , " Data base is open : " + mDatabase);
// }
mDatabase.disableWriteAheadLogging();
}
@Override
public synchronized void close() {
if (mDatabase != null)
mDatabase.close();
super.close();
}
private boolean checkDataBase1() {
SQLiteDatabase checkDB = null;
String DB_NAME1;
try {
if (Build.VERSION.SDK_INT >= 28) {
SQLiteDatabase database = getReadableDatabase();
DB_NAME1 = database.getPath();
} else {
DB_NAME1 = Environment.getExternalStorageDirectory() + File.separator +
mContext.getPackageName() + File.separator + "database" + File.separator + DB_NAME;
}
File file = new File(DB_NAME1);
if (file.exists() && !file.isDirectory()) {
checkDB = SQLiteDatabase.openDatabase(DB_NAME1, null,
SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
// Log.i( TAG , " Database exist in sd card : " + checkDB);
checkDB.disableWriteAheadLogging();
} else {
File dbfile = new File(Environment.getExternalStorageDirectory() + File.separator +
mContext.getPackageName() + File.separator + "database");
dbfile.mkdirs();
// Log.i(TAG, "Media Scanner Null pointer excpetion.." + mContext);
MediaScannerConnection.scanFile(mContext, new String[]{dbfile.toString()}, null, null);
File dbfile1 = new File(DB_NAME1);
// Log.i(TAG, "Media Scanner Null pointer excpetion.." + mContext);
checkDB = SQLiteDatabase.openOrCreateDatabase(dbfile1, null);
// Log.i(TAG, "Media Scanner Null pointer excpetion.." + checkDB);
checkDB.close();
try {
copyDataBase1();
Uri uri = Uri.fromFile(dbfile1);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
mContext.sendBroadcast(intent);
} catch (Exception e) {
// TODO Auto-generated catch block
// Log.i( TAG , " New Database is created " + e.toString());
e.printStackTrace();
}
// Log.i(TAG ," New Database is created " + checkDB);
}
} catch (SQLiteException e) {
Log.i("", TAG + " Data base exception : " + e.toString());
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
public boolean insertContact(String name, String phone) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("phone", phone);
db.insert("FredUserActivityLog", null, contentValues);
Log.e("contentvalues", ":" + contentValues);
return true;
}
}