I am using sqlite db in android.
In this I need to give Id as autoincrement and second 'Incoming_sms' field as primary key. but it shows me error as below:
detailMessage "near "AUTOINCREMENT": syntax error:
CREATE TABLE TwoWayTable111 (
ID INTEGER AUTOINCREMENT,
INCONMING_MSG TEXT PRIMARY KEY,
OUTGOING_MSG TEXT,
STATUS TEXT )"
Why does this error occur? But when I give id as autoincrement and primary key, it works fine.
This is a FAQ. It works as designed. This statement returns a syntax error.
create table test (id integer autoincrement);
This one runs without error.
create table test (id integer primary key autoincrement);
You should be able to do this.
CREATE TABLE TwoWayTable111 (
ID INTEGER PRIMARY KEY,
INCONMING_MSG TEXT NOT NULL UNIQUE,
OUTGOING_MSG TEXT,
STATUS TEXT );
Related
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.
I have that query
"CREATE TABLE CAdres (id INTEGER AUTO INCREMENT,CariID INTEGER, No INTEGER, Adres TEXT,Tel TEXT,Fax TEXT, NotTanim TEXT,PRIMARY KEY (CariID, No));"
when i inside record , id is null . why null ? normal sql increment number but sqlite come "null".
To get an autoincrementing id column, the column type must be
INTEGER PRIMARY KEY
or
INTEGER PRIMARY KEY AUTOINCREMENT
Your id column INTEGER AUTO INCREMENT is just a regular column which can store NULLs. The AUTO and INCREMENT are not keywords and don't affect anything.
You can also have only one primary key per table so remove the
,PRIMARY KEY (CariID, No)
If you need the combination to be unique, you can change that to a UNIQUE constraint.
try this.. it may work..
"CREATE TABLE CAdres (id INTEGER PRIMARY KEY AUTOINCREMENT,CariID INTEGER NOT NULL, No INTEGER NOT NULL, Adres TEXT,Tel TEXT,Fax TEXT, NotTanim TEXT ,UNIQUE (CariID, No));"
note : diff between primary key and unique is that , unique can be null and primary key cannot be null..
Mycode
final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER AUTOINCREMENT, bookdir TEXT , lastaddress TEXT,addresname TEXT PRIMARY KEY(bookdir,lastaddress));";
db.execSQL(createtabBook);
Logcat:
03-05 18:29:31.708: I/System.out(17160): android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error: , while compiling: CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER AUTOINCREMENT, bookpath TEXT , lastchapter TEXT, PRIMARY KEY(bookpath,lastchapter));
i just want to create a table with composite primary key so that it is a combination of bookdir and lastaddress, also i want to make the lid as auto increment.
To get the value of any column autoincrement you need to write it or declare it as primary key.
In SQLite a column declared INTEGER PRIMARY KEY will autoincrement.
EDITED:
As you can not define more than one PRIMARY KEY in a table you have to make the bookdir,lastaddress columns as UNIQUE and define the lid columns as PRIMARY KEY as below:
Try out as below:
final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER PRIMARY KEY AUTOINCREMENT,
bookdir TEXT , lastaddress TEXT,addresname TEXT, UNIQUE(bookdir,lastaddress));";
Also add "," after the column addresname TEXT, in your query.
I am getting following output on debugger. I am not sure what syntax is missing.
The SQL code is:
CREATE TABLE weeks(Week_Id INTEGER PRIMARY KEY,
Day TEXT,
Start_Time Text,
End_Time Text,
Break_Time Text );
CREATE TABLE projects(Project_Id INTEGER PRIMARY KEY,
Name TEXT,
Description Text,
Client_Name Text,
Location Text );
CREATE TABLE timesheets(Timesheet_Id INTEGER PRIMARY KEY,
Project_Id INTEGER,
FOREIGN KEY (Project_Id) REFERENCES projects (Project_Id),
Week_Id INTEGER,
FOREIGN KEY (Week_Id) REFERENCES weeks (Week_Id));
The error boils down to:
12-09 12:34:20.782: E/SQLiteLog(6490): (1) near "Week_Id": syntax error
Try moving your FOREIGN KEY lists to after your variables are created.
CREATE TABLE timesheets(Timesheet_Id INTEGER PRIMARY KEY,
Project_Id INTEGER,
Week_Id INTEGER,
FOREIGN KEY (Project_Id) REFERENCES projects (Project_Id),
FOREIGN KEY (Week_Id) REFERENCES weeks (Week_Id));
According to the SQLite syntax (http://www.sqlite.org/lang_createtable.html) you can also write something like this:
CREATE TABLE timesheets(Timesheet_Id INTEGER PRIMARY KEY,
Project_Id INTEGER REFERENCES projects (Project_Id),
Week_Id INTEGER REFERENCES weeks (Week_Id));
This merges declarations and foreign keys.
I'm trying to create a table and I've tried so many times to figure this out... for some reason it won't accept this.. it's saying something about the auto_increment
create table if not exists Assignments(
id auto_increment primary key,
class_name VARCHAR(30),
assignment_name VARCHAR(30) not null,
due_date VARCHAR(30) not null,
notes VARCHAR(30));
whats the problem?
EDIT: i am trying to use SQLite eventually but this command was written on my mySQL thru WAMP
First of all, Android uses SQLite, so your mysql tag is slightly incorrect unless I'm missing something you're doing.
Secondly, you would say
CREATE TABLE ASSIGNMENTS(id INTEGER PRIMARY KEY, class_name TEXT, assignment_name TEXT NOT NULL, due_date TEXT NOT NULL, notes TEXT);
"autoincrement" is handled automatically if you set your primary key as an INTEGER type, even though under the covers SQLite uses strings for everything
reference: SQLite datatypes
further reference: INTEGER PRIMARY KEY
Even more reference: "If an INSERT statement attempts to insert a NULL value into a rowid or integer primary key column, the system chooses an integer value to use as the rowid automatically. A detailed description of how this is done is provided separately."
It is autoincrement, not auto_increment