Ошибка sqlite3.OperationalError, при попытке поставить переменную как название таблицы в sqlite3

В моем проекте нужно чтобы при изменении переменной ID создавалась новая таблица, поэтому я пытался вставить переменную в качестве названия таблицы

import sqlite3

conn=sqlite3.connect('tb.db')
cur=conn.cursor()
ID=2304829445
cur.execute('''DROP TABLE IF EXISTS {0}'''.format(ID))
cur.execute('''CREATE TABLE {0} (name TEXT, age INTEGER)'''.format(ID))
cur.execute('''INSERT INTO {0} (name, age) VALUES('anton', '12')'''.format(ID))
conn.commit()

print('mytable:')
cur.execute('''SELECT name FROM {0}'''.format(ID))
Name=cur.fetchone()[0]
cur.execute('''SELECT age FROM {0}'''.format(ID))
Age=cur.fetchone()[0]
print(Name, Age)

но при выполнении этого кода мне выдает ошибку:

Traceback (most recent call last):
  File "Q:\python\p\code.py", line 6, in <module>
    cur.execute('''DROP TABLE IF EXISTS {0}'''.format(ID))
sqlite3.OperationalError: near "2304829445": syntax error

что я делаю не так?


Ответы (1 шт):

Автор решения: MaxU

Названия объектов в SQL должны начинаться с буквы или символа подчеркивания.

Поэтому вы получаете ошибку синтаксиса.


Вот выдержка из стандарта ANSI SQL-92:

Object Naming Conventions for ANSI/ISO Entry SQL-92 Compliant Databases

For reports based on tables or views in ANSI/ISO Entry SQL‑92 compliant databases, observe the following conventions regarding object names when referencing them within Report-Writer statements and commands:

  • Names consist of 18 or fewer characters.
  • Regular identifiers begin with an alphabetic character (az) and contain only alphabetic, numeric, or underscore (_) characters.
  • Regular identifiers are case insensitive.
  • Delimited identifiers are case sensitive.

These conventions differ somewhat from standard Ingres conventions for regular and delimited identifiers. Also, in standard Ingres databases, regular identifiers can include the non‑alphanumeric characters #, @, and $, and delimited identifiers can be case insensitive, depending on how the database was created.

→ Ссылка