SQLite id auto-increment - android

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.

Related

SQLiteDatabase AUTO INCREMENT null

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..

SQLite is not saving my id in an android application

Simple problem which got me stuck for hours now...
When I add a value into my database via db.insert(TABLE_NAME, null, myValues) the new id of my item is returned.
Exactly after this call I'd like to update my entry. Therefore I call db.update(TABLE_NAME, values, MY_ID_NAME + " = " + returnedId, null). I'd expect to get 1 returned as one entry is affected by this update.
However, I get 0. My id is simply not saved into the database. If I try more complex queries I always get every value I need but the id, which is always 0.
I've no idea how to find my fault - I get a cursor, try to get the id by long id = cursor.getLong(0) and it will return 0.
My CreateStatement starts with:
"CREATE TABLE " + TABLE_NAME + "("+ MY_ID_NAME + " LONG PRIMARY KEY, "....
Also I need to mention that I work on an In-Memory Database for testing purposes.
What is missing? What am I doing wrong?
Also: I decided to open the database only at the beginning via Singleton-Pattern - this wasn't an issue until now
Edit2: What I also forgot to mention is that I'm currently working in an InstrumentationTestCase-environent for some unit-tests
I'm almost positive that the primary ID number in Android's SQLite must be called _id during table creation and referred to when querying the table as _ID.
so in this case:
"CREATE TABLE " + TABLE_NAME + "(_id LONG PRIMARY KEY, "....
Edit:
Try this:
"CREATE TABLE " + TABLE_NAME + "("+ MY_ID_NAME + " LONG PRIMARY KEY AUTOINCREMENT, "...
Although I am curious why you choose to use LONG instead of INTEGER
After hours of testing and debugging, Zolnoor's answer was nearly correct:
SQLite does not allow a LONG PRIMARY KEY statement but an INTEGER PRIMARY KEY as described in the SQLite-Documentation:
The data for rowid tables is stored as a B-Tree structure containing one entry for each table row, using the rowid value as the key. This means that retrieving or sorting records by rowid is fast. Searching for a record with a specific rowid, or for all records with rowids within a specified range is around twice as fast as a similar search made by specifying any other PRIMARY KEY or indexed value.
With one exception noted below, if a rowid table has a primary key that consists of a single column and the declared type of that column is "INTEGER" in any mixture of upper and lower case, then the column becomes an alias for the rowid. Such a column is usually referred to as an "integer primary key". A PRIMARY KEY column only becomes an integer primary key if the declared type name is exactly "INTEGER". Other integer type names like "INT" or "BIGINT" or "SHORT INTEGER" or "UNSIGNED INTEGER" causes the primary key column to behave as an ordinary table column with integer affinity and a unique index, not as an alias for the rowid.

Error when creating a table in SQLite database with composite primary key at AUTOINCREMENT

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.

foreign key constraint ON DELETE CASCADE not working in sqlite database on android

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.

how to get listview id in android

How can i get the id of an item (_id of almag table)? the item comes from sqlite (retrieve from sqlite) and it is showed in listview. I want to get the id and i want to use the id later (to be saved in sqlite into score table as userId).
Here is code to create table
db.execSQL("CREATE TABLE almag (_id INTEGER PRIMARY KEY AUTOINCREMENT, nama TEXT, jekel TEXT);");
db.execSQL("CREATE TABLE score (_id INTEGER PRIMARY KEY AUTOINCREMENT, score INTEGER, userId INTEGER NOT NULL, FOREIGN KEY (userId) REFERENCES almag(_id) ON DELETE CASCADE);"); //create table score
You could make a custom listview that includes a string, textview or whatever you want. You could follow this tutorial:
http://www.anddev.org/android_filebrowser__v20-t101.html
to make a list view and just remove the icons if you don't want them.
//André

Categories

Resources