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
Related
I didn't find any answer to what I'm searching for so I'm sending this question.
I am using Room for Android.
I have an Entity with an Int column and I need to change it to Double, and I don't know how to do it.
Does anybody know how to do it?
My question might be dumb, but I didn't find any answer on stackoverflow/any google search.
You need to create new table with the new schema.
Copy data from old table to the new one.
Drop old table.
Rename new table to the name of old table.
Here you've got nice article about that:
https://medium.com/androiddevelopers/understanding-migrations-with-room-f01e04b07929
As mentioned in documentation for sqlite over here, updating column type is not supported in 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 colum,
remove a column, or add or remove constraints from a table.
As pointed by Sebastian M you will need to create new table with the new column added, Copy data from old table to the new one. Drop old table. Rename new table to the name of old table.
Why sqlite database has the limitation of "not to drop a column from a table"
in a single command?
Are there any chances it would be added in future versions?
ALTER TABLE DROP COLUMN is now supported, as of 3.35.0 which was released 2021-03-12.
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.
https://sqlite.org/lang_altertable.html
No, you can't.
Are there any chances it would be added in future versions?
Well, we can't tell you that. And even if they will add it, there are still a lot of Android devices out there which have an older version of SQLite without that feature.
However, you can delete a column like this from this answer:
https://stackoverflow.com/a/5987838/5457878
The ALTER TABLE statement supports exactly those operations that can be implemented easily without having to rewrite the entire table. (When adding a new column, that value is missing in all rows; it gets replaced with the column's default value when read.)
The ability to drop columns (or other things, like reordering columns) would add lots of complex code to the SQLite library.
However, SQLite is designed as an embedded database, so it must take care to conserve resources. It is very unlikely that such a large amount of code will ever be added.
http://www.sqlite.org/faq.html#q11
SQLite has limited ALTER TABLE support that you can use to add a column to the end of a table or to change the name of a table. If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.
So you can't just delete the column
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
I am trying to create one application to store information in sq-lite table.
But i want to take table information from user like table name, columns name, and number of columns, also user can edit that by table name, delete column add new column.
how can i do it.
i add example screen for table filed
Android allows you to execute arbitrary SQL strings. Construct a SQL strings from your UI and then input the string into a command similar to db.execute(string).
Reference
The Android app that I am currently working on dynamically adds columns to an SQLite database. The problem I have is that I cannot figure out a way to remove these columns from the database.
If I add column A, B, C, D, and E to the database, is it possible to later remove column C?
I have done a lot of looking around and the closest thing I could find was a solution that requires building a backup table and moving all the columns (except the one to be deleted) into that backup table.
I can't figure out how I would do this, though. I add all the columns dynamically so their names are not defined as variables in my Java code. There doesn't seem to be a way to retrieve a column name by using Android's SQLiteDatabase.
SQLite has limited ALTER TABLE support that you can use to add a column to the end of a table or to change the name of a table.
If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.
For example, suppose you have a table named "t1" with columns names "a", "b", and "c" and that you want to delete column "c" from this table. The following steps illustrate how this could be done:
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(a,b);
INSERT INTO t1_backup SELECT a,b FROM t1;
DROP TABLE t1;
CREATE TABLE t1(a,b);
INSERT INTO t1 SELECT a,b FROM t1_backup;
DROP TABLE t1_backup;
COMMIT;
SQLite doesn't support a way to drop a column in its SQL syntax, so its unlikely to show up in a wrapper API. SQLite doesn't often support all features that traditional databases support.
The solutions you've identified make sense and are ways to do it. Ugly, but valid ways to do it.
You can also 'deprecate' the columns and not use them by convention in newer versions of your app. That way older versions of your app that depend on column C won't break.
Oh... just noticed this comment:
The app is (basically) an attendance tracking spreadsheet. You can add
a new "event" and then indicate the people that attended or didn't.
The columns are the "events".
Based on that comment you should just create another table for your events and link to it from your other table(s). You should never have to add columns to support new domain objects like that. Each logical domain object should be represented by its own table. E.g. user, location, event...
Was writing this initially. Will keep it if you're interested:
Instead of dynamically adding and removing columns you should consider using an EAV data model for that part of your database that needs to be dynamic.
EAV data models store values as name/value pairs and the db structure never needs to change.
Based on your comment below about adding a column for each event, I'd strongly suggest creating a second table in which each row will represent an event, and then tracking attendance by storing the user row id and the id of the event row in the attendance table. Continually piling columns onto the attendance table is a definite anti-pattern.
With regards to how to find out about the table schema, you can query the sqlite_master table as described in this other SO question - Is there an SQLite equivalent to MySQL's DESCRIBE [table]?
As per SQLite FAQ, there is only limited support to the ALTER TABLE SQL command. So, the only way you can do is that ou can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.
Also you can get the column name from the database using a query. Any query say "SELECT * FROM " gives you a cursor object. You can use the method
String getColumnName(int columnIndex);
or
String[] getColumnNames();
to retrieve the names of the columns.