簡體   English   中英

MySQL Python 以變量為名稱創建表

[英]MySQL Python creating table with variable as name

我正在和朋友一起做一個項目,並且是 sql 數據庫的新手。 我想要一個數據庫,其中包含以創建日期命名的表。 我嘗試了多種方法,但我認為.format() 會是最好的,但它沒有用。 這是它目前的樣子:

today = date.today()
d1 = today.strftime("%b-%d-%Y")
sql = "CREATE TABLE {table} (CEO VARCHAR(255), profits INTEGER(10))"
mycursor.execute(sql.format(table = d1))

我也試圖用.format插入數據庫:

sql = "INSERT INTO{table} (CEO, profits) values (%s, %s)"
mycursor.execute(sql.format(table = d1), (Company(c).get_CEO(), int(Company(c).get_profit())))
mydb.commit()

我得到錯誤:

mysql.connector.errors.ProgrammingError: 1064 (42000): 你的 SQL 語法有錯誤;

INTO{table}之間沒有空格

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM