当我执行我的程序时出现这个错误:
query = query % tuple([db.literal(item) for item in args]) TypeError: not all arguments converted during string formatting
我试图修复它,但没有任何效果。这是我的代码。 感谢您的帮助。
con = mdb.connect('localhostt', '****', '*********', 'credentials');
with con:
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS Data")
cur.execute("CREATE TABLE Data(Id INT PRIMARY KEY AUTO_INCREMENT, \
Name VARCHAR(25))")
name1 = "Paste"
country_code = 'PSE'
district = 'Someyork'
population = 10008
cur.execute("INSERT INTO Data(Name) VALUES(%s) ",(name1))
con.comit()
最佳答案
你想要
cur.execute("INSERT INTO Data(Name) VALUES(%s)", (name1,))
请注意 (name1,)
中的额外逗号 - 需要它才能使其成为一个元组。没有逗号就和
cur.execute("INSERT INTO Data(Name) VALUES(%s)", name1)
并且由于字符串也是序列,因此连接器会遍历 name1
中的每个字符,因此会出现错误。
关于Python MySQLDB - 并非所有参数都在字符串格式化期间转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50815217/