1

This is my code:

import sqlite3

conn = sqlite3.connect(':memory:')
cursor = conn.cursor()

cursor.execute('''
CREATE TABLE schools (
    county TEXT,
    statustype TEXT
)
''')

cursor.execute('''
INSERT INTO schools VALUES ('some_county', 'some_status')
''')


cursor.execute('''
SELECT COUNT(*) FROM schools 
WHERE county = 'Alpine' 
  AND statustype IN ('Active', 'Closed') 
  AND "School Type" = 'District Community Day Schools'
''')

result = cursor.fetchone()[0]
print(f"Count result: {result}")

conn.close()

Note that there is intentionally no 'School Type' column in the database schema in this example.

The result is 0.

Is possible to change some settings of SQLite3 or of the database in order to get an error about non-existing column instead?

0

1 Answer 1

2

SQLite has a quirk when it comes to delimiting identifiers. In ANSI SQL double quoting an column name does what you expect i.e. delimits the column name. However in SQLite you get the following behaviour (from the docs)

... in an effort to be compatible with MySQL 3.x (which was one of the most widely used RDBMSes when SQLite was first being designed) SQLite will also interpret a double-quotes string as string literal if it does not match any valid identifier. This misfeature means that a misspelled double-quoted identifier will be interpreted as a string literal, rather than generating an error. It also lures developers who are new to the SQL language into the bad habit of using double-quoted string literals when they really need to learn to use the correct single-quoted string literal form.

Therefore, because the column doesn't exist, you end up comparing strings to strings e.g. "School Type" = 'District Community Day Schools' which is always false.

Therefore you need to delimit the column name using either square brackets ([]) or backticks (``).

e.g. you should use [column name here] or `column name here` after which you'll get an error as such:

sqlite3.OperationalError: no such column:

Of course in general its actually better to not use spaces in the first place (eg. school_type).

Note: The same documentation goes on to say:

As of SQLite 3.29.0 (2019-07-10) the use of double-quoted string literals can be disabled at run-time...

Which would seem to be a good practice in order to avoid this sort of confusion.

Sign up to request clarification or add additional context in comments.

10 Comments

weird, because this query SELECT COUNT(*) FROM schools WHERE county = 'Alpine' AND statustype IN ('Active', 'Closed') AND "District Community Day Schools" = "District Community Day Schools" does in fact return a result (given an entry in the table with country = alpine, status = active) `
using ` School Type` gives the required error message
@DaleK not in python IDLE :/
@DaleK also apparently works in python's sqlite3 module ¯\_(ツ)_/¯
@DaleK idle basically just default python shell docs.python.org/3/library/idle.html ; no comment on the dialects though; also as Stanislav mentioned import sqlite3 ...
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.