SQLite3 WHERE clause doesn't return anything if datatype is BLOB - sqlite

I'm using SQLite version 3.11.0 2016-02-15 17:29:24 3d862f207e3adc00f78066799ac5a8c282430a5f on Ubuntu 16.04
The query SELECT * FROM wordlist WHERE term like 'juliet' doesn't seem to return anything while the query SELECT * FROM wordlist WHERE cast(term as text) like 'juliet' returns as it should.
The datatype of the term column is blob which is also strange since I defined it as TEXT.
The problem doesn't occur in SQLite 3.8.10.2

Related

SQLite ordering column with accented characters

I'm developping a website using SQLite databases with PHP. I'm running Windows (dev) and my production environment should be a *nix platform. Here is the schema of my table :
CREATE TABLE [animals](
[id] INTEGER NOT NULL UNIQUE,
[name] VARCHAR(50) NOT NULL
);
I want to sort the animals by name (contains accented characters). My SQL query is:
SELECT * FROM animals ORDER BY name DESC
and I get :
éléphant
tigre
renard
chien
instead of,
tigre
renard
éléphant
chien
I've searched on the web. I tried,
SELECT * FROM animals ORDER BY name COLLATE *binary|nocase|rtrim* DESC
but I have the same problem. I also tried,
SELECT * FROM animals ORDER BY name COLLATE *localized|unicode* DESC
But I get an error (either my SQLite client crashes, either PHP returns the following error: no such collation sequence: UNICODE).
It seems there's a solution for Android, but I'm running Windows and my production environment should be a *nix platform.
How can I get my animals sorted the right way?
By default, SQLite has only ASCII collations.
It would be possible to use the ICU extension to get Unicode support, but PHP did not enable this.
But you can create your own collation and implement the comparison in PHP.

SQLite nvarchar(100) field can accept 200 char field. Why?

I have a table with a field defined as nvarchar(100).
I just noticed if inserted a new record (an 200 string value for example) the query works and not throws any exception.
Is a SQLite 'feature'?
Usign SQLite 1.0.94 with Visual Studio 2010 / C# and SQLite v3 dabatabse.
SQLite doesn't recognize the limit you specified in statement, so it's not enforced.
In order to enforce it, you might need a statement like this:
CREATE TABLE t (f TEXT CHECK(LENGTH(f)<101));
So text with more than 100 characters cannot be inserted.
SQLite has a single unlimited TEXT datatype. See the documentation:
http://www.sqlite.org/datatype3.html#affname
Note that numeric arguments in parentheses that following the type
name (ex: "VARCHAR(255)") are ignored by SQLite - SQLite does not
impose any length restrictions on the length of strings, BLOBs or numeric
values.

Opposite of HEX() in SQLite?

I have this simple query that returns a bunch of guids as hexadecimal strings:
SELECT HEX(guid) FROM table;
One of them is for instance 43F4124307108902B7A919F4D4D0770D. Then imagine I want to get the record with this guid, so I write a query like this:
SELECT * FROM table WHERE guid = '43F4124307108902B7A919F4D4D0770D';
Of course, this will not work, since the string is directly interpreted as a blob and not converted to it's hex value. I looked here, but couldn't find anything that looks like a method that takes a hexadecimal string and converts it to a blob.
While writing the question I found the answer. I simply had to add an X before the string. Like this:
SELECT * FROM table WHERE guid = X'43F4124307108902B7A919F4D4D0770D';
I figured I should post the question anyway, since non of the "Similar Questions" answers this. What I was looking for was not a function, but a literal and when I realized this I quickly found the answer here.
In the upcoming version it's supported:
SQLite version 3.41.0 2023-02-08 12:47:37
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> select unhex('41')
...> ;
A
sqlite> select hex('a')
...> ;
61
sqlite> .q

SQLite does not save string made of zeroes

I'm using SQLite 3.7.4 (within my c++ app under Ubuntu PP) and when I try to save string like "000000" it saves just one char "0". I tried it in console too - is it feature, or bug? How can I get rid of that?
For example:
CREATE TABLE status (readTime INTEGER, status STRING);
INSERT INTO status (readTime, status) values(1234, "000000");
INSERT INTO status (readTime, status) values(4321, "111111");
SELECT * FROM status;
1234|0
4321|111111
Some quick research points to the fact that STRING is not a recognized type in SQLite (or, I believe, most other SQL dialects). However, SQLite isn't strict with its types, so the type defaulted to NUMERIC. You should be able to change the column type to TEXT to resolve your issue.
Further reading: Type Affinity
"STRING" isn't a SQLite data type. Try "TEXT".

WHERE - IS NULL not working in SQLite?

Here's a strange one:
I can filter on NOT NULLS from SQLite, but not NULLS:
This works:
SELECT * FROM project WHERE parent_id NOT NULL;
These don't:
SELECT * FROM project WHERE parent_id IS NULL;
SELECT * FROM project WHERE parent_id ISNULL;
SELECT * FROM project WHERE parent_id NULL;
All return:
There is a problem with the syntax of your query (Query was not
executed) ...
UPDATE:
I am doing this with PHP- through my code with ezSQl and using the PHPLiteAdmin interface
Using the PHPLiteAdmin demo, this expression works- so now I'm suspecting a version issue with my PHP's SQLite? Could that be? Wasn't this expression always valid?
UPDATE 2:
When I run the code from PHP using ezSQL, the PHP warning is:
PHP Warning: SQL logic error or missing database
Is there a way to get more information out of PHP? This is maddeningly opaque and weird, especially because the same statement in the CLI works fine...
UPDATE 3
The only other possible clue I have is that the databases that I create with PHP cannot be read by the CLI, and vice versa. I get:
Error: file is encrypted or is not a database
So there's definitly two SQlite flavors butting heads here. (See this) Still, why the invalid statment??
UPDATE 4
OK I think I've traced the problem to the culprit, if not the reason- The DB I created with PHP ezSQL is the one where the IS NULL statement fails. If I create the DB using PHP's SQLite3 class, the statement works fine, and moreover, I can access the DB from the CLI, whereas ezSQL created DB gave the file is encrypted error.
So I did a little digging into ezSQL code- Off the bat I see it uses PDO methods, not the newer SQLite3 class. Maybe that's something- I'm not gonna waste further time on it...
In any case, I've found my solution, which is to steer clear of ezSQL, and just use PHPs SQLite3 class.
a IS b and a IS NOT b is the general form where a and b are expressions.
This is generally only seen in a IS NULL and a IS NOT NULL cases. There are also ISNULL and NOTNULL (also NOT NULL) operators which are short-hands for the previous expressions, respectively (they only take in a single operand).
The SQL understood in SQLite expressions is covered in SQLite Query Language: Expressions.
Make sure that (previous) statements have been terminated with a ; first if using the CLI.
These are all valid to negate a "null match":
expr NOT NULL
expr NOTNULL
expr IS NOT NULL
These are all valid to "match null":
expr ISNULL
expr IS NULL
Since all of the above constructs are themselves expressions the negations are also valid (e.g. NOT (expr NOT NULL) is equivalent to expr IS NULL).
Happy coding.
The proof in the pudding:
SQLite version 3.7.7.1 2011-06-28 17:39:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table x (y int null);
sqlite> select * from x where y isnull;
sqlite> select * from x where y notnull;
sqlite> select * from x where y not null;
sqlite> select * from x where y is null;
sqlite> select * from x where y is not null;
sqlite>
The problem could stem from how SQLite handles empty columns. For instance just because a column is empty does not mean it is NULL. Have you tested against ""?
SELECT * FROM project WHERE parent_id = ""
That query might return results.
In Android SQLite, field IS NULL doesn't work either.
field = 'null' does. Give it a try in your environment
This works on SQLite in SQLite Manager for Firefox:
select * from foo where not baz is not null
The query above returns rows where column [baz] is null. :-) Yarin, maybe it will work for you?
(The 'not' before the column name is not a typo).
This query too finds rows where baz is null:
select * from foo where [baz] is null
If you are testing perhaps the PK column (?) and the column is being treated as synonym for rowid, then no rows will have a rowid that's null.
try where your_col_name ISNULL
wheres ISNULL contains no space

Resources