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()
Avinash Raj
175k32 gold badges247 silver badges289 bronze badges
asked Dec 20, 2015 at 20:08
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.

answered Dec 21, 2015 at 9:04
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.