How to retrieve last _ID from SQLite - android

I have a "logic" problem when making "inserts" on the SQLite database of my app.
These are my tables.
How should I manage the "need" of retrieving contact_id and photos_id in order to make the insert into the table contact_photo?
I know that I can get the last _id generated by SQLite with SELECT seq from sqlite_sequence where ..., but this doesn't seem a "professional" solution.
Is there a better way to achieve my goal?
Thank you in advance

have a look to this document
long insertOrThrow (String table,
String nullColumnHack,
ContentValues values)
will returns row ID of the newly inserted row, or -1 if an error occurred

Related

Get newly added row in SQLite table

In my android application, I have a SQLite database containing a table with an id column with AUTOINCREMENT. I'm using an INSERT statement to add a new row but I need to immediately access this row so I can refer to it by the ID. So does anyone know of a way to get the ID of this newly added row?
Thanks!
SQLiteDatabase.insert returns the id of the newly created row.
So, you would get the row like so:
long row = mDatabase.insert(MY_TABLE, "id", values);
(above of course is just an example)
See here: SQliteDatabase.insert
Returns
the row ID of the newly inserted row, or -1 if an error occurred
I think you can use last_insert_rowid() function
From documentation
The last_insert_rowid() function returns the ROWID of the last row insert from the database connection which invoked the function. The last_insert_rowid() SQL function is a wrapper around the sqlite3_last_insert_rowid() C/C++ interface function.
Try this out:
select id from myTable where id = (select max(id) from myTable);
When you insert a row, the long value which is return is the id of inserted row : see
public long insert (String table, String nullColumnHack, ContentValues values)
Copy past link :
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert(java.lang.String, java.lang.String, android.content.ContentValues)

SQLite insert record with _id value for two or more fields

I have a case that I would like to insert record in SQLite with database.insert(table, null, values).
TABLE t2 (_id, field1, field2)
..
val.setVal1(null);
val.setVal2(val2);
..
if(val.getVal1==null){
values.put(field1, _id);
}else{
values.put(field1, var.val1);
}
values.put(field2, var.val2);
database.insert("t2", null, values);
Is possible to do sth like this "values.put(field1, _id);"?
_id is generated at database.insert().
Note: I am looking for solution for one insert call. Insert and update row with (field1=_id) is easy.
i think i see now. you're asking if you can enter a value into a specific SQLite row _id field if it's available in your val object. Else, you want the database to automatically create a unique id for that column while inserting, like normally done. Is this correct?
To that end, i would seriously reconsider this purpose. You should never be specifying values for the _id column because it needs to be unique or else you'll get exceptions thrown. Moreover, it's only purpose is to be a unique identifier for the system, so you personally knowing this value should be of no use to you.
If you still need this functionality, i'd suggest making another field in your table (much like the _id column but not it), which you can fill with randomly generated numbers or val.getVal1 values.

How to check if a row has been updated and not inserted in android sqlite3

I have a database which i am inserting to. Right now I have this code.
database.insertWithOnConflict(FOO_TABLE, null, values,
SQLiteDatabase.CONFLICT_REPLACE);
This inserts into my database. Is there an efficient way to see if a row has been updated and not inserted with the constraint CONFILCT_REPLACE? Thanks!
You can compare returned _id with previously retrieved max(_id) - if _id > max(id) record was inserted, otherwise replaced.
Other way is a bit dumb - try to insert, in case of sql exception make update.

Sqlitedatabase cursor getString related

I have a sqlitedatabase which has table lets say "ABC" and this table has a column lets say "X". While creating the table i mentioned the data type of column X to "text". And i store current milliseconds time in this column. But the problem is mentioned below.
I get the cursor by running the query on this table "ABC"
I check the cursor that it has got rows.
When i try to get the value of column "X" by using cursor.getString method then it throws the numberFormatException.
please suggest any reason for this...
Thanks
Dalvinder Singh

keep the _id of a record in the database

I have a databse with three tables.
The first table is something like:
_id user
The second table:
_id route user_id
and user_id is exactly the _id from the first table.
So when I insert in the first tabel a new record,I should keep the _id in order to insert it
in the second table.But how could I keep something that is autoincremented and given by the database?:-S
The SQLiteDatabase has an insert() method, which returns the _id you are looking for.
There may be a better way, but you could grab (query for) the _id field right after you do your commit to the first table. Then you will have the user_id for use in the second table.

Categories

Resources