2

I have very simple SQLite table:

CREATE TABLE IF NOT EXISTS `settings` (`Name` TEXT PRIMARY KEY, `Value` TEXT);

I use 2 following queries:

SELECT `Value` FROM `settings` WHERE `Name` LIKE 'MainTabControl.active';

(returns 1 row)

and

SELECT `Value` FROM `settings` WHERE `Name` = 'MainTabControl.active';

(returns 0 rows)

Row with Name column value MainTabControl.active definitely exists (I see it when do SELECT *), doesn't have any spaces at beginning and end, and has the same case of all characters. What is reason of different behavior of equality operator and LIKE?

5
  • @gordon: sqlite, not mysql
    – Marc B
    Commented Feb 11, 2013 at 20:22
  • Hi, no, as I am writing directly in my question, no spaces at the end and begin, I use SQLite C++ API to get data with SELECT *, and all data is without spaces.
    – Vitalii
    Commented Feb 11, 2013 at 20:25
  • Which version of sqlite are you using? I just tested it with 3.7.13 on my system with the sqlite3-cli and it works.
    – Patrick B.
    Commented Feb 11, 2013 at 20:29
  • are you sure there's no unprintable characters in there? e.g. maybe a line feed? what does a length(Name) show for that particular field?
    – Marc B
    Commented Feb 11, 2013 at 20:29
  • My SQLite version: #define SQLITE_VERSION "3.7.14.1" When I do SELECT Name, Value, length(Name) FROM settings it returns MainTabControl.active, 3, 21. It worked for me in past, but it stopped when I started to use queries on this DB with "COLLATE NOCASE like that: SELECT * FROM table WHERE col LIKE '%' || a || '%' COLLATE NOCASE; but it is on another table for this database. May COLLATE used in "where" on another table somehow affect settings table?
    – Vitalii
    Commented Feb 11, 2013 at 20:37

2 Answers 2

4

You may be running into sqlite's type system. Full details are on http://www.sqlite.org/datatype3.html, but the relevant bit is this:

A TEXT value is less than a BLOB value

My guess is that you (intentionally or not) stored the name column as a BLOB rather than TEXT. This will not be = to the text value, but will be LIKE it.

$ sqlite3
SQLite version 3.7.15 2012-10-15 18:02:57
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table settings (name, value) ;
sqlite> insert into settings values ('MainTabControl.active','textname') ;
sqlite> insert into settings values (cast ('MainTabControl.active' as blob),'blobname') ;
sqlite> select value from settings where name = 'MainTabControl.active' ;
textname
sqlite> select value from settings where name like 'MainTabControl.active' ;
textname
blobname
sqlite> select value, typeof(name) from settings where name like 'MainTabControl.active' ;
textname|text
blobname|blob
sqlite> 
1

The case sensitivity of the LIKE operator is determined by the PRAGMA case_sensitive_like command. The default behavior of the LIKE operator is to ignore case for ASCII characters.

sqlite> SELECT 'A' = 'a';
0         
sqlite> SELECT 'A' LIKE 'a';
1           
sqlite> PRAGMA case_sensitive_like = TRUE;
sqlite> SELECT 'A' LIKE 'a';
0 

If we set it to TRUE, the result is equal to the = operator. see http://www.sqlite.org/pragma.html#pragma_case_sensitive_like

3
  • PRAGMA case_sensitive_like = TRUE doesn't change anything in my situation; LIKE still returns 1 row, = returns 0 ones.
    – Vitalii
    Commented Feb 11, 2013 at 20:52
  • Could you post the output of the .dump settings command? (.output setting.sql will redirect the output to a file) It might have some useful information of how the value is exactly stored.
    – Jan Bodnar
    Commented Feb 11, 2013 at 21:25
  • It looks like I experience any bug in SQLite. I continued tests with the same database, on other table I started to receive SQLITE_INTERNAL errors on prepare_v2 calls to very simple queries that can not be wrong (like SELECT * FROM table). From the other hand, PRAGMA integrity_check gives OK. I even exported data with .dump and imported it in empty database, but result is the same. SQLITE_INTERNAL means bug in SQLite according to theirs documentation.
    – Vitalii
    Commented Feb 12, 2013 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.