I have 2 tables (master/detail): Client and Phone.
I have 1 layout with client profile and I can add several fields with phones for this client.
There are primary keys in both tables and in Phone table there are a PK_ID_Client.
I use Maps.Put and DBUtils.InsertMaps to save the record in table Client.
Save the Client table is OK, but how I can check the ID created of Client in SQLite database to add in field PK_ID_Client and save Phone table?
How I can save this information (client profile and phones) in two tables simultaneously?
I have 2 tables (master/detail): Client and Phone.
I have 1 layout with client profile and I can add several fields with phones for this client.
There are primary keys in both tables and in Phone table there are a PK_ID_Client.
I use Maps.Put and DBUtils.InsertMaps to save the record in table Client.
Save the Client table is OK, but how I can check the ID created of Client in SQLite database to add in field PK_ID_Client and save Phone table?
How I can save this information (client profile and phones) in two tables simultaneously?
I use SQLite.
ID is a autoincrement field in Client table and integer field (PK) in Phone table.
"Select max(id)..." don't work.
my code:
B4X:
Sub AddRecord
Dim ListOfMaps As List
ListOfMaps.Initialize
Dim m As Map
m.Initialize
m.Put("name", edName.Text)
m.Put("address", edAddress.Text)
ListOfMaps.Add(m)
DBUtils.InsertMaps(SQL1, "Client", ListOfMaps)
End Sub
How I can get ID (autoincrement) of this record right after insert?
How I can add the new field "phone" (Phone table) after DBUtils.InsertMaps in Client table?
SQLite works in a strange manner. If you have a field declared as INTEGER PRIMARY KEY it will be a "copy" of ROWID (which is an internal field).
ROWID has some aliases and I remembered also _id (perhaps _ID).
SQLite works in a strange manner. If you have a field declared as INTEGER PRIMARY KEY it will be a "copy" of ROWID (which is an internal field).
ROWID has some aliases and I remembered also _id (perhaps _ID).
You could use my SQLExtended library. That wraps the insert method from the API which returns the ROWID. The advantage being it is faster as the API uses the
sqlite3_last_insert_rowid function to return the ROWID so you are only running one SQL statement not two.
Not the same thing. Only one SQL statement is executed.
B4X:
iRowId = oSQLE.insert("atable",cInsertStatement)
You solution uses one statement to do the insert and one statement to get the RowID. The API calls the sqlite3_last_insert_rowid function directly and is not wrapped in a SELECT statement so it is faster because there is no overhead in executing a second SQL statement.