使用python的字符串格式库的问题

时间:2023-01-14 15:19:34
cur.execute('INSERT INTO company VALUES (%(cname), %(symbol), %(start_date), %(end_date))' %{'cname' : company, 'symbol' : company, 'start_date' : startdate, 'end_date' : enddate})

Trying to run this line on my computer results in a string formatting error: ValueError: unsupported format character ',' (0x2c) at index 36

尝试在我的计算机上运行此行会导致字符串格式错误:ValueError:索引36处不支持的格式字符','(0x2c)

It seems to be concerning the , but I have checked and all the parenthesis are properly nested (none enclosing an errant ,)

它似乎与此有关,但我检查过并且所有括号都已正确嵌套(没有包含错误,)

2 个解决方案

#1


3  

What @imm said. Also, you may want to use the built in query formatting that is part of MySQLdb.

@imm说的是什么。此外,您可能希望使用作为MySQLdb一部分的内置查询格式。

cur.execute("INSERT INTO company VALUES (%s, %s, %s, %s)", (company, company, startdate, enddate))

#2


16  

You need an "s" after each of those positional arguments.

在每个位置参数之后你需要一个“s”。

(%(cname)s, %(symbol)s,  ....

#1


3  

What @imm said. Also, you may want to use the built in query formatting that is part of MySQLdb.

@imm说的是什么。此外,您可能希望使用作为MySQLdb一部分的内置查询格式。

cur.execute("INSERT INTO company VALUES (%s, %s, %s, %s)", (company, company, startdate, enddate))

#2


16  

You need an "s" after each of those positional arguments.

在每个位置参数之后你需要一个“s”。

(%(cname)s, %(symbol)s,  ....