I have a problem after upgraded to Android Studio 3.0 canary 1 from Android Studio 2.3
db.execSQL("CREATE TABLE IF NOT EXISTS " + Contract.COUNTRY_PATH + " (" +
Contract.Country._ID + " INTEGER AUTO INCREMENT , " +
Contract.Country.COLUMN_CITY_ID + " INTEGER PRIMARY KEY," +
Contract.Country.COLUMN_COUNTRY_NAME + " TEXT," +
Contract.Country.COLUMN_SUNRISE + " INTEGER," +
Contract.Country.COLUMN_SUNSET + " INTEGER) ");
It shows an error.
'(',')',<column constraint> or comma expected ,got 'AUTO'
It was fine with Android Studio 2.3.
Any suggestions.
Thanks.
Do not put any spaces in you column name.
I'm using Android Studio 3.0 beta7. Same problem still exists.
The SQL syntax checker in Android Studio 3 is stricter than sqlite itself.
INTEGER AUTO INCREMENT in sqlite itself basically just results in a column with integer affinity. The mistyped AUTO INCREMENT is just noise. That's why it gets flagged in Studio but does not cause a syntax error in sqlite.
If you want an autoincrementing column, make it INTEGER PRIMARY KEY. If you really need the rowid reuse avoidance, then the column should be INTEGER PRIMARY KEY AUTOINCREMENT. Note that you can only have one primary key column in a table. See also: https://sqlite.org/autoinc.html
CREATE TABLE yourtablename (id INTEGER primary key autoincrement NOT NULL, name TEXT, description TEXT, expValue INTEGER, category INTEGER NOT NULL REFERENCES categories (id), date TEXT)
Related
I have created a SQLite table by using query below
db.execSQL("create table " + TABLE__WORK + " ( " + ID1 + " INTEGER PRIMARY KEY ,Name TEXT, Title TEXT, Time TEXT, Date TEXT)");
The ID now can be auto-incremented. But after I perform delete query, delete the first row data and add again, the ID will start from 1 instead of 2. Is it possible to make the id become 2 ?
SQLite AUTOINCREMENT is a keyword used for auto incrementing a value of a field in the table.You do not need ID1.
See reference here
Please use this:
db.execSQL("create table " + TABLE__WORK + " (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,Name TEXT, Title TEXT, Time TEXT, Date TEXT)");
Make it INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL.
Here's what the SQLite Autoincrement document say:
If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY,
that changes the automatic ROWID assignment algorithm to prevent the
reuse of ROWIDs over the lifetime of the database. In other words,
the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs
from previously deleted rows.
I have "days" table created as follows
"create table days(" +
"day_id integer primary key autoincrement, " +
"conference_id integer , " +
"day_date text, " +
"day_start_time text, " +
"day_end_time text, " +
"day_summary text, " +
"day_description text)";
and i have tracks table created as follows
CREATE_TABLE_TRACK = "create table track(" +
"track_id integer primary key autoincrement," +
"day_id integer,"+
"track_name text," +
"track_description text," +
" FOREIGN KEY(day_id) REFERENCES days(day_id) ON DELETE CASCADE )";
as shown above i have foreign key day_id referencing to the day_id of table days...
So what i want is if i delete the day then corresponding track should also be deleted... But it does't happen in my case..
I have sqlite with version 3.5.9
And also i have added 1 line in my helper class as
> db.execSQL("PRAGMA foreign_keys=ON;");
but is still won't work.. please help me out..
Cascading delete isn't supported until Sqlite version 3.6.19, which is first included on Android 2.2.
Fortunately there is an alternative.
You can execute another query like this below your create table query:
db.execSQL("CREATE TRIGGER delete_days_with track BEFORE DELETE ON track "
+ "FOR EACH ROW BEGIN"
+ " DELETE FROM days WHERE track.day_id = days.day_id "
+ "END;");
Note that delete_days_with_track is just a name descriptive of what the trigger does, and this is just the pattern I use; I believe you could name it anything you wish.
According to the SQLite Documentation support for Foreign Keys was not added until 3.6.19.
Using 3.5.9 you'll have to do your cascade deletions in some other manner.
I am working with an SQLite database. I have a table which contain the primary keys of 2 other tables as foreign keys; I want to delete one of them. Here is the code for the table:
protected static final String Item_places=(" CREATE TABLE "
+ Item_place + "("
+ place_id + " INTEGER ,"
+ Item_id + " INTEGER ,"
+ "FOREIGN KEY("+place_id+ ") REFERENCES " + PlaceTable + "("+ PlaceID+ " ) ON DELETE CASCADE"
+ "FOREIGN KEY("+Item_id+ ") REFERENCES "+ contentTable+ "("+contentID+"));");
You would need an ALTER TABLE DROP CONSTRAINT command but SQLite doesn't support this, see How do I DROP a constraint from a sqlite (3.6.21) table? for a workaround.
This is an old question, but it's best to provide an updated answer.
Since API 16 (Aka Android 4.1), it's possible to turn on the FK constraints using SQLiteDatabase#setForeignKeyConstraintsEnabled(boolean enabled).
As per the docs:
Sets whether foreign key constraints are enabled for the database.
By default, foreign key constraints are not enforced by the database.
This method allows an application to enable foreign key constraints.
It must be called each time the database is opened to ensure that
foreign key constraints are enabled for the session.
To make this work, inside your custom SQLiteOpenHelper use the following code:
#Override
public void onConfigure(SQLiteDatabase db) {
// Enable FK constraints.
db.setForeignKeyConstraintsEnabled(true);
super.onConfigure(db);
}
I am trying to create a database with the name of a string stored in a local variable with the following syntax:
mDb.execSQL("CREATE TABLE " + FLASH_TABLE + " (" + KEY_CARD_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUESTION
+ "TEXT NOT NULL," + KEY_ANSWER + "TEXT NOT NULL);");
which produces the following error in log cat:
05-14 04:40:05.892: ERROR/Database(372): Failure 1 (near "15": syntax error) on 0x272140 when preparing 'CREATE TABLE 15 (_id INTEGER PRIMARY KEY AUTOINCREMENT, questionTEXT NOT NULL,answerTEXT NOT NULL);'.
with FLASH_TABLE being the local variable with the value "15"
KEY_QUESTION and KEY_ANSWER are also local variables
I have looked at every example online that I could find and I can not find the reason for the syntax error. I appreciate any advice you can offer.
You cannot create a table name (or a field name, for that matter) with a string that begins with a number unless you double-quote it. But even though it's technically possible, it's not considered a best practice. If you do, you will also have to double-quote the table name every time you use it in a query so you will quickly find that it's more trouble than it's worth.
I'm trying to insert a record into a SQLite database table. The table has a Unique constraint on a column "URL" and a primary key auto increment integer "_id".
I'm using insert or replace as my conflict resolution, since I need the new data to be persisted. However, this method is incrementing the primary key and messing up some foreign keys in other tables.
What's the best way to overwrite the record without updating the _id?
The simple answer is to stop using Replace syntax. This causes the old record to be deleted then a new one added ... which would increment your index.
Utilize the UPDATE syntax to handle conflicts instead
EDIT:
If you are really partial to the Replace syntax then it will come at a cost. You will need to write additional code that updates all prev occurrences of the old index to the new one. Not overly hard but this will correct the issue of synchronizing indexes
Documentation [Listed under REPLACE section little ways down the page]: http://www.sqlite.org/lang_conflict.html
this is my code of SQLite
"CREATE TABLE IF NOT EXISTS posts (" +
"_id integer NOT NULL," +
"id_language integer NOT NULL" +
");" +
"CREATE UNIQUE INDEX posts_idx ON posts(_id, id_language);";
"INSERT OR REPLACE INTO " + DB_TABLE + " (" + formulateColumns() + ") " +
"VALUES (" + formulateValues(v) + ");");