SQLite: difference between 'INSERT OR REPLACE' and only 'REPLACE' - sqlite

I've found many discussions about INSERT OR REPLACE and I thought it was clear to me, but after reading this article saying:
The following illustrates the syntax of the REPLACE statement.
INSERT OR REPLACE INTO table(column_list)
VALUES(value_list);
Or in a
shorter form:
REPLACE INTO table(column_list)
VALUES(value_list);
I'm afraid I missed something.
Why is the INSERT statement needed together with REPLACE?
Does it actually make any difference in examples like this one, for instance?

When in doubt, consult the documentation (emphasis mine):
The REPLACE command is an alias for the "INSERT OR REPLACE" variant of the INSERT command. This alias is provided for compatibility other SQL database engines. See the INSERT command documentation for additional information.

Related

RowCount,Table must Exist, Delete All Rows from Table keywords from Robotframework

I am new to robotframework and I am trying to get the hang of the keywords of DatabaseLibrary. I am getting error at 3 of such keywords.
1) I am using rowcount keywords as below-
${rowCount} Row Count <sql query>
And I always get ${rowCount}=0 irrespective of the number of rows in my table.
2) I am using Delete All Rows From Table as below-
Delete All Rows From Table <Table_Name>
And I get ORA-00911: invalid character but if use the same table with other keywords like Query ,it works fine.
3) I am using Table Must Exist as below-
Table Must Exist <Table_Name>
And I get ORA-00942: table or view does not exist but this table is very much there.
Please help me find what am I doing wrong.
Thanks in Advance!!!
I could be wrong but I believe a colleague told me there were issues, at the very least with the Row Count keyword.
However, for all three options there are easy solutions, which you've even hinted at in your question by using Query or Execute SQL Script
1)
${result}= Query Select count(id) from table
${rc} = ${result[0][0]} #Play with this as I forget exact syntax
2) Put your delete script in a test scripts folder with your tests and call it using Execute SQL script. You could also use Query to perform a select query before and after to confirm expected states.
3) Again perform a query against the table you're expecting to be there, a simple row count on id would do for this purpose. You could set a variable based on the result and use this again later if required.
I had similar issues.
I use cx_Oracle.
With the Table Must Exist keyword my problem was the same.
I dont really understand why, but first I have to use Encode String to Bytes keyword.
And I need to use a DatabaseLibrary 0.8 at least, because earlier versions didnt have solution for cx_Oracle. These solved this issue for me.
But with Delete all rows from table I still have problems.
Because this keyword puts a ; at the end of the line and it passes on that line to execute query if I understand weel, so it still causes an ORA-00911 error for me.
With Execute Sql String and the command DELETE FROM tablename you can have the same results, but it will work this way.
I hope it helps a little

UPDATE statement not working in sqlite

I'm using the following command to update a field in my database:
UPDATE Movies SET 'From'=2 WHERE 'Name'="foo";
I'm using sqlite3.exe in windows (command prompt). Although no error message is produced, nothing changes in the table. I examined the database with a couple of gui tools and I'm sure UPDATE does nothing.
'From' is of type integer and 'Name' is text.
The problem you've got is that you're getting your quoting wrong. SQLite follows the SQL standard here, and that specifies what quote characters to use: '…' is for strings, and "…" is for tokens (like special names used as column or table names). Sometimes it manages to guess what you mean anyway and compensate for getting it wrong, but it can't with the WHERE clause because that is syntactically correct (if decidedly unhelpful):
Swapping the quoting appears to work:
UPDATE Movies SET "From"=2 WHERE "Name"='foo';
Those aren't good column names. Both are keywords, best avoided, and not self-explanatory at all.
Do a SELECT to see how many rows match the WHERE clause in the UPDATE. If none come back, you have your answer.
Did you commit the UPDATE? Is auto commit turned on?

sqlite3 square brackets in a select statement

In the following piece of code, what is the purpose of the square?
SELECT Value, [Default] AS Selected FROM SKUOptVal WHERE SKUOptID = ?
Cheers.
This code is written for SQLite3.
It's an identifier. It's the same as saying "Default". It's included for compatibility with MS Access. Since Default is a keyword in SQL, it needs to be quoted if used as an identifier, as it is here.
The column is named default, which is the same as an SQL keyword. Thus, the brackets are used to denote we are referring to the column default and not the keyword default.

Sqlite delete query error

delete N.* from Tbl_Nodes N, Data_Tree DT WHERE N.Part = DT.Part
for this command I am getting following error.
System.Data.SQLite.SQLiteException: SQLite error near "N": syntax error
Above command works fine for MSAccess.
Is there any alternative to use table shortcut in Sqlite?
The DELETE statement operates on a single table and does not use a table alias. Therefore, your FROM clause must read FROM Tbl_Nodes.
You're probably looking for:
delete from Tbl_Nodes WHERE Part IN (SELECT Part FROM Data_Tree)
Note that this will remove all nodes from Tbl_Nodes that have a corresponding Part value in Data_Tree but does not remove any records from Data_Tree itself.
While SQL varies somewhat among vendors, as a general principle it's a mistake to learn SQL from MS Access and try to apply it to other products. MS Access features some very non-standard constructions.
Using an alias for the table?
FROM table AS t1
You're missing a bit of your SQL statement there I guess but does it not work if you just say:
delete N from Tbl_Nodes N, Data_tree DT WHERE...(rest of statement)
I've just removed the .*

Verifying sqlite FTS (Full Text Search) syntax, and how to do a single term NOT search

Is there a way to determine if a MATCH query sent to an fts3 table in sqlite is valid? Currently, I don't find out if the expression is invalid until I try to run it, which makes it a little tricky. I'd like to report to the user that the syntax is invalid before even trying to run it.
I'm using sqlite with the C api.
Additionally, doing a search with the expression "NOT " will fail with a "SQLlite logic/database error". Googling seems to indicate that such a query is invalid. Is there a correct syntax to do the operation? I'm essentially trying to find entries that do NOT contain that term. Or do I have to drop back down to using LIKE and do a sequential scan and not use FTS?
Looks like the simplest way right now is to create a separate FTS table with no rows, and execute the query against it. It will be immediate since there's no data in the table, and will report and error if the query syntax is incorrect.

Resources