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).
Related
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.
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 = ?
I use this method to delete a row in my sqlite db:
db.execSQL("delete from "+TABLE_NUMS+" where _ID = '" + this.rowID + "'");
and then I update the rest of Ids to make my entries consecutive:
db.execSQL("UPDATE "+TABLE_NUMS+" set _ID = (_ID - 1) WHERE _ID > "+this.rowID);
And it works fine, but when I add new entries to my DB, the ID of the new entries still add as if the deleted entries existed, say I have 10 rows with IDs starting from 1 to 10, and then I delete number 5 and 6, the rows become 1 to 8, but the new entry's ID will be 11. So my IDs sequence would be 1 to 8 and 11. How can I fix this?
SQLite keeps track of the largest ROWID that a table has ever held using the special SQLITE_SEQUENCE table. You cam modify that sequence as:
UPDATE SQLITE_SEQUENCE SET seq = this.ID -1 WHERE name = TABLE_NUMS
The same functionality is asked in this question.
The normal ROWID selection algorithm described above will generate
monotonically increasing unique ROWIDs as long as you never use the
maximum ROWID value and you never delete the entry in the table with
the largest ROWID. If you ever delete rows or if you ever create a row
with the maximum possible ROWID, then ROWIDs from previously deleted
rows might be reused when creating new rows and newly created ROWIDs
might not be in strictly ascending order.
http://www.sqlite.org/autoinc.html
This is how SQLite works.
If you really need to have the Ids consecutive don't use autoincrement.
Insert the ids yourself.
You can select MAX(_ID) first to get the last id (greatest value).
This is because you have autoincrement set on _ID when you created the table. So, every row you add will be given a number automatically unless you explicitly set it. If it is absolutely necessary that you need the _IDs in consecutive order, I recommend that you set it yourself instead of using autoincrement.
Here is how to reset it:
delete from your_table;
delete from sqlite_sequence where name='your_table';
This will delete all your data and reset the sequence.
SQLite keeps the largest ROWID in the special SQLITE_SEQUENCE table. You can update that table as:
db.execSQL("UPDATE SQLITE_SEQUENCE SET seq = 0 WHERE NAME = '"+TABLE_NAME+"'");
OR
delete that table as:
db.delete("SQLITE_SEQUENCE","NAME = ?",new String[]{TABLE_NAME});
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()