Titling sqlite3 table columns with number

I got a tuple whose elements are int or real numbers and I need to create a sqlite3 table assigning to each column name the correspondent element into the tuple.

Here’s my code:

fAEHz=('20','25','31.5','40','50','63')
conn = sqlite3.connect('daticert.db')
cur = conn.cursor()
columns = " TEXT, ".join([str(freq) for freq in fAEHz])
command = "CREATE TABLE IF NOT EXISTS mk221ae (" + columns + " TEXT)"
cur.execute(command)
conn.commit()

There’s something wrong since I got the following message:

", line 293, in <module>
    cur.execute(command)
sqlite3.OperationalError: near "20": syntax error

If I use a tuple whose elements are strings (as shown below), everything works fine!

fAEHz = ["Name", "Address", "Email", "Phone"]

Is there any solution or it is forbidden to give sqlite3 column name a number?
Thank you ever so much.

SQLite does not accept integers as column (or table) names. (I’m not sure if any SQL dialect actually do.) As an exercise, try writing these queries out directly in the SQLite CLI.

If you really want numbers as column names, you’ll have to write them as string literals. I leave that also as an exercise for the reader :slight_smile:

Slightly related: perhaps we should revive bpo-33436: Add an interactive shell for Sqlite3? It could make it easier for users to experiment with and test out queries while developing.

2 Likes

Having python, sqlite3.dll, and no simpler battery included way to explore datas with this, is a crime

Crime or not; there’s a draft PR for an interactive sqlite3 shell now: