pymsql - "TypeError: not all arguments converted during string formatting" when passing multiple parameters

pymsql - "TypeError: not all arguments converted during string formatting" when passing multiple parameters

我试图将多个 Python 变量传递给 pymysql 中的 SQL 查询,但总是收到“TypeError:并非所有参数都在字符串格式化期间转换”。为了调试,这个table:

里面没有其他记录
c.execute('''CREATE TABLE upcoming_events1(
                    id INT PRIMARY KEY AUTO_INCREMENT, 
                    name VARCHAR(255),
                    html_insert TEXT,
                    date INT(11),
                    institution VARCHAR(5))''')

header = "test string"
my_html = "some html, also a string"
event_date = "1273627537"
event_institution = "xyz"

c.execute('''INSERT INTO upcoming_events1 (name, html_insert, date, institution) VALUES (%s);''', (header, my_html, event_date, event_institution, ))

c.execute('''SELECT * FROM upcoming_events1''')
debug = c.fetchall()
print(debug)

我已经尽可能多地阅读了有关此上下文中错误的信息。许多帖子(参见here, here, here, and ) suggest that something seems to be wrong with the parameter substitution syntax around %s, but I just can't find the mistake. also dealt with multiple parameters, but I do not see me actively passing any "None" types. I have tried reducing the number of parameters passed at a time in various combinations and noticed that while two or more variables cannot be passed in one statement, every single one of them individually can. In case this should be relevant: I have foreign keys in other tables referencing the id column, as you can see I do not try to pass any values to it here though (mentioning it because it seemed like the issue might have been caused by constraints here)。

您缺少几个 %s。正确说法:

c.execute('''INSERT INTO upcoming_events1 (name, html_insert, date, institution) VALUES (%s, %s, %s, %s);''', (header, my_html, event_date, event_institution, ))