Alter column name? - android

I have a table "Items" with different columns but I didn't asign a name in each column. Now I have different rows in this table and I would like to execute:
ItemsDao.queryBuilder().where().eq(Items.NAME, "computer").query();
But my column doesn't have name. I have tried to update my table with "alter table" but it doesn't work.
AnyBody know how I can add a name of my column without lost my information?
Thank you

Edit:
Sorry, now I understand the question. You are saying that you did not specify a columnName = "..." for each of your fields and you are having problems with QueryBuilder now because of this.
You don't need a columnName to get QueryBuilder to work although it is recommended. You should be able to go back to fields and add a columnName static that matches your existing schema. For example, if the field in the database is "items" then you just add the columnName = "items" to your Java field. I don't believe Sqlite is case sensitive with regards to column names but it would be good to match the case as well.
But you don't need the columnName. If you field name in the database is "items" then you should be able to do:
itemsDao.queryBuilder().where().eq("items", "computer").query();
If you edit your question to show your schema and the exception you are getting from QueryBuilder, I'll be able to respond more specifically.
I believe you should be able to do:
dao.executeRaw("ALTER TABLE `items` ADD COLUMN name VARCHAR;");
That doesn't work for you? There is a section in the ORMLite documentation about updating your schema under Android:
http://ormlite.com/docs/upgrade-schema
To quote from it, you will need to change your database version when you make schema changes and then in your onUpgrade(...) method do something like:
Dao<Account, Integer> dao = getHelper().getAccountDao();
// change the table to add a new column named "age"
dao.executeRaw("ALTER TABLE `account` ADD COLUMN age INTEGER;");
Most likely you will be keying off the database version number do if they have version 1 you do X and Y but if they have version 2 you just do Y. Something like that:
if (oldVersion == 1) {
// we added the age column in version 2
dao.executeRaw("ALTER TABLE `account` ADD COLUMN age INTEGER;");
}
if (oldVersion < 2) {
// we added the weight column in version 3
dao.executeRaw("ALTER TABLE `account` ADD COLUMN weight INTEGER;");
}
You are limited by Sqlite on what you can do with ALTER TABLE. Right now you can only rename a column or add a new column. See the docs for more details.

If you want to change the database, in your SQLiteOpenHelper you should:
change database version
override "onUpgrade" to make the changes you
want
change onCreate method, this way new users will have the good version of your database, not the old one
If there are big structural changes, in onUpgrade you copy the useful information in new database and drop the old database.
Hope it helps:)

Related

Can sugarorm automatically remove fields that are no more used?

I begin with SugarORM and i've noticed that it can add automatically fields without the need of making sql scripts (just by incrementing the version number in manifest.xml)
Example: ALTER TABLE CLIENT ADD COLUMN PRENOM TEXT before the first use of Client.Save()
Is there a setting to ask SugarORM to remove no more used fields automatically in the same way ?
ALTER TABLE SQLite
SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a column, remove a column, or add or remove constraints from a table.
In SQLite you can:
(1). create new table as the one you are trying to change (without the column to delete)
(2). copy all data (except the column you want to delete)
(3). drop old table
(4). rename the new table, to the old name.
Because android-sugarorm (sugarorm) uses SQLite underneath, it is not possible use ALTER TABLE to remove a column (field), so it is not done automatically.
See also Migrating database from previous version
SQLite FAQ

Android SQLite update column data type

How can I change the data type of one column to another?
I want to change the data type of one column form Number to Text of existing table on database upgrade.
As far as I know you cannot change it, so easily with an alter table or something like that because there are some restrictions in SQLite:
Complete ALTER TABLE support
Only the RENAME TABLE and ADD COLUMN variants of the ALTER TABLE command are supported. Other kinds of ALTER TABLE operations such as DROP COLUMN, ALTER COLUMN, ADD CONSTRAINT, and so forth are omitted.
Omitted information in SQLite
However, I have some advice based on my previous experience where I have needed to recreate tables:
To create a BackUp of the current information (storing the information in a XML or Cursor, it's just temporal). More information about cursors: Accessing Data With Android Cursors
To drop the table that you want to change.
db.execSQL("DROP TABLE IF EXISTS YourTableName");
To recreate again the table with the new structure.
db.execSQL("CREATE TABLE YourTableName");
To insert again the old information with a loop.
WATCH OUT: remember that you need to cast the column that before was a Number and now is a Text.

How to alter a table in DataBase in android application using ORMLite and retain the values

I want to alter a table in my android application which is using ORMLite. I will explain my scenario.
Let's say that someone download my app on the Google Play. Few months later I will certainly populate some tables with new entries.
When this person is doing an update of the app, how can I just alter a table in database with my new entries and keep the old ones inside it.
To be more clear, imagine that the user table is altered and i introduced a new field say 'city', how can i update the table with old values?
When i tried to back up the data from older table(which doesn't have 'city' field) with my new DAO Object with an attribute 'city', i was not able to fetch the data.
Thanks
The idea is to use the version number that is passed to the onUpgrade(...) method. With ORMLite, the OrmLiteSqliteOpenHelper.onUpgrade(...) method takes an oldVersion and newVersion number. You then can write conversion code into your application that is able to convert the data from the old format and update the schema.
For more details visits docs http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_4.html#Upgrading-Schema
Most likely, you should make your schema changes conditional to the version you are upgrading from:
if (oldVersion < 2) {
// we added the age column in version 2
dao.executeRaw("ALTER TABLE `account` ADD COLUMN age INTEGER;");
}
if (oldVersion < 3) {
// we added the weight column in version 3
dao.executeRaw("ALTER TABLE `account` ADD COLUMN weight INTEGER;");
}

Android SQLite Database: adding columns after DB is created

I'm a bit unfamiliar with updating SQLite Databases after I have created them. Is it relatively easy to add new columns and update values in the rows? Like adding an email or twitter name column to a contact list app. Or could I set up columns with the name "Col1, Col2, etc" to have extra for later and then when I want to use those columns add them into my queries/cursors in my app such that KEY_EMAIL is "Col15" or whatever.
Sqlite supports a limited subset of the ALTER TABLE command. You can rename a table or add a new column. So you can't rename or delete a column or remove constraints after you've created the table (at least not with sql-commands).
So, no need to add "spare"-columns. In fact, since you can't remove columns, that'd be a bad idea.
SQL As Understood By SQLite

Need a good method to change (SQLite) column data type

I am having a table with 'int' column. During software upgrade, I want to change it to 'long' data type. It seems SQLite does not provide an option (in alter statement) to change/modify column properties. Since I want to do an upgrade, the table might contain data which the user should not lose. So please suggest me a good method to change the column data type property without data loss.
One way suggested in the search links is to create a temporary table, copy the records from the existing table, delete the existing table, and rename the temporary table. I doubt that is efficient.
Your help appreciated!
Regards
Vivek Ragunathan
I used the follow statements to change the type of the column.
CREATE TABLE IF NOT EXISTS **TEMP_TABLE** (id integer primary key autoincrement, **col2change integer not null**, ...)
INSERT INTO TEMP_TABLE SELECT * FROM EXISTING_TABLE
DROP TABLE EXISTING_TABLE
ALTER TABLE TEMP_TABLE RENAME TO EXISTING_TABLE
I changed the int type column in the existing table to integer type. For a few hundred rows, it was reasonably fast.
SQLite3 columns do not have data types, only affinities -- there is no benefit in changing the column type from int to long.
If a wrapper program is truncating values before giving them to SQLite3, there is a way to fix this by editing the schema. It is a dangerous operation, so do it only after backing up your database. The schema is stored in the table sqlite_master; normally it is read-only, but you can modify it if you enable it with the writable_schema pragma. Be careful to only make changes that do not invalidate your data; you may change int to long int since they both have INTEGER affinity.
From SQLite documentation
It is not possible to rename a column, remove a column, or add or
remove constraints from a table.
Check this link
Please do remember that column data types are not rigid in SQLite. Check this link
Edit:
Following your comments on another answer, I guess the option you mentioned - working through the temp table - is the only one, which is not efficient off course.
you could add a new colum, copy the values form the old to the new column, delete the old column and then rename the new column to the old name
AFAIK there is no way in Android to change column data types once a table is created and used. The practiced way is to make a new table and copy the data which you read about

Categories

Resources