【问题标题】:How Python can display data on Label by Access (SQL)?Python 如何在 Label by Access (SQL) 上显示数据?
【发布时间】:2018-02-18 08:13:46
【问题描述】:

我希望 Python 在标签上显示来自 Access 的数据。我试图这样做,但它不起作用。没有错误,但标签显示pypyodbc.Cursor object at 0x05E02990。我该怎么办?谢谢

from tkinter import *
import pypyodbc
import ctypes

#Create connection
con = pypyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;UserCommitSync=Yes;Threads=3;SafeTransactions=0;PageTimeout=5;MaxScanRows=8;MaxBufferSize=2048;FIL={MS Access};DriverId=25;DefaultDir=C:/Users/HP/Desktop/PITL;DBQ=C:/Users/HP/Desktop/PITL/PITL.mdb;')
cursor = con.cursor ()

form = Tk ()
form.title ("Main")
form.geometry ('400x400')

form2 = Tk ()
form2.title ("Main")
form2.geometry ('400x400')
form2.withdraw()

def Show():
    cursor.execute ("SELECT Law_ID FROM Laws WHERE Fine=1")
    a=cursor.execute
    for a in cursor:
        print ("Law ID where Fine is 1 is", a)
        Label(form2, text=cursor).pack() 
    form.withdraw()
    form2.deiconify()

Button(form, text = 'PUSH ME', command = Show).pack()

form.mainloop ()

con.commit ()
cursor.close ()
con.close ()

【问题讨论】:

  • print ("Law ID where Fine is 1 is", a) 替换为print ("Law ID where Fine is 1 is", list(a)) 会发生什么?
  • @Deadlock 它不会更改标签中的文本

标签: python sql ms-access select tkinter


【解决方案1】:

错误是因为您打印的光标对象的值始终是一个位置,在您的情况下为“0x05E02990”。要获取光标检索到的值,您必须编写:

variable_name = cursor.fetchall()

在您的代码中:

def Show():
    cursor.execute ("SELECT Law_ID FROM Laws WHERE Fine=1")
    a=cursor.fetchall()
    ##print as you like    

它将获取光标检索到的所有数据

【讨论】:

    猜你喜欢
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多