Android Question Get rowid from SQL database

andredamen

Active Member
Licensed User
Longtime User
I know how to get al the data from a sql database, but the row begins with a rowid. I want to us this rowid. How do I retrieve this field from the sql database? Is there someone who can help me with this?
 

Mahares

Expert
Licensed User
Longtime User
most(all) cases you don't need a rowid but should be using the primary key for the table.
The primary key is an alias for the rowid only if the primary key is: INTEGER PRIMARY KEY or ( Integer Primary key autoincrement). But a table can have a TEXT field as PRIMARY KEY or even a multi fields as PRIMARY KEY. In that case using the rowid is a viable way as shown in post #3
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
@Mahares, you are of course correct in your description of a primary key, but I wasn't talking directly about an alias for rowid but that in general the primary key should be used to UNIQUELY reference items in the table and rowid should be avoided.

an extract from the SQLLite documentation (i.m making an assumption that the OP is using this)


 
Upvote 0

andredamen

Active Member
Licensed User
Longtime User
Thanks for jour reply's. You can't acces and then use the rowid. I know now. I'm going to us a primary key. In ACCES you can us a auto number so that the field is always unique. By using a fieldin SQLITE as primary key I must fill that field myself in the program. I hoped that there was a way in SQLITE whit auto numbering.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
see this:


from

 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
By using a fieldin SQLITE as primary key I must fill that field myself in the program
That is not correct. You can use one of the fields and have SQLite auto fill it for you. See example below:
B4X:
strQuery="CREATE TABLE IF NOT EXISTS Country (ID INTEGER PRIMARY KEY, COUNTRY TEXT, POPULATION TEXT)"
SQL1.ExecNonQuery(strQuery)

strQuery ="INSERT INTO Country VALUES(?,?,?)"
SQL1.ExecNonQuery2(strQuery, Array As Object(Null, "Netherlands","17500000"))
I hope we are not beating a dead horse.
 
Upvote 0

andredamen

Active Member
Licensed User
Longtime User
Thanks for all the help. I made a ID field as INTEGER with UNIQUE set as true and use it as a PRIMARY KEY. And this works! Fine, thanks.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…