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)
Related
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
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).
I'm using Ormlite for database operations on my Android App.
In one of the methods, I need to delete the last row in a table. However I m not able to find out how. I'm successfully deleting rows when giving an argument, but in this specific case I don't have arguments.
Here's my attempt:
LottoDatabaseHelper helper = OpenHelperManager.getHelper(getApplicationContext(), DatabaseHelper.class);
//You get helper
Dao dao = helper.getDao(MyTable.class);
//get your Dao
DeleteBuilder<MyTable, Integer> deleteBuilder = dao.deleteBuilder();
//How can I specify last row here?
deleteBuilder.delete();
Thank you for your help
I haven't used Ormlite .. here is a simple solution which can use with any database ... As you said you can delete a row by passing argument...
try this..
1. Find the last id using the query..
eg. SELECT id FROM table_name ORDER BY id DESC LIMIT 1
// will return the last row id
2. then pass the id as a parameter to delete ...
Here is my table schema:
downloadstbl
_id int not null auto
url text not null
filename text not null
date text not null)
I would like to insert a row only if there isnt a row in the table already with the same url.
E.g, if the following row exists:
_id=1
url="http://google.com/img.jp"
filename="img.jp"
date="11/03/2012"
then if I try to insert another row that has url="http://google.com/img.jp" the sql statement will not insert to avoid duplicate rows for the same remote file.
I know I could probably do this by first doing a SELECT and checking for whether a row already exists, however I would like to check if this is possible at the point of insertion to make things more robust/clean.
You can (and should, for data integrity reasons) add a unique constraint to the url column by adding UNIQUE(url) to the SQL create statement for your table or just adding unique after not null. This will cause an insertion to fail when inserting a row where the URL is already in the table. Then you can use insertWithOnConflict to check for the result of the insert.
First of all, I would add the UNIQUE keyword to the url column.
Then, when inserting, use this INSERT OR IGNORE instead of INSERT.
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()