We have a sqlite database in our Application. Its working fine for all the users but few of them experiencing the Caused by: android.database.sqlite.SQLiteException: n
I have had the same issue, when I created an update for app. By google documentation onCreate() from SQLiteOpenHelper is called only when the app is installed first time. So if you want to add a table you need to increment the database version and add
onUpgrade() { your_table.create(database) }
@Override
public void onUpgrade(final SQLiteDatabase database,
final int oldVersion,
final int newVersion) {
YourTable.onCreate(database);
}
Another possible solution is just uninstall an App from the Android emulator and after this run it again.
If you just want to remove a application:
1.Start the emulator. 2.Open the Android settings app. 3.Select "Applications" (Called "Apps" on Android 4.0 or higher) 4.Select "Manage Applications" (Only on Android 3.2 or lower) 5.Select the application you want to uninstall. 6.Click "Uninstall"
Even if you have coded for few tables and upgraded database, your app will crash. What you have to do is for each and every table create database changes upgrade the DATABASE_VERSION
If you have few DatabaseHelper classes(read this), you have to add this method to your db helper:
@Override
public void onOpen(SQLiteDatabase db) {
onCreate(db);
}
My scenario is different:
I am using the Room
's createFromAsset() to switch between a couple of database files, achieving that requires to first delete the database and recreate it again to be loaded with the other database file..
During the deletion process, the LiveData
that I observe from the database raises SQLiteException: no such table
and of course it's logical, because the LiveData
senses that the underlying data is changed, and tries to get the new data, and finds that the table is gone.
To solve this: either use normal data (no LiveData
) or stop observing the LiveData
and re-observe it again when the database is recreated successfully.
Note: without deleting the database file, Room continues to use a cached version of the old database file.
it's an upgrading exception. Make sure you have the table in your previous database. If not, create it. PS: if you're newly dev this app, uninstall it from your emulator or your device and re-install. But its not recommended for the data will ne lost.
This error Occurs Because you are not using DATABASE_VERSION
public class DatabaseHelper extends SQLiteOpenHelper {
private static SQLiteDatabase sqliteDb;
private static DatabaseHelper instance;
private static final int DATABASE_VERSION = 1;
Increase Your version every time you make changes in your database just increase DATABASE_VERSION with +1..