1

I have an issue. I have a list of names that I want to use as table names in my SQLite database, but I am not able to use the SQLite "formatting" to use them. I have the following code:

import sqlite3 as sql

name_list = ['a', 'b', ''c]
conn = sql.connect('test.db')
c = conn.cursor()
for name in name_list:
    c.execute("CREATE TABLE ? (id integer, author text, message text)", (name,))
conn.commit()

But it gives me a syntax error on the c.execute("CREATE TABLE ? (id integer, author text, message text)", (name,)) line.
I want to create these tables because I don't like the idea of having to type out all the names manually, as I am expecting to have ~10-20 tables.

PythonSnek
  • 542
  • 4
  • 21

1 Answers1

1

Assuming you are using python 3, you can use an f-string:

import sqlite3 as sql

name_list = ['a', 'b', ''c]
conn = sql.connect('test.db')
c = conn.cursor()
for name in name_list:
    c.execute(f"CREATE TABLE '{name}' (id integer, author text, message text)")
conn.commit()