3

I would like to use a python output to create table in sqlite3. I've tried some possibilities but the script create tables with the given name. Is it possible to create a table with a variable?

for name in test.all():
    return name

or just

name = 'dbName'
#
conn = sqlite3.connect("my_db")
db = conn.cursor()
db.execute('''CREATE TABLE (here var)(id, column1, column2, column2)''')
conn.commit()
db.close()
1

1 Answer 1

5

Try with string formatting:

sql_cmd = '''CREATE TABLE {}(id, column1, column2, column2)'''.format(
            'table_name')
db.execute(sql_cmd)

Replace 'table_name' with your desire.

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

Comments

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.