获取Python SQL错误-必须声明标量变量"@p1offset";使用Tkinter时
我正在尝试运行2个SQL查询,并将它们分别输出到Tkinter中的2个标签:获取Python SQL错误-必须声明标量变量"@p1offset";使用Tkinter时,python,sql-server,tkinter,Python,Sql Server,Tkinter,我正在尝试运行2个SQL查询,并将它们分别输出到Tkinter中的2个标签: self.output = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n') self.output.grid(column=0, row=6, columnspan=5, padx=0, pady=0) self.output2 = tkinter.Labe
self.output = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output.grid(column=0, row=6, columnspan=5, padx=0, pady=0)
self.output2 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output2.grid(column=0, row=7, columnspan=5, padx=0, pady=0)
我得到了一个标量变量错误,我不确定是什么原因造成的。
在Tkinter中,我不能将多行输出到一个标签,因此我必须创建额外的标签并放置
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY
在第二个SQL查询中,以便在第二个标签上显示第二行
以下是我正在尝试的:
def calculate(self):
firstname = str(self.first_entry.get())
lastname = str(self.last_entry.get())
license = str(self.lic_entry.get())
if (firstname and not lastname and not license): # "You entered first name."
try:
connection = pypyodbc.connect('Driver={SQL Server};Server=myServer;Database=myDatabase;Trusted_Connection=yes;')
except pypyodbc.Error as ex:
sqlstate = ex.args[0]
if sqlstate == '28000':
self.answer_label['text'] = "You do not have access."
cursor = connection.cursor()
SQLCommand = ("SELECT LASTNAME, FIRSTNAME, L_LICNUMBER "
"FROM dbo.my_table " # table name
"with (nolock)"
"WHERE FIRSTNAME = ?")
Values = [firstname]
cursor.execute(SQLCommand,Values)
results = cursor.fetchall()
if results:
self.output['text'] = (results[0]) # display results
else:
self.output['text'] = "That name does not exist."
cursor = connection.cursor()
SQLCommand = ("SELECT LASTNAME, FIRSTNAME, L_LICNUMBER "
"FROM dbo.my_table " # table name
"with (nolock)"
"WHERE FIRSTNAME = ?"
"order by FIRSTNAME"
"offset 1 rows"
"fetch next 1 rows only")
Values = [firstname]
cursor.execute(SQLCommand,Values)
results = cursor.fetchall()
if results:
self.output2['text'] = (results[0]) # display results
connection.close()
我找了一整天才找到解决办法。我在谷歌上找不到任何东西。 偏移行和获取不起作用 这是:
def calculate(self):
firstname = str(self.first_entry.get())
lastname = str(self.last_entry.get())
license = str(self.lic_entry.get())
if (firstname and not lastname and not license): # "You entered first name."
try:
connection = pypyodbc.connect('Driver={SQL Server};Server=myServerName;Database=myDatabBaseName;Trusted_Connection=yes;')
except pypyodbc.Error as ex:
sqlstate = ex.args[0]
if sqlstate == '28000':
self.answer_label['text'] = "You do not have access."
cursor = connection.cursor()
SQLCommand = ("SELECT LASTNAME, FIRSTNAME, L_LICNUMBER "
"FROM dbo.my_table " # table name
"with (nolock)"
"WHERE FIRSTNAME = ?")
Values = [firstname]
cursor.execute(SQLCommand,Values)
results = cursor.fetchmany(10)
if results:
self.output0['text'] = (results[0]) # display results
self.output1['text'] = (results[1])
self.output2['text'] = (results[2])
self.output3['text'] = (results[3])
self.output4['text'] = (results[4])
self.output5['text'] = (results[5])
self.output6['text'] = (results[6])
self.output7['text'] = (results[7])
self.output8['text'] = (results[8])
self.output9['text'] = (results[9])
connection.close()
else:
self.output['text'] = "That name does not exist."
您必须创建一组LabelFrame、列表框或消息来显示结果
self.output0 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output0.grid(column=0, row=6, columnspan=5, padx=0, pady=0)
self.output1 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output1.grid(column=0, row=7, columnspan=5, padx=0, pady=0)
self.output2 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output2.grid(column=0, row=8, columnspan=5, padx=0, pady=0)
self.output3 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output3.grid(column=0, row=9, columnspan=5, padx=0, pady=0)
self.output4 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output4.grid(column=0, row=10, columnspan=5, padx=0, pady=0)
self.output5 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output5.grid(column=0, row=11, columnspan=5, padx=0, pady=0)
self.output6 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output6.grid(column=0, row=12, columnspan=5, padx=0, pady=0)
self.output7 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output7.grid(column=0, row=13, columnspan=5, padx=0, pady=0)
self.output8 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output8.grid(column=0, row=14, columnspan=5, padx=0, pady=0)
self.output9 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output9.grid(column=0, row=15, columnspan=5, padx=0, pady=0)
我找了一整天才找到解决办法。我在谷歌上找不到任何东西。 偏移行和获取不起作用 这是:
def calculate(self):
firstname = str(self.first_entry.get())
lastname = str(self.last_entry.get())
license = str(self.lic_entry.get())
if (firstname and not lastname and not license): # "You entered first name."
try:
connection = pypyodbc.connect('Driver={SQL Server};Server=myServerName;Database=myDatabBaseName;Trusted_Connection=yes;')
except pypyodbc.Error as ex:
sqlstate = ex.args[0]
if sqlstate == '28000':
self.answer_label['text'] = "You do not have access."
cursor = connection.cursor()
SQLCommand = ("SELECT LASTNAME, FIRSTNAME, L_LICNUMBER "
"FROM dbo.my_table " # table name
"with (nolock)"
"WHERE FIRSTNAME = ?")
Values = [firstname]
cursor.execute(SQLCommand,Values)
results = cursor.fetchmany(10)
if results:
self.output0['text'] = (results[0]) # display results
self.output1['text'] = (results[1])
self.output2['text'] = (results[2])
self.output3['text'] = (results[3])
self.output4['text'] = (results[4])
self.output5['text'] = (results[5])
self.output6['text'] = (results[6])
self.output7['text'] = (results[7])
self.output8['text'] = (results[8])
self.output9['text'] = (results[9])
connection.close()
else:
self.output['text'] = "That name does not exist."
您必须创建一组LabelFrame、列表框或消息来显示结果
self.output0 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output0.grid(column=0, row=6, columnspan=5, padx=0, pady=0)
self.output1 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output1.grid(column=0, row=7, columnspan=5, padx=0, pady=0)
self.output2 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output2.grid(column=0, row=8, columnspan=5, padx=0, pady=0)
self.output3 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output3.grid(column=0, row=9, columnspan=5, padx=0, pady=0)
self.output4 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output4.grid(column=0, row=10, columnspan=5, padx=0, pady=0)
self.output5 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output5.grid(column=0, row=11, columnspan=5, padx=0, pady=0)
self.output6 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output6.grid(column=0, row=12, columnspan=5, padx=0, pady=0)
self.output7 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output7.grid(column=0, row=13, columnspan=5, padx=0, pady=0)
self.output8 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output8.grid(column=0, row=14, columnspan=5, padx=0, pady=0)
self.output9 = tkinter.LabelFrame(self, height=20, width=830, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output9.grid(column=0, row=15, columnspan=5, padx=0, pady=0)
我不认为这是问题所在,但如果第二个查询(在
连接之前.close()
)是有意的,那么在最后一个查询之后您似乎没有else
?是的。我把它放在那里尝试它,我仍然得到标量变量错误。我认为这可能与用户输入被用作变量有关。它只允许我在第一次查询中使用它一次。我不认为这是问题所在,但如果第二次查询(在连接之前.close()
)是有意的,那么在最后一次查询之后您似乎没有else
。是的。我把它放在那里尝试它,我仍然得到标量变量错误。我认为这可能与用户输入被用作变量有关。它只允许我在第一次查询中使用它一次。