Mysql Connector/Python 下的 SQL 语句中的格式化字符串问题?

发布于 2022-09-02 11:10:52 字数 1178 浏览 28 评论 0

import pandas as pd
import mysql.connector as cnmsq

try:
    cnkun = cnmsq.connect(user='root',
                          password='123456',
                          host='127.0.0.1',
                          database='dbkun')
except:
    print"failed connect..."

cursor = cnkun.cursor()
aos_query = "SELECT is_accepted_name, accepted_id FROM kun_species WHERE species_name=%s"
acc_query = "SELECT species_family, species_genus, species_name, species_full_name FROM kun_species WHERE species_id=%d"
cursor.execute(aos_query, ('Linaria genistifolia',))
for id, acc_num in cursor:
    print(id)
    cursor.execute(acc_query, (acc_num,))
    print([w for w in cursor])

如上,数据库连接成功之后,执行cursor.excute(acc_query,(acc_num,))时候发生错误,将acc_query%d改为%s则程序可以完全正常的执行,但问题是acc_num本身是int型,为什么acc_query内要用%s而非%d格式化它?
引发的错误:

Traceback (most recent call last):
    ....
    "Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2022-09-09 11:10:52

sql 语句 必须是 字符串啊,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文