3 回答
TA贡献1871条经验 获得超8个赞
python 把变量的值传递给execute的sql中去的代码:
import pymysql
db = pymysql.connect(host="119.XX.XX.XX",
port=3306,
user="XXXXXXXX",
passwd="XXXXXXXXXXXXX",
db="XXXXXX",
charset='utf8')
# %s 占位符为需要传递的参数,切记不要加''双引号,要不然会报错
sql = "SELECT totalusercount * 1.4 FROM mm_project_uv_outdoor WHERE poiid = %s AND currenttime = %s"
cursor = db.cursor()
# 以下为传递多个参数的用法
cursor.execute(sql,['B00140N5CS','2019-04-23'])
# 传递单个参数时 cursor.execute(sql,'B00140N5CS')
print(cursor.fetchall())
db.close()
扩展资料:
函数
Python的函数支持递归、默认参数值、可变参数,但不支持函数重载。为了增强代码的可读性,可以在函数后书写“文档字符串”(Documentation Strings,或者简称docstrings),用于解释函数的作用、参数的类型与意义、返回值类型与取值范围等。可以使用内置函数help()打印出函数的使用帮助。比如:
>>> def randint(a, b):
... "Return random integer in range [a, b], including both end points."...
>>> help(randint)
Help on function randint in module __main__:
randint(a, b)
Return random integer inrange[a, b], including both end points.
TA贡献1890条经验 获得超9个赞
问题:
curs.execute("select * from tables where userid=%s and code=%s")%(IDEntry2.get(),%IDEntry1.get()) #Entry获取的值无法当成变量传递给execute的sql语句中。
问题是IDEntry1之前多了一个%
解决:
#curs.execute("select * from tables where userid=‘aaa’ and code=‘123’") //直接写死就可以
按照可以的进行替换
selectSql = "select * from tables where userid=‘%s’ and code=‘%s’" % (str(IDEntry2.get()),str(IDEntry1.get()))#加str确认与%s匹配
curs.execute(selectSql)
TA贡献1880条经验 获得超4个赞
添加回答
举报