I updated my application today and started seeing the following in crashlitics:
android.database.sqlite.SQLiteException: no such table: LESSONATTACHMENT (code 1 SQLITE_ERROR[1]): , while compiling: SELECT _THUMB_PATH FROM LESSONATTACHMENT WHERE _id = 2
I create a new table inside my SQLiteOpenHelper, it was not in the application before I update.
This is how I created the table:
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_LESSON_ATTACHMENTS);
}
Should I instead place it in OnUpgrade? Like this:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(CREATE_TABLE_LESSON_ATTACHMENTS);
}
Whenever you want to make changes in tables or create a table on app update, you have to change the version of database. In your case let us suppose you have upgraded from dbversion from 2 to 3 and you want to create a table for versions greater than 2.Then you have to create a table in onUpgrade like below:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion>=3){
db.execSQL("CREATE TABLE IF NOT EXISTS `TABLE_NAME` ....");
}
}
here I am creating the table for all the versions greater than 2 since there is a possibility that dbversion gets upgraded from 2 to 4 or some other version greater than 3.
You Need to increase your Database Version and create the new Table in onUpgrade and Maybe delete the old Table.
Your Database is created only once. Upgrades are done in the onUpgrade function
You can also delete the Database and it will be created new with the new Table, but it depends if you are able to do this at the Moment.
for example this Code will upgrade your Database up to the current Version
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch(oldVersion){
case 1:
//Changes between version 1 and 2
case 2:
// Changes between Version 2 and 3
case currentversion:
//changes between last change and now
}
}
when you update your database just put a new case with the new Version below this. This way you can maintain every Version of Database and make sure that it will be upgraded properly. Important is, that you don't use a break after each case.
this Code Shows for example one of my Databases, when I increase the Version I simply add a new case for the old Version. When you havent upgraded your Database yet, set the new Version to 1 higher than your old Version and add a case clause for the old Version.
public class database extends SQLiteOpenHelper {
private static String DATABASE = "Database";
private static int VERSION = 4;
public database(#Nullable Context context) {
super(context, DATABASE, null, VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE DATABASE TABLE");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch(oldVersion){
case 1:
db.execSQL("CHANGES DONE AFTER VERSION 1");
case 2:
db.execSQL("CHANGES DONE AFTER VERSION 2");
case 3:
db.execSQL("CHANGES DONE AFTER VERSION 3");
}
}
Related
I am developing a database application and I am stuck up at one place.
How to upgrade external database after adding new table in Database in android sqlite?
after updating my app I got error: No such table found in database
In SQLiteOpenHelper class
public abstract void onUpgrade (SQLiteDatabase db,
int oldVersion,
int newVersion)
Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.
You have to Update the Version Number
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(){
super(context,DATABASE_NAME,null,VersionNumber);// Change Version here
}
public void onCreate(SQLiteDatabase db) {}
//While upgrading pass new version
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {}
}
Example
How to use onUpgrade() correctly
I want to add new columns to a SQLite database, but I already released my app on the Play Store so, if I edit it, users need to uninstall and reinstall the app,
but I don't want that. Please, help, I am new to Android.
(1) Increment (or simply change) your database version
(2) It would lead to onUpgrade() method call
(3) Execute your query(for adding a new column) in the onUpgrade() method.
The Right Way of doing is mentioned here in this blog.
Sometimes user may update the version 1.5 from the version 1.1.In other words, They may skip the other versions between the 1.1 to 1.5. You might changed database couple of time between 1.1 to 1.5. So in order to give the user a benefit of all the database changes, you need to take of onUpgrade() method as below.
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
db.execSQL(DATABASE_ALTER_TEAM_1);
}
if (oldVersion < 3) {
db.execSQL(DATABASE_ALTER_TEAM_2);
}
}
Hope it helps.
You need to changes in following method of Database Class
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "ALTER TABLE " + TABLE_SECRET + " ADD COLUMN " +
"name_of_column_to_be_added" + " INTEGER";
db.execSQL(sql);
}
In my Android application I'm using the SQLite DB with SQLiteOpenHelper
I have several DB versions, and I've been doing onUpgrade() operations by switching on the old db version, but now I have to remove one of the tables because I'm no longer using it, so should I do something different?
public void onCreate(SQLiteDatabase database) {
database.execSQL(tableUserCreate);
database.execSQL(tableProductsCreate);
database.execSQL(tablePicturesCreate);
}
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
switch (version_old) {
case 1:
database.execSQL(addPostcodeFieldToUserTable);
database.execSQL(tablePlacesCreate);
// Intentional fallthrough, no break;
case 2:
database.execSQL(tableProductVideosCreate);
break;
}
} // End of onUpgrade
Now I want to remove the User table in a new DB version. What do I do?
SQL for dropping a table:
DROP TABLE table_name
Or use:
DROP TABLE IF EXISTS table_name
When you change Database_Version it called
onUpdate(SQLiteDatabase db, int oldVersion, int newVersion)
method first, so you have to do all drop or create kind of stuff inside that method. In your case you use Switch case i do same things using if else.
But both work fine in that case.
if (newVersion == yourNewVersion) {
try {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + YOUR_TABLE_NAME);
// Create tables again IF YOU WANT TO CREATE HERE
} catch (Exception e) {
e.printStackTrace();
}
I am developing a database application and I am stuck up at one place.
The scenario is: I have a database and I am maintaining the version of the database.
It is the same as that of the application version.
Now I am implementing the database in version 1. There are 3 tables getting created in version 1. In version 2, I am upgrading the database by adding one more table. Hence, the query is in the upgrade().
Now, what if the user installs version 2. The onUpgrade() will not get called because there is no database. Hence, the onCreate() will be called and the consequence will be it will create only 3 tables.
I was thinking to call the onUpgrade() explicitly in the onCreate(). But many developers on stackoverflow have suggested not to call it explicitly.
I am completely stranded.
Thanks in advance.
I know it's an old post, you have certainly found a solution, but I find the question valid (upvoted) and as it did not receive an answer, here are my 2 cents:
Calling onUpgrade() from onCreate() should work (I did not test it), but there is a better way to organize you helper (see snippet hereafter). The key point is that both those methods are just entry points.
public class SampleDBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "mydatabase";
private static final int DB_VERSION = 2;
public SampleDBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
updateDatabase(db, 0, DB_VERSION);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
updateDatabase(db, oldVersion, newVersion);
}
private void updateDatabase(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 1) {
// Create 3 initial tables
}
if (oldVersion < 2) {
// Create the new table
}
}
}
The advantages of this kind of helper:
You never need to change onCreate() and onUpgrade() methods, only updateDatabase().
For each new version, you just need to :
increment DB_VERSION
add a new section if (oldVersion < N) { // do the upgrade }.
Credit : Strongly inspired from Head First Android Development, Dawn and David Griffiths, O'Reilly, 2015
I add one more table to my app's database.
However, if I upgrade my app without clearing its data, the new table doesn't seem to be added automatically.
Therefore, some code that need to retrieve data from the new table fail.
Is it possible to add new tables automatically on app upgrade?
Please implement onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) method.
There is an Android helper class to manage database creation and version management using an application's raw asset files.
Have a look at this :
https://github.com/jgilfelt/android-sqlite-asset-helper
Yes there is a method onUpgrade
add you code there and increase database version if your version is higher than the previous
onUpgrade is fired and the your code will execute which you write to add table
sample
Remember increasing you version code in super call
class Database extends SQLiteOpenHelper
{
public Database(Context _myContext)
{
super(_myContext, DB_NAME, null, DB_VERSION_CODE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if(oldVersion > newVersion)
{
// create a new table what you want to create you can also check the existence of the table
}
}
}