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.
Related
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 working on a project, in which I'm creating a SQLite database table and it has certain number of columns. I want to include a condition that if a particular column is not null, then perform certain action (in order to fill that value up). I searched a lot, but didn't find anything. Please help.
Thanks in advance.
There is PRAGMA table_info(table_name) statement which will return for each column of table_name a result row with following informations (in separate columns):
Column name
Column type
If column can be null or not (that is if it's 0, then the column has NOT NULL constraint)
Default value for the column (will be null, unless column has DEFAULT constraint defined)
The third column would be answer to your question.
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)
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.
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()