我是python的初学者,我尝试使用tkinter开发接口来连接数据库并进行查询以显示两个日期和日期格式之间的数据,例如2019-05-31 00:00:00,但是我遇到了问题与日期变量,我无法插入到SQL查询。我想从用户那里获取日期。对不起,真的很简单
我在互联网上搜索并尝试了不同的符号,但是我无法解决问题
import pyodbc
from tkinter import *
import datetime
root = Tk()
root.configure(background='blanched almond')
root.geometry("700x600")
root.title("QUERY PROGRAM ")
label1= Label(root,text=" QUERY PROGRAM ",bg='blanched almond',fg='LightSkyBlue4',font="Arial 12 bold")
label1.pack()
label2=Label(root,text="Please enter the first date such as 2019-05-22 00:00:00",bg='blanched almond')
label2.place(x=0,y=100)
label3=Label(root,text="Please enter the last date such as 2019-05-24 00:00:00",bg='blanched almond')
label3.place(x=0,y=160)
date1_text=StringVar()
date2_text=StringVar()
entry1=Entry(root, textvariable=date1_text)
entry1.place(x=10,y=130)
entry2=Entry(root, textvariable=date2_text)
entry2.place(x=10,y=190)
date1=date1_text.get()
date2=date2_text.get()
def query():
con= pyodbc.connect("DRIVER={SQL Server}; server=server;database=TestVT;uid=da;pwd=1234")
cur = con.cursor()
cur.execute("select date from TestVT.data where date >='%s' and date < '%s' and datetype in (4,5,6,7,13)" %(date1,date2))
data =cur .fetchall()
for i in data:
#print (f" ID = {i[0]} NAME={i[1]}")
print (i)
cur.close()
con.close()
button1=Button(root,text="QUERY",command=query )
button1.place(x=300,y=280)
root.mainloop()
如果我从%s中删除了',例如date> =%s,我得到了关键字'and'附近的错误语法消息。谢谢您的邀请