how to get listview id in android - 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é

Related

SQLite id auto-increment

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.

Reset INTEGER PRIMARY KEY AUTOINCREMENT in Android

I created a table called DRINK. I inserted two rows in to it initially. When upgraded, I would like to delete both rows and reset my Primary Key to 1. I am getting the rows to delete but am not having any luck resetting the primary key. Does anybody know the syntax on how to do that or if it's even possible? Here's the function which is called from my onUpdate function:
private void updateMyDatabase(SQLiteDatabase db, int olderVersion, int newVersion){
Log.v("DatabaseHelper","UPDATE MY DATABASE");
if (olderVersion<1){
db.execSQL("CREATE TABLE DRINK (_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "NAME TEXT, "
+ "DESCRIPTION TEXT, "
+ "IMAGE_RESOURCE_ID INTEGER);");
insertDrink(db,"latte","Espresso and Steamed Milk",R.drawable.latte);
insertDrink(db, "cappuccino", "This a Cappuccino", R.drawable.cap);
}
if (olderVersion < 2){
db.execSQL("delete from DRINK");
db.execSQL("alter table AUTO_INCREMENT =1"); //*** NEED HELP HERE***
insertDrink(db, "coff", "Espresso and Steamed Milk", R.drawable.latte);
}
}
I found this piece of code online but it's not working. Thank you.
db.execSQL("alter table AUTO_INCREMENT =1");
Deleteing the rows won't reset ROWID.
SQLite documentation states:
SQLite keeps track of the largest ROWID that a table has ever held
using the special SQLITE_SEQUENCE table. The SQLITE_SEQUENCE table is
created and initialized automatically whenever a normal table that
contains an AUTOINCREMENT column is created. The content of the
SQLITE_SEQUENCE table can be modified using ordinary UPDATE, INSERT,
and DELETE statements. But making modifications to this table will
likely perturb the AUTOINCREMENT key generation algorithm.
This sql should do what you need:
UPDATE SQLITE_SEQUENCE SET seq = <n> WHERE name = <table>
Where n is the sequence you wish to set and table is your table name

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

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.

Using different IDs for multiple table in SQLITE

I am using SQLite to work on an android application. What I have learned so far is that a table made in SQLite requires a column called _id. So, for instance, to create a table, I use the following SQL Statement:
CREATE TABLE IF NOT EXISTS Semesters(
_id INTEGER PRIMARY KEY AUTOINCREMENT,
semester_name TEXT,
is_current TEXT
GPA REAL);
However, now if I want to create a seperate table in the same database, what would I name the primary key column? For instance, this is the table I want to create:
CREATE TABLE IF NOT EXISTS Classes(
_id INTEGER PRIMARY KEY AUTOINCREMENT,
course_name TEXT);
Now, both the semesters table and the courses table have a column called _id, and if I want to make a foreign key reference to the _id column in the semesters table, I will have to call it by the table name. Is there any way to make this simpler by using different names for ids?
Thanks.
For example, you make a third table and want to use both primary keys from your tables as foreign keys:
CREATE TABLE ClassSemester(
_id INTEGER PRIMARY KEY AUTOINCREMENT,
semester_id INTEGER,
class_id INTEGER,
FOREIGN KEY(semester_id) REFERENCES Semesters(_id),
FOREIGN KEY(class_id) REFERENCES Classes(_id)
);
More details in here:
http://www.sqlite.org/foreignkeys.html

Categories

Resources