Pymysql-将混合类型的数据添加到数据库记录中的问题
Pymysql- issue with adding mixed types of data into database record
我正在尝试编写一个使用 pymysql 将记录添加到数据库的函数。当我使用字符串时它起作用了,但是当我尝试混合使用字符串和整数时它不起作用。
def dodawanie_do_bazy(a,b,c,e):
connection = pymysql.connect(host='localhost',
user='root',
password='',
db='dbprojekt',
charset='utf8mb4',
cursorclass=pymysql.cursors.SSDictCursor);
try:
with connection.cursor() as cursor:
sql="INSERT INTO `Czlonek` (`imie`,`nazwisko`,`wydzial`,`telefon`,`mail`) VALUES (%s,%s,%s,%d,%s)";
cursor.execute(sql,(a,b,c,456798136,e));
connection.commit()
finally:
connection.close();
错误是 TypeError:%d 格式:需要数字,而不是 str
作为错误
ERROR is TypeError: %d format: a number is required, not str
显然指出,它需要是 %s
格式而不是 %d
。格式字符串并不是真正要使用 %d
的 Python 格式字符串。您必须始终对所有字段使用 %s
只需将此从 VALUES (%s,%s,%s,%d,%s)
更改为 VALUES (%s,%s,%s,%s,%s)
即可解决您的问题。
我正在尝试编写一个使用 pymysql 将记录添加到数据库的函数。当我使用字符串时它起作用了,但是当我尝试混合使用字符串和整数时它不起作用。
def dodawanie_do_bazy(a,b,c,e):
connection = pymysql.connect(host='localhost',
user='root',
password='',
db='dbprojekt',
charset='utf8mb4',
cursorclass=pymysql.cursors.SSDictCursor);
try:
with connection.cursor() as cursor:
sql="INSERT INTO `Czlonek` (`imie`,`nazwisko`,`wydzial`,`telefon`,`mail`) VALUES (%s,%s,%s,%d,%s)";
cursor.execute(sql,(a,b,c,456798136,e));
connection.commit()
finally:
connection.close();
错误是 TypeError:%d 格式:需要数字,而不是 str
作为错误
ERROR is TypeError: %d format: a number is required, not str
显然指出,它需要是 %s
格式而不是 %d
。格式字符串并不是真正要使用 %d
的 Python 格式字符串。您必须始终对所有字段使用 %s
只需将此从 VALUES (%s,%s,%s,%d,%s)
更改为 VALUES (%s,%s,%s,%s,%s)
即可解决您的问题。