SELECT statement returns nothing in SQLite - sqlite

I have a table in which there's a column of type BLOB. Another column is of type 'text'. Something like:
CREATE TABLE Tbl(TXT text, BLB blob);
Now I've inserted a few records using:
INSERT INTO Tbl(TXT) VALUES("whatever");
As you can see nothing was defined for BLB. But each time that I issue a query like:
SELECT * FROM 'Tbl' WHERE 'TXT'="whatever";
I get nothing at all without any error message or anything. My primary guess was that the problem might have something to do with BLB being null or undefined or something like that. Any ideas?

Your query should be:
SELECT * FROM Tbl WHERE TXT="whatever";
You are specifying strings for the table name and for the column. SQL lets you perform queries on values, not just tables.

Related

Where condition in mysql with array value

I have a table with like this:
id
values
user_id
1
["8","7","6"]
5
Now I'm running a query with WHERE condition on values column:
SELECT * from table_name WHERE values = ["8","7","6"]
But MySQL returns this error:
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '["8","7","6"]'
If you want to compare for strict equality, you want to do the comparison as JSON objects. You can do this by using JSON_EXTRACT to parse the data as JSON instead of text:
SELECT * from table_name WHERE
JSON_EXTRACT(`values`, '$') = JSON_EXTRACT('["8","7","6"]', '$');
You should be able to use this for any type of JSON as long as you want strict equality. If you want to return all rows that match the given JSON object, use JSON_CONTAINS.
For example to find all values with the string "8" in it, you'd use the following:
SELECT * from table_name WHERE JSON_CONTAINS(`values`, '"8"');
Note that this matching is not as simple as you'd expect and matches any value in the document. If your data consists of JSON arrays, this should still be adequate.
The information about your column datatype, especially values are crucial. Since the column stores a mix of numbers and non-numbers characters, we can assume that it might be stored in VARCHAR() or TEXT datatype. But since the data format looks like a JSON array, it's also a possibility that the column datatype is JSON. Now both of these datatypes have a very different query structure.
First, let's address some issues:
Whenever the cell values include other characters than numerical, it will be considered as string. Hence, using plain .. WHERE values = ["8","7","6"] without wrapping it in quotes ('), you'll get that Error Code : 1064.
VALUES is a reserved word in MySQL so if you want to stick to it as your table column names, you always need to wrap it in backticks. If not, this will also return Error Code : 1064:
.. WHERE `values` = ..
Now let's try this:
If the column datatype for values is VARCHAR() or TEXT, you just have to simply wrap the search value in single quote like:
SELECT * from table_name WHERE `values` = '["8","7","6"]';
Refer this fiddle
updated for MariaDB
If the column datatype for values is JSON, it's something like this:
SELECT * from table_name where JSON_UNQUOTE(`values`)= '["8","7","6"]'
Refer this fiddle for JSON
The JSON method I've referred to this MariaDB documentation.
P/S: According to this documentation JSON is an alias for LONGTEXT introduced for compatibility reasons with MySQL's JSON data type. In other words, when creating a table with JSON datatype in MariaDB, it will be shown as LONGTEXT but with extra definition than just plain LONGTEXT datatype. See this fiddle for more detail.

Sqlite INSERT INTO ... SELECT using ORDER BY

I have a table as below:
CREATE TABLE IF NOT EXISTS TRACE_TABLE ([TRACE_NUM] INTEGER NOT NULL PRIMARY KEY [TRACE_ID] INTEGER NOT NULL [TRACE_TIME_DELTA] TEXT NOT NULL [TRACE_TIME_HEX] INTEGER NOT NULL [TRACE_TIME_AHB] INTEGER NOT NULL [TRACE_PARAM_TEXT] TEXT NOT NULL [TRACE_PARAM_TEXT_DECODED] TEXT);
Now I want to sort this table using a column. To do this I do following:
Create a new table TRACE_TABLE_TEMP using above statement if not exists.
Then delete all rows (in case any exists) by earlier operations
Then copy all rows from TRACE_TABLE to TRACE_TABLE_TEMP but in sorted order using a column.
I try to execute the statement in Sqlite DB browser but I am not getting the expected result. Please see below, the TRACE_NUM column is not sorted as DESC.
How do I copy the table to another in sorted order?
The documentation says:
If a SELECT statement that returns more than one row does not have an ORDER BY clause, the order in which the rows are returned is undefined.
So it does not make much sense to change the order in which rows are stored, because you'd have to put the same ORDER BY on the queries used to read the data later.
Anyway, the error is that 'TRACE_NUM' is a constant string. To refer to the contents of the column, use TRACE_NUM.

Unable to use QtSqlDriver to retrieve data from a table with "." in column names

I have a SQlite database I'm trying to read with the QtSql.QSqlTableModel. The issue is it won't read any table where the field name contains a "." via the setTable method.
As an example if I have table called MyTable with the column names
(ID, Name.First, Name.Last)
I can manually select it with the query
SELECT * FROM MyTable
or
SELECT "ID", "Name.First", "Name.Last" and all is ok
However, the QSqlTableModel won't use that query but will error out with "no such column Name.First Unable to execute statement."
When I dug a little deeper the SQLITE driver in Qt would rewrite the query as
SELECT "ID", "Name"."First", "Name"."Last" FROM MyTable
But this SELECT statement is wrong and would try and grab columns from another table "Name" but I want a column called "Name.First" in the table "MyTable"
I tried to circumvent this by subclassing the setTable method which worked for getting the data into the TableView:
def tableName(self):
return self._tableName
def setTable(self, tableName):
self.clear()
self._tableName = tableName
self.setQuery(QtSql.QSqlQuery("SELECT * FROM {0}".format(tableName), self.database()))
However, reimplementing the method in this fashion broke the method submitAll().
Inside the File Save method I have the following:
ok = self.tableModel.submitAll()
if not ok:
logging.error('Error %s' % self.tableModel.lastError().text())
logging.error('Error %s' % self.tableModel.query().lastQuery())
return False
This gives this log:
ERROR:root:Error near "SET": syntax error Unable to execute statement
ERROR:root:Error SELECT * FROM MyTable
But when I don't reimplement the setTable method, submitAll() works without errors.
So... How do I circumvent the "." in the Column name problem and also have the submitAll() work?
BTW: I agree that having "." in the field names for SQL tables is not a good idea but this is pairing up with another tool that generates the sqlite file in this manner which I have no control over.
http://www.qtcentre.org/archive/index.php/t-7565.html
http://www.qtforum.org/article/11245/sqlite-how-to-insert-text-that-contains-character-in-field.html
Looks like you just need to call one or both of the functions below before sending it to the database, in order to sanitize the input.
http://qt-project.org/doc/qt-4.8/qsqlquery.html#bindValue
http://qt-project.org/doc/qt-4.8/qsqlquery.html#prepare
http://xkcd.com/327/
:)
Hope that helps.

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';

SQLite Query to Insert a record If not exists

I want to insert a record into a sqlite table if its actually not inserted.
Let's say it has three fields pk, name, address
I want to INSERT new record with name if that name not added preveously.
Can we do with this in a single Query. Seems like its slightly different from SQL Queries sometimes.
Yes, you can do that with a single query.
INSERT ON CONFLICT IGNORE should help you: http://www.sqlite.org/lang_conflict.html
Put a unique key on the name, this will create a conflict when you try inserting a record if the name already exists.
The default is ABORT, so without the IGNORE, the statement will return an error. If you don't want that, use IGNORE.
If you can't make use of a UNIQUE INDEX in combination with INSERT INTO or INSERT OR IGNORE INTO, you could write a query like this;
INSERT INTO table (column)
SELECT value
WHERE NOT EXISTS (SELECT 1
FROM table
WHERE column = value)

Resources