mysql-connect - Mysql Connector/Python 下的 SQL 语句中的格式化字符串问题?
PHP中文网
PHP中文网 2017-04-17 17:25:30
0
1
902
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
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
Peter_Zhu

SQL statement must be a string,

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template