【问题描述】
1、在一条查询语句中,查询条件既包含了整形又包含了字符串型,执行查询函数后,直接报%d format: a number is required, not str
2、例如 ,如下sql 语句 sql = ‘select productid from product where productid = %d and productName = %s’
【实例代码】
#coding=utf-8
import MySQLdb
def createConnection():
try:
conn = MySQLdb.connect('127.0.0.1', 'root', '000000', 'auto', charset='utf8')
except Exception as e:
print e
return conn
return conn
def select(sql, fieldValue):
conn = None
csor = None
result = None
try:
conn = createConnection()
csor = conn.cursor()
csor.execute(sql, fieldValue)
result = csor.fetchall()
return result
except Exception as e:
print e
finally:
closeDatabaseConnectionOrCursor(conn, csor)
return result
if name == ‘main’:
sql = ‘select productid from product where productid = %d and productName = %s’
fieldValue = [int(28), ‘妈妈钱包’]
result = select(sql, tuple(fieldValue))
执行完这一段代码后,系统会提示 %d format: a number is required, not str
解决方法:把%d全部改成%s