SQLite with android not adding column - android

I read some threads about topics similar to this one however I could not find any big information on it. I am trying to add a column to the existing database table, and read from it on the content provider class, but it is not going thru, on the DBHelper class I do the ALTER TABLE as follows
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion<2) {
db.execSQL("ALTER TABLE SongInfo ADD COLUMN playcount INTEGER DEFAULT 0");
}
}
my version number is 1, and when I run it it gets the column playcount does not exist, I am trying to get the column by calling for it on contentProvider class, is there something else to it? do I need to update in another part of the code or is it suppose to accept the ALTER TABLE and just make the column readable from start of program, thanks much.

try
db.beginTransaction();
db.execSQL("...");
db.setTransactionSuccessful();

Related

how to make changes to one table in onupgrade method of database of five tables ignoring the rest in oncreate method

I have five tables in my sqlite db, the five tables are created in oncreate method if I make changes to one table in upgrade method based on the previous db version when I launched the app the changes are made I can see through my logcat but it calls oncreate method and say the 4 tables already exist. How can I handle this error?
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
if (version_old < 3) {
database.execSQL(queryFive);
}
on onCreate I have statements that creates table initially which is then called again after onUpgrade and triggering the error they already exist. How can I handle this? Thanks.
If you make changes in one table, you should increment the DATABASE_VERSION = 1,2,3 and so on.
Whenever changes are done in database, you should first drop (delete) all table and create them again.
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); // drop table(s),if you have five tables , drop all five
onCreate(db); // this will create all tables again
}
Update : in case of not loosing old data of other tables
You need an abstract class that implements the upgrade process described here. Then you extend this abstract class for each of your tables. In your abstract class you must store you tables in a way(list, hardcoded) so when the onUpgrade fires you iterate over the table items and for each table item you do the described steps. They will be self upgraded, keeping all their existing details. Please note that the onUpgrade event fires only once per database, that's why you need to iterate over all your tables to do the upgrade of all of them. You maintain only 1 version number over all the database.
beginTransaction
run a table creation with if not exists (we are doing an upgrade, so the table might not exists yet, it will fail alter and drop)
put in a list the existing columns List<String> columns = DBUtils.GetColumns(db, TableName);
backup table (ALTER table " + TableName + " RENAME TO 'temp_" + TableName)
create new table (the newest table creation schema)
get the intersection with the new columns, this time columns taken from the upgraded table (columns.retainAll(DBUtils.GetColumns(db, TableName));)
restore data (String cols = StringUtils.join(columns, ",");
db.execSQL(String.format(
"INSERT INTO %s (%s) SELECT %s from temp_%s",
TableName, cols, cols, TableName));
)
remove backup table (DROP table 'temp_" + TableName)
setTransactionSuccessful
other than that, there is not way you can achieve this. unless, you can create separate db file for each table.

How to add variables taken from a previous activity to the SQLite table as columns in android

I have already known how to create a SQL table with specified columns.
But I got this question when new variables(like names) need to be added to an existing SQL table as columns. Please note this is for android studio.
Can you guys give some advice on this?
Thanks a lot!
You can use ALTER TABLE function on your onUpgrade() method in the SQLiteHelper class, to do this.
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// If you need to add a column
if (newVersion > oldVersion) {
db.execSQL("ALTER TABLE table_name ADD COLUMN new_column INTEGER DEFAULT 0");
}
}
You may refer this link for syntax Alter Table
Besides, don't forget to increase the version number in the below code, which will be present in the constructor of the class which extends the SQLiteOpenHelper class.
super(Context context, String name, CursorFactory factory, int newVersion)
it is not ideal way to keep altering table dynamically based on user input. Instead create a flexible table that can store key value pair to solve this.
key can be the user entered variable(which you are trying to make a new column name) and value is the content you have to store.

Managed SQLite Upgrade

Android Developers have to use SQL lite for Android application, that is easy, but the problem comes when we have to upgrade the database version, we have to remove old database and create new database on upgrade, so if we want to add just one column, we have to remove all user data, is there any component or source code that manage the database upgrade, so if it only need one column, just add one column, not delete all tables.
This is completely wrong
when we have to upgrade the database version, we have to remove old database and create new database on upgrade
In your onUpgrade method, it would look something like this:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String upgradeQuery = "ALTER TABLE yourtable ADD COLUMN yourcolumn TEXT";
if (oldVersion == 1 && newVersion == 2)
db.execSQL(upgradeQuery);
}

Upgrade database. Can I alter tables or need to create new?

I'm using a database in my application which is currently on the Google Play. Now I'm releasing an update and have added a new column to an existing table. Looks like I need to use onUpgrade() method of SQLiteOpenHelper class. I'm just wondering if I need to recreate the whole table, which leads to backup/restore (which is complex) process, or I can just alter it on upgrade? How shall I handle upgrade in the code? Is it just enough to implement onUpgrade() method? Might be any good example? Really appreciate your input, thank you very much.
Yes, you can alter table . No need to create it again .
private static final String ALTER_USER_TABLE_ADD_USER_SOCIETY = "ALTER TABLE user_table ADD user_society TEXT";
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if(newVersion > oldVersion)
{
db.execSQL(ALTER_USER_TABLE_ADD_USER_SOCIETY);
}
}

How to update an SQLite Database and NOT lose all existing data?

I'm adding a table to my app's SQLite DB. All my syntax there is fine, not the issue. But I'm having some trouble getting the new table to be created properly. I added the new table....
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
db.execSQL(CREATE_REQUESTS);
db.execSQL(CREATE_OVERRIDE);
}
My on create method. I have 3 tables. When I updated the version number, I got an error saying "table requests (referring to CREATE_REQUESTS) has already been created." A look at my onUpgrade method...
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
Led me to understand that the line db.execSQL("DROP TABLE IF EXISTS contacts"), which refers to my DATABASE_CREATE table in the onCreate method, is used to drop the old table, then the next line, onCreate(db); recreates it. I did not add requests into that execSQL line, which is what caused the error. Here is the issue: I would rather not lose the data in the two tables I already have. Is there a way to add a table like I am trying to do, and not lose all the old data? Thanks.
You can do anything you want in onUpgrade. You can use ALTER to add new columns to your table.
Worst case, if your schema is completely and entirely different, you'll have to create the new table, populate it using data from the old table, and then delete the old table.
In any case, onUpgrade was designed to allow for a smooth upgrade without any loss of data. It's just up to you to implement it properly.
if DB version : 6
Ex : There is a table with 5 columns
When you upgrade to : 7 ( I am adding 1 new column in the 3 tables)
1. We need to add the columns when creating a table
2. onUpgrade method:
if (oldVersion < 7)
{
db.execSQL(DATABASE_ALTER_ADD_PAPER_PAID);
db.execSQL(DATABASE_ALTER_LAST_UPLOADED);
db.execSQL(DATABASE_ALTER_PAPER_LABEL);
}
Where : "DATABASE_ALTER_ADD_PAPER_PAID" is query.
EX: public static final String DATABASE_ALTER_ADD_PAPER_PAID = "ALTER TABLE "
+ TableConstants.MY_PAPERS_TABLE + " ADD COLUMN " + COLUMN_PAPER_PAID + " TEXT;";
After above two operation it will works fine for the fresh install user and app upgrade user

Categories

Resources