I am migrating a Room database in my Android app. This is the migration code:
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
#Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE item RENAME itemInfoId TO itemId");
}
};
The error message
android.database.sqlite.SQLiteException: near "itemInfoId": syntax error (code 1 SQLITE_ERROR): , while compiling: ALTER TABLE item RENAME itemInfoId TO itemId
I have also tried the SQL of "ALTER TABLE item RENAME COLUMN itemInfoId TO itemId", same error
android.database.sqlite.SQLiteException: near "COLUMN": syntax error (code 1 SQLITE_ERROR): , while compiling: ALTER TABLE item RENAME COLUMN itemInfoId TO itemId
Rename keyword is available in SQLite version 3.25 which is not available for the latest android version. You will have to manually upgrade the table
1. Create item_tmp table with correct column value itemId
CREATE TABLE item_tmp(<column1> <data_type>, itemId <data_type>,.....)
2. Copy the data from item to item_tmp
INSERT INTO item_tmp(<column1>, <column2>,..)
SELECT <column1>, <column1>, ...
FROM item;
3. Drop table item
DROP TABLE item;
4. Rename the item_tmp table
ALTER TABLE item_tmp RENAME TO item;
i have faced same problem while using RENAME keyword in Sqlite.
it gives error in below android 10 device and working perfectly in android 11 and above.
Related
I added the below code for adding a column to the SithluBody table when migration.
static final Migration MIGRATION_3_4 = new Migration(3, 4) {
#Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE SithaluBody ADD COLUMN has_thumb INTEGER NOT NULL
DEFAULT 0");
database.execSQL("ALTER TABLE SithaluBody ADD COLUMN reacts TEXT");
}
};
but I got the error ==>
Fatal Exception: android.database.sqlite.SQLiteException
duplicate column name: has_thumb (code 1 SQLITE_ERROR).
How can I fix it? please help me.
This can be one of the two problems:
You have the has_thumb column added.
You installed an older version in your device/emulator that had that column name, and now that you are trying to do this migration, it's having a conflict.
In case of the second option, try to uninstall or clear the data of you app in the device/emulator and try to install the app again.
I have a ContentProvider backed by an sqlite table. So to create my table I used
public class H extends SQLiteOpenHelper {
#Override
public void onCreate(SQLiteDatabase sqliteDatabase) {
…// here I defined my original table without constraints
}
}
When originally created, the table had the columns: name, age, height. No Constraints. Nothing.
Now I need to add constraints to the table. So I increased the DATABASE_VERSION, and then in the onCreate String I added UNIQUE(name,age) ON CONFLICT REPLACE.
My question is, what should I do inside the onUpgrade method? Stated more simply: How do I call ALTER TABLE just for adding constraints? My attempt failed
ALTER TABLE myTable ADD UNIQUE(name,age) ON CONFLICT REPLACE
Here is the error message:
Caused by: android.database.sqlite.SQLiteException: near "CONSTRAINT": syntax error (code 1): , while compiling: ALTER TABLE myTable ADD CONSTRAINT UNIQUE(name,age) ON CONFLICT REPLACE
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(near "CONSTRAINT": syntax error (code 1): , while compiling: ALTER TABLE myTable ADD CONSTRAINT UNIQUE(name,age) ON CONFLICT REPLACE)
Alter table is rather limited in sqlite. However for your purpose CREATE UNIQUE INDEX works just as well.
Generally there is no difference between creating a unique constraint vs creating a unique index. Refer:
SQLite - Any difference between table-constraint UNIQUE & column-constraint UNIQUE? https://dba.stackexchange.com/questions/144/when-should-i-use-a-unique-constraint-instead-of-a-unique-index
So in your case change your code to use should change the code in your onCreate to use CREATE INDEX syntax instead.
Please be aware that this may still fail if your table already has duplicate entries.
You cannot add a constraint with an ALTER TABLE in SQLite.
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.
http://www.sqlite.org/omitted.html
I'm trying to write a simple query that fetches all the columns of a specific table, but also adds a count of rows from a related table.
Here is the query:
SELECT
todos.*,
(SELECT COUNT(assets.*) FROM assets WHERE assets.parent_id = todos._id) AS asset_count
FROM todos
Here is the error:
Caused by: android.database.sqlite.SQLiteException: near "*": syntax error (code 1): , while compiling: SELECT todos.*, (SELECT COUNT(assets.*) FROM assets WHERE assets.parent_id = todos._id) AS asset_count FROM todos
try replace COUNT(assets.*) for COUNT(*) or COUNT(assets._id)
my second suggestion, try running the inside select like a single one
SELECT COUNT(*) FROM assets WHERE assets.parent_id = 1 <-- any _id you have
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I rename a column in a SQLite database table?
I want to rename field table in SQlite, but I can't rename it in my aplication. This my query to rename:
ALTER TABLE 'table_name' RENAME 'column1' TO 'new_column1'
but my query gets this error:
SQLiteManager: Likely SQL syntax error: ALTER table 'table_name'
RENAME 'column1' TO 'new_column1' [ near "'column1'": syntax error ]
Exception Name: NS_ERROR_FAILURE
Exception Message: Component returned failure code: 0x80004005
(NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]
and can't rename field...
i change my query :
ALTER TABLE 'table_name' RENAME COLUMN 'column1' TO 'new_column1'
ALTER TABLE 'table_name' CHANGE 'column1' 'new_column1' TEXT
and error again...
how do I solve my problem??
You can't directly rename a column in a SQLite table. You need to re-create the table. The overall process is this
1.- create a new table with the new column
2.- copy all the old table contents to the new table
3.- recreate all indexes
4.- rename the old table to another name
5.- rename the new table to the original name
I tried deleting a column by using the following
openDB.execSQL("ALTER TABLE favs" + " DROP COLUMN favsCount");
LogCat gives the following message:
11-07 21:18:29.238: ERROR/Database(13952): Failure 1 (near "DROP": syntax error) on 0x34e550 when preparing 'ALTER TABLE favs DROP COLUMN favsCount'.
Is it not possible to delete fields in sqlite for Android?
Sorry, SQLite doesn't support DROP COLUMN:
(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. [...]
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;
So basically, you have to use the "copy, drop table, create new table, copy back" technique to remove a column.
as mu is too short says Sqlite doesn't allow to do an alter table to delete a column. here you can see the alter syntax definition