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
-
you need to look into String concatenation: docs.python.org/2/library/string.htmlFrank Thomas– Frank Thomas2015年12月20日 21:09:08 +00:00Commented Dec 20, 2015 at 21:09
1 Answer 1
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
oz123
29.1k30 gold badges133 silver badges196 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default