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)