I want to implement delete column in Android's SQLite. I want to make table copy without desired column, than delete former tablet and set name at new_one as a former name. I have made table to table copy, but now I have it as a 1:1 copy. How to change columns when copyiing?
Goal is to have method deleteColumn(int indexOfColumn);
Thanks
drop your new table first,then create a new table directly with desired columns as follows:
create table new_table as select column1,column2,....from old_table;
Here select those columns that you want to see in new table.Then drop old table and rename new table to old table's name.Hope it will work.
Related
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
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.
I read data from an external database and I want to delete the contents of an entire column from it.
How can I achieve this?
If you are working on the Sqlite database then add this in your Database Class File.
From: http://www.sqlite.org/faq.html:
(11) How do I add or delete columns from an existing table in SQLite.
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;
Simply UPDATE the column value with its default value (i.e. '' for a text column, 0 for an integer, NULL for a nullable, ...), specifying no condition (no WHERE clause).
This will replace ALL the values on that column with a default value.
I use SQLite database in Android app, and i want to know how to add a unique column to already created table.
by this i mean that the exist table uniqness determine by 2 values and i want to edit the table uniqueness to be determined by 3 values (the 2 exist and 1 more).
is it possible?
The writable_schema trick does not work for UNIQUE because the internal index would need to be changed.
The only way to make this change is with a temporary copy of the table:
CREATE TABLE NewTable(
[...],
UNIQUE(Col1, Col2, Col3)
);
INSERT INTO NewTable SELECT * FROM MyTable;
DROP TABLE MyTable;
ALTER TABLE NewTable RENAME TO MyTable;
(This should be wrapped in a transaction; Android's onUpdate does this automatically.)
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