如何在python中将MySql表名作为变量提交?

2024-09-30 06:34:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为每个唯一的用户创建一个新的mysqldb表,但出现了以下错误:

1.'bytes' object has no attribute 'encode'

2.Can't convert 'bytes' object to str implicitly

3.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Text'( id int(11) NOT NULL AUTO_INCREMENT, UserName text NOT NULL' at line 1

c.execute("CREATE TABLE IF NOT EXISTS {table_name}".format(table_name=belekas), (belekas) + """(
`id` int(11) NOT NULL AUTO_INCREMENT,
`UserName` text NOT NULL,
`Data` date NOT NULL,
`Laikas` time NOT NULL,
`KeyStrokes` text NOT NULL,
PRIMARY KEY (`id`)
)   ENGINE=InnoDB DEFAULT CHARSET=utf8""")
con.commit()
c.execute("INSERT INTO {table_name} VALUES (id, %s, Data, Laikas, %s)".format(table_name=belekas),
          (belekas, vartotojas, tekstas))
con.commit()

我试过使用:

c.execute("CREATE TABLE IF NOT EXISTS" + vartotojas + """(

还有这个:

c.execute("CREATE TABLE IF NOT EXISTS" + repr(vartotojas.decode('utf-8')) + """(

还有这个:

c.execute("CREATE TABLE IF NOT EXISTS {this_table}".format(this_table=vartotojas), (vartotojas.encode("utf-8")) + """(

有人能提出解决这个问题的办法吗


Tags: totextnameidformatexecuteifcreate

热门问题