Find id of last inserted row - android

I have to simple tables in SQLite in my app and I need to insert into my tables values, but when I insert into the first I need to find value id ( primary key first column ) and that to be foreign key in second table. I know to find this with select last but is there better way to find this, id of last inserted row ?

If you're using android.database.sqlite.SQLiteDatabase then you can use insert() which returns the id of the inserted row: see here

use
SELECT last_insert_rowid()

Related

How delete row without deleting its _ID column value?

i want save number of _ID column in sqlite if its row deleted
if( deleting row in sqlite table without delete _ID column if it "_ID" declared as `INTEGER PRIMARY KEY`== true)
{How() ;}
else{do_not_vote_this_question_down();} .
thanks in advance !
Using this as a guide to how to use SQL updates in SQLite, you shouldn't delete the row, instead update the values of all other columns to be null.
The SQL to achieve this would look like the below:
UPDATE YourTable SET column1 = null, column2 = null WHERE someCondition = 'value'
Update is used because the ID column of a row represents the unique identifier of a record. If you need to keep this value, you are updating that row, as deleting the row by design will remove all references to that ID.
I question why you need to clear out the data but keep the ID - if you are looking to mark something as "deleted" but keep it for historical purposes, a column should be added called "deleted" that is default false, and then set to true (if you do need to re-access this row).

Using insert return value to retrieve that same row later

I'm adding rows using database.insertOrThrow(), how do I use the returned value, which is documented to be the row id, to retrieve that same row later?
If there is no way to do that, will insertOrThrow return an AutoIncrement key field value if I define one? If not, what is the simplest way to insert data to sqlite database and get an handle to it for later use?
The rowid is the INTEGER PRIMARY KEY column, if you have defined any. If not, you can access it with the special column name rowid:
SELECT ... FROM MyTable WHERE rowid = ?

Is 'ORDER BY _id' enough to get rows in insertion order?

I'm using SQLite DB in my Android app.
Now I need to implement data extraction in the same order in which it was inserted.
Is it enough to do SELECT ... ORDER BY _id ... if the _id column is INTEGER PRIMARY KEY AUTOINCREMENT?
Or should I add a column to store the date and time a row has been created?
You can use ORDER BY _id. Make sure you are using a List and not a Set....
No need, an id generated by AUTOINCREMENT is enough.
Yes, if you just need the order, you may use such statement or just simple "SELECT ... FROM sometable". Results will be equals.

Autoupdate of the primary key in android

I created the table using SQLiteOpenHelper the image of the table i created is given below
After this I delete the first row from the table and now my table looks like this
You can see that the id is now starting with 2.
So my problem is when I delete the first row the id first column goes to 2
Please help me to solve this issue . i want a table looks like this.
You can see that the id is now start with 2. So my problem is when I
delete the first row the id first column goes to 2 Please help me to
solve this issue
There is no problem with id value. If you are using autoincrement or not this is normal behaviour. In the case of autoincrement has internal iterator and it hasn't "memory".
So imagine this scenario: You have two rows with id 1, 2.
What will happen when you will delete first row?
In db will remain one row with id 2 but if you will insert next row, this row will be automatic have value id with 3 not deleted 1 also if you will delete first row, second row won't change value to 1.
In the other case if you are not using autoincrement this is possible(only make it programatically) but i think this approach is not effective. If you will have for example milion records, every row's id should be changed? This is sick.
So here is really not problem but normal behaviour.
Your issue is probably one of the following:
Your ID field is simply called id, whereas Android uses the conventional name _id (as a Java constant, BaseColumns._ID).
The ID field should be of type INTEGER PRIMARY KEY

Obtain "next to be used" id for a table

I have a table with 2 int type columns: one auto increment and primary, while the other is just not null.
now, whenever a record is inserted into the tale, the first auto-increment column gets a unique and new id. What I want is that this new ID value to be also inserted to the other column, in the same insert statement.
How can a achieve this? Plz help...
thnx in advance :)
I do not understand why you need in this? However, you can create the second column also with autoincrement (but you should check if the values in these two columns are the same). Moreover, you can create a trigger that will update the value of the second column after the insert (this is the best way, I guess).

Categories

Resources