I am working on sqlite in my android app.
I am doing db.getVersion()
to get current version of my database.
it is showing 3
in logcat.
even in constructor i set 4
as a version number.
public Helper(Context context)
{
super(context, DATABASE_NAME, null,4);
}
i am giving 4
it should take 4
as version number.
but its not behaving like this. it is showing 3
as version number.
please any suggestion.
UPDATE:-
public class Helper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "helper.db";
public static final String TITLE = "title";
public static final String AUTHOR = "author";
public static final String ISBN = "isbn";
public Helper(Context context)
{
super(context, DATABASE_NAME, null,4);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL( "CREATE TABLE book1(_id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT, author TEXT,isbn INTEGER);");
Log.v("Create Table", "CREATE TABLE book1(_id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT, author TEXT,isbn INTEGER);");
Log.v("version ", "Version number is "+db.getVersion());
}
2020-01-20 - Release 3.34. SQLite version 3.34. 1 is a patch releases that fixes a possible use-after-free bug that can be provoked by malicious SQL.
The sqlite. version is the version of the pysqlite 2.6. 0, which is the binding of the Python language to the SQLite database.
You are checking the version number in the onCreate function, why? the OnCreate function should only be called the first time the datbase is created from scratch, in this case it should build the DB as you require fitting to the version you are using.
Can you try checking the DB version after the DB has been created and you ask to get a readable or writable instance of it?
SQLiteDatabase db = new Helper(context).getWritableDatabase();
db.getVersion(); // what value do you get here?
check out the source code of android 4.1.1 SQLiteOpenHelper, here it seems as the db version should be 0 inside the onCreate function, I guess it might be implemented in another way on the version you are testing on. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/database/sqlite/SQLiteOpenHelper.java#242
It seems like the db is already created by previous version of your code, try uninstalling the app from the device you're testing on , then re-install. this should give you version = 4
You have to drop the table inside oldversion database, override this function
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS book1");
onCreate(db);
}
that should be it.
to avoid data loss refer to this tutorial
http://denverdroid.blogspot.com/2010/04/how-to-non-destructibly-upgrade-your.html
or this
How to update an SQLite Database and NOT lose all existing data?
I think you're providing the wrong constructor for your Helper class. Try this:
public Helper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
Your current constructor is telling Android that it is already at version 4 and there is no need to upgrade it.
Also, you need to provide an onUpgrade method as well.
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
// upgrade code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With