详细代码如下,请问我该如何把变量的值传递给execute的sql中?

###_*_coding:gb2312_*_
from Tkinter import *
import sys
import DB2
class ONE(object):
def __init__(self):
self.top=Tk()
self.top.title('test')
self.IDVar1=StringVar()
self.IDVar2=StringVar()
frame=Frame(self.top)
IDLabel1=Label(frame,text="code:",underline=0)
IDLabel2=Label(frame,text="Userid:",underline=0)
IDEntry1=Entry(frame,textvariable=self.IDVar1,width=25)
IDEntry2=Entry(frame,textvariable=self.IDVar2,width=25)
def check():
dsn="driver={IBM DB2 ODBC DRIVER};database=%s;hostname=%s;port=%s;protocol=tcpip;"%("dbname","192.168.1.1","65535")
conn = DB2.connect(dsn,"user","password")
curs = conn.cursor() #到此为止都很顺利
curs.execute("select * from tables where userid=%s and code=%s")%(IDEntry2.get(),%IDEntry1.get()) #Entry获取的值无法当成变量传递给execute的sql语句中。
#curs.execute("select * from tables where userid=‘aaa’ and code=‘123’") //直接写死就可以

ows = curs.fetchall()
print ows
curs.close()
conn.close()
Button1=Button(frame,text="check",command=check)
IDLabel1.grid(row=0,column=0,sticky=W,pady=3,padx=15)
IDEntry1.grid(row=0,column=1,sticky=EW,pady=3,padx=15)
Button1.grid(row=2,column=1,sticky=EW,pady=3,padx=3)
frame.pack()
def close(self,event=None):
quit()
def main():
ONE()
mainloop()
if __name__=='__main__':
main()

紫衣仙女
浏览 87回答 3
3回答

ITMISS

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.

当年话下

问题: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)

慕村225694

1、交换变量x,y的值,需要利用到第三个变量Z。2、在python中,就必须简单了,下面我们一步步来进行操作演示。首先给x,y变量赋值:x=10,y=20。3、python中,还可以几个变量一起幅值,如x,y=10,20。4、python中,两个变量交换值可以在一个等式中完成,如下所示:x,y=y,x,从结果中可以看到两个变更的值已经发生变量。5、在python中各类型数据都能通过。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python