SQLite insert or replace into - sqlite

Assuming I have the Database structure with 2 colums + rowid column. Whenever i do this sql statement:
REPLACE INTO player (id,credits) VALUES ('123123',10);
However i seen for values that already exist,the data gets updated but the ROW ID gets incremented. Why is this so? Is it possible to keep the ROW ID consistent?
Thanks

with replace, the row gets deleted then reinserted, you'll more than likely have to do update

REPLACE stands for INSERT OR REPLACE, and practically, is an INSERT statement. You need to use UPDATE.

Related

How do I populate an empty column in my sql database?

How do I populate an empty column in my sql database?
I have a list that I want in the last column of my database. If I use executemany it just adds rows on the bottom of my database. Is there a way to fill it from the top? The column is currently populated with NULL values.
Just to be clear. I want the first item in the list to be put in the first row and item n on the list to be put into row n.
Thanks in advance
*Always take backup of your database, just incase you want to revert it back to original state.
Each row in your database table should have a unique primary key. As you mentioned you have a list to be uploaded in the last column, I assume you have some kind of relationship that distinguishes which list item goes to which row in the last column of the table.
You an update a single row by simple update syntax as below:
UPDATE table_name
SET last_column = somevalue_from_list
WHERE id = unique_row_id;
If you want to update all the rows with the same value then
UPDATE table_name
SET last_column = one_value_for_all_rows;
Follow the below suggestion only when you are using phpmyadmin to manage your tables:
If you want to update each row with the unique value from the list then my suggestion will be to export your table in csv format, open in excel like software and add your last column values in last column of each row and then import the changed table back to the database.
Hope this help.

SQLite Find ROWID of most recent or next ROWID

So I have a table with data about an image. The table looks something like this...
ROWID|title|description|file_path
The file path contains the name of the image. I want to rename the image to match the ROWID.
How do I get the latest ROWID? I need to also account for rows that have been deleted as I am using this as an autoincremented primary key. Because, if a row within the table has been deleted it is possible for the table to look like this...
1|title A|description A|..\fileA.jpg
2|title B|description B|..\fileB.jpg
5|title E|description E|..\fileE.jpg
7|title G|description G|..\fileG.jpg
On top of that there could be one or more rows that have been deleted so the next ROWID could be 10 for all I know.
I also need to account for an fresh new table or a table that has had all data deleted and the next ROWID could be 1000.
In summary, I guess the real question is; Is there a way to find out what the next ROWID will be?
If you have specified AUTOINCREMENT in primary key field and table is not empty this query will return latest ROWID for table MY_TABLE:
SELECT seq
FROM sqlite_sequence
WHERE name = 'MY_TABLE'
What language? Looks like the c API has the following function:
sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
http://www.sqlite.org/c3ref/last_insert_rowid.html
You could also just do:
select MAX(rowid) from [tablename];
Unfortunately neither of these methods completely worked the way I needed them to, but what i did end up doing was....
insert data into table with the fields I needed the rowid for filled with 'aaa'
then updated the rows with the data.
This seemed to solve my current issue. Hopefully it doesn't cause another issue down the road.
I think last_insert_rowid is what you want, usually.
Note that the rowid behavior is different depending on the autoincrement flag - either it will monotonically increase, or it will assume any free id. This will not usually affect any smaller use cases though.

How to DELETE a row with a GUID Value in SQLite

I have a column in SQLite of GUID type, I have tried a query like this, and it returns no error, but the row is not deleted
DELETE FROM MyTable WHERE Id='4ffbd580-b17d-4731-b162-ede8d698e026';
In SQLite Browser the Id values look like binary values, they have strange characters.
I also have tried this, but still does not work
DELETE FROM MyTable WHERE Id='{4ffbd580-b17d-4731-b162-ede8d698e026}';
I know I'm late for this, but it might just be useful for someone with the same problem.
I have a uniqueidentifier type of column in one of my tables and when I execute a select query without any conditions, it returns the result guid column values in this format -
{000B6A69-04D6-C557-7EA3-08CF8C8AD84B}
(Yes, with the braces)
I found out using typeof() function that my guid column values had been stored as text. So, I just tried out four different statements and luckily, the 4th one worked -
1. select myGuidColumn, typeof(myGuidColumn) from MyTable WHERE [myGuidColumn] = '000B6A69-04D6-C557-7EA3-08CF8C8AD84B' --didn't work
2. select myGuidColumn, typeof(myGuidColumn) from MyTable WHERE [myGuidColumn] = '{000B6A69-04D6-C557-7EA3-08CF8C8AD84B}' --didn't work
3. select myGuidColumn, typeof(myGuidColumn) from MyTable WHERE [myGuidColumn] LIKE '{000B6A69-04D6-C557-7EA3-08CF8C8AD84B}' --didn't work
4. select myGuidColumn, typeof(myGuidColumn) from MyTable WHERE [myGuidColumn] LIKE '000B6A69-04D6-C557-7EA3-08CF8C8AD84B' --it works!
Try this command. Id is a probably a binary blob field
DELETE FROM MyTable WHERE Id= X'4ffbd580b17d4731b162ede8d698e026';

How can I insert a new table row into every other row in an existing table?

Ok I have a sqlite db, that has roughly 100 rows. It is kind of a strange thing that I'm trying to do, but I need to insert a new row between each of the existing rows.
I have been trying to use the Insert statement as follows, but haven't had any luck:
insert into t1(column1) values("hello") where id%2 == 0
So I'm basically trying to use the %-operator to tell me if the id is even or odd. For every even id number, I'd like to insert a new row.
What am I missing? What can I do differently? How can I insert a new row into every other row and have the index updated as well?
Thanks
Your question assumes that the rows have some kind of built-in order to them, and that you can insert rows between other rows. That's not true.
It is true that rows have an order on disk, and that the id column is usually assigned in order, but that's an implementation detail. When you perform a query, the database is free to return the rows in any order it chooses, unless you specify what you want with an ORDER BY clause.
Now, I'm assuming what you really want is to insert rows between the existing rows in id order. One way to get what you want would look like this:
UPDATE t1 SET id = id * 2
INSERT INTO t1 (id, column) SELECT id+1, "hello" FROM t1
The UPDATE would double the ids of all the existing rows (so 1,2,3 becomes 2,4,6); then the INSERT would perform a query on t1 and use the result to insert a new set of rows with id values one more than the existing rows (so 2,4,6 becomes 3,5,7).
I haven't tested the above statements, so I don't know if they would work or if they require some extra trickery (like a temporary table) since we are querying and updating the same table in one statement. Also I may have made a syntax error.
Don't consider the rows as pre-ordered in the database. A database will store them as they come in, or according to an index. It's your task to order them on retrieval (i.e. when you query for data) according to your needs.

SQLite "INSERT OR REPLACE INTO" vs. "UPDATE ... WHERE"

I've never seen the syntax INSERT OR REPLACE INTO names (id, name) VALUES (1, "John") used in SQL before, and I was wondering why it's better than UPDATE names SET name = "John" WHERE id = 1. Is there any good reason to use one over the other. Is this syntax specific to SQLite?
UPDATE will not do anything if the row does not exist.
Where as the INSERT OR REPLACE would insert if the row does not exist, or replace the values if it does.
Another fact to notice: INSERT OR REPLACE will replace any values not supplied in the statement.
For instance if your table contains a column "lastname" which you didn't supply a value for, INSERT OR REPLACE will nullify the "lastname" if possible (if constraints allow it), or fail.
REPLACE INTO table(column_list) VALUES(value_list);
is a shorter form of
INSERT OR REPLACE INTO table(column_list) VALUES(value_list);
For REPLACE to execute correctly your table structure must have unique rows, whether a simple primary key or a unique index.
REPLACE deletes, then INSERTs the record and will cause an INSERT Trigger to execute if you have them setup. If you have a trigger on INSERT, you may encounter issues.
This is a work around.. not checked the speed..
INSERT OR IGNORE INTO table (column_list) VALUES(value_list);
followed by
UPDATE table SET field=value,field2=value WHERE uniqueid='uniquevalue'
This method allows a replace to occur without causing a trigger.
The insert or replace query would insert a new record if id=1 does not already exist.
The update query would only oudate id=1 if it aready exist, it would not create a new record if it didn't exist.

Resources