when i am trying to run following query in sqlite in android i am getting error
android.database.sqlite.SQLiteException: near "autoincrement": syntax error (code 1):
follwing is the table creating query , i am not be able to figure out where things are going wrong
CREATE TABLE IF NOT EXISTS template_info
( _id integer autoincrement ,
temp_contact_key integer ,
temp_text text ,
FOREIGN KEY ( temp_contact_key ) REFERENCES contact_info ( _id )
ON UPDATE CASCADE ON DELETE CASCADE ,
PRIMARY KEY ( _id , temp_contact_key )
);
SQL syntax problems:
If you want to use autoincrement, it should be primary key. Either make the _id integer primary key autoincrement and remove the last line composite PRIMARY KEY (and the , comma before it), or remove the autoincrement keyword. If you need to enforce that (_id,temp_contact_key) pairs are unique, add UNIQUE(_id,temp_contact_key) constraint.
Remove the , before ON UPDATE CASCADE ON DELETE CASCADE.
Related
I'm confused about the correct usage and implementation of the "Foreign KEY" in the SQLite Database in Android.
I created a DB with several relations, as following:
CREATE TABLE "food" (
`_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`name` TEXT NOT NULL,
`description` TEXT,
`category_id` INTEGER,
FOREIGN KEY(`category_id`) REFERENCES food(_id) )
CREATE TABLE `category` (
`_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`cagory_name` TEXT NOT NULL)
CREATE TABLE "favourites" (
`_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`id_favourite` INTEGER NOT NULL,
FOREIGN KEY(`id_favourite`) REFERENCES food ( _id ))
As you can see, there are 3 tables and the _id of "food" is the foreign key connecting the table "category" and "favourites".
Now, in Android I don't see happening ANYTHING that proves that I created such a relation among tables. Do I miss some declaration to make it effective?
How does it work in Android?
The category_id foreign key reference seems odd.
Is there a reason it's:
FOREIGN KEY(`category_id`) REFERENCES food(_id)
and not
FOREIGN KEY(`category_id`) REFERENCES category(_id)
To make sure foreign key constraints are enforced, in SQLite run
PRAGMA foreign_keys = ON;
See http://www.sqlite.org/foreignkeys.html for documentation.
I created two sqlite tables on android
phone table with primary key "id"
CREATE TABLE BLOCKED_PHONES_TABLE ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 1, KEY_PHONE TEXT UNIQUE,KEY_IS_BLOCKED BIT )
comment table with foreign key "id"
CREATE TABLE COMMENTS_TABLE ( id INTEGER, KEY_COMMENT_TEXT TEXT, FOREIGN KEY(id) REFERENCES BLOCKED_PHONES_TABLE(id))
why does the comment table don't refer id as a foreign key?
otherwise it won't have ids that are missing in the phone table.
how can I know my sqlite version?
For backwards compatibility, foreign key checking is disabled by default.
You need to call setForeignKeyConstraintsEnabled in onConfigure.
This is my SQLite table structure
CREATE TABLE tbl_district (
districtId INTEGER PRIMARY KEY not null,
districtName TEXT unique NOT NULL
);
CREATE TABLE tbl_route (
routeId INTEGER,
districtId INTEGER NOT NULL REFERENCES tbl_district (districtId) ON DELETE CASCADE ON UPDATE CASCADE,
routeName TEXT NOT NULL,
primary key (routeId , districtId)
);
CREATE TABLE tbl_city (
cityId INTEGER PRIMARY KEY,
routeId INTEGER NOT NULL REFERENCES tbl_route (routeId) ON DELETE CASCADE ON UPDATE CASCADE,
cityName TEXT NOT NULL
);
If i execute
insert into tbl_district ( districtId, districtName) values (1,'Sri Lanka')
It works as it should
But if i execute
replace into tbl_district ( districtId, districtName) values (1,'Sri Lanka')
It gives following error
Error while executing query: foreign key mismatch - "tbl_city" referencing "tbl_route"
Any idea to resolve this?
Thanks in advance!
The documentation says that
the parent key columns must be collectively subject to a UNIQUE constraint or have a UNIQUE index.
The routeId column is the parent key of the foreign key in tbl_city, but has no UNIQUE constraint.
(Your REPLACE statement is not directly related to this; it's just when the database happend to notice the error.)
I have the following tables in my sqlite android database:
CREATE TABLE MAIN (
_ID INTEGER PRIMARY KEY ASC AUTOINCREMENT,
NAME TEXT,
.....
CURRENT_CARD INTEGER
FOREIGN KEY (CURRENT_CARD) REFERENCES CARD (_ID));
CREATE TABLE CARD (
_ID INTEGER PRIMARY KEY ASC AUTOINCREMENT,
.....
Now I want to delete records from my CARD table in the following way:
DELETE FROM CARD WHERE _ID IN (10,11,12,...);
Everything works ok until I have the record in Card table being linked to Main table, ex. let's admit that Card with id 11 has the corresponding record in Main table. When deleting Card 11 I would like to set the corresponding CURRENT_CARD field for record ex. 1 in MAIN as null (I don't want to delete the whole record in MAIN, just to null one column).
How to do that in the easiest way? If possible as one sql statement...
Use ON DELETE SET NULL action with the FOREIGN KEY.
Remember to enable foreign key support with pragma foreign_keys='ON'.
I have 2 tables:
Table 1- create table table1(_id integer primary key, content text not null );
Table 2- create table table2(_id integer primary key, id_parent integer, content text not null , CONSTRAINT [FK_table2_id_parent] FOREIGN KEY (id_parent) REFERENCES table1 (_id) ON DELETE CASCADE );
But the DELETE CASCADE doesn't do anything... Anyone know what I'm doing wrong?
[EDIT:
When I try to delete rows from table1 I can, but when I look for table2 the records are all there ... even those who have the id_parent that no longer exists from Table1.]
I'm using android 2.2.
First make sure your used create table commands are syntactically and logically right.
If yes then this is due to following:
Declaring and defining foreign key does not apply them really to act.
You need to explicitly on the foreign key constraint in your database.
if (!db.isReadOnly()) {
// Enable foreign key constraints
db.execSQL("PRAGMA foreign_keys=ON;");
}