[英]Python mysql create and name new table using variable name
So I have variable named 'racenumber' that I want to use as the name of a new table I'm creating. 因此,我有一个名为“ racenumber”的变量,希望用作创建的新表的名称。
The python code below is currently not working. 以下python代码目前无法正常工作。 Any ideas on where the issue is? 关于问题所在的任何想法?
racenumber = 10
cursor = db.cursor()
cursor.execute("""CREATE TABLE %s (ID INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(50) DEFAULT NULL,
PRIZE_TOTAL VARCHAR(50) DEFAULT NULL, STRENGTH VARCHAR(50) DEFAULT NULL, URL VARCHAR(50) DEFAULT
NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1""" % (racenumber))
db.commit()
db.close()
Since you are using a number for the table name, you need to use back-ticks around table name. 由于您在表名中使用数字,因此需要在表名周围使用反引号。 I tested an example using SQL Fiddle here: http://sqlfiddle.com/#!2/c38e57 我在这里使用SQL Fiddle测试了一个示例: http ://sqlfiddle.com/#!2 / c38e57
racenumber = 10
cursor = db.cursor()
cursor.execute("""CREATE TABLE `%s` (ID INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(50) DEFAULT NULL,
PRIZE_TOTAL VARCHAR(50) DEFAULT NULL, STRENGTH VARCHAR(50) DEFAULT NULL, URL VARCHAR(50) DEFAULT
NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1""" % (racenumber))
db.commit()
db.close()
Here is the link from documentation: http://dev.mysql.com/doc/refman/5.0/en/identifiers.html 这是文档中的链接: http : //dev.mysql.com/doc/refman/5.0/en/identifiers.html
Specifically this line: 具体来说这行:
Identifiers may begin with a digit but unless quoted may not consist solely of digits 标识符可以以数字开头,但除非加引号,否则不能仅由数字组成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.