Python不会从mysql表中检索数据

时间:2019-04-02 20:47:14

标签: mysql xampp python-3.7

我试图从mysql表(命名用户)中检索entry_time,然后找到现在(exitTime)和entry_time之间的时间差。我的代码似乎无法从mysql表中恢复数据。 f是代码的ID搜索关键字,该代码针对mysql数据库的ID(PRIMARY KEY)进行搜索以找到相应的entry_time。

我也尝试将其作为字符串查看是否可以检索该值但无济于事。

from tkinter import *
import time,datetime
import mysql.connector as mc
from tkinter import messagebox
import cv2
import matplotlib.pyplot as  plt
import sys
import time





def billCalc():
    global exitTime
    global EntryTime
    EntryTime = datetime.datetime
    exitTime = datetime.datetime.now()
    try:
        conn = mc.connect(host='localhost', user='root', password='', db='car_park_master')

    except mc.Error as e:
        print("Error %d: %s" % (e.args[0], e.args[1]))
        sys.exit(1)


        sql_Query = "SELECT `entry_time` FROM `user` WHERE `ID` ="+f.get()
        #id = (f.get(),)
        print(record[1])
        cursor = conn.cursor(buffered=True)
        cursor.execute(sql_Query, id)
        record = cursor.fetchone()
        # selecting column value into varible
        EntryTime = datetime(record[1])
        print(record[1])




    conn.close()
    print(exitTime)
    print(EntryTime)
    print(f.get())
    BillTime = EntryTime - exitTime
    Bill = BillTime * 2
    print(Bill.get())




def main_screen():





    screen = Tk()
    screen.geometry("300x400")
    screen.title("EXIT GATE")


    global f
    f = StringVar()

    Entry(screen, textvariable=f).place(x=150, y=200)

    print(f.get())


    Button(screen,text="Exit",height='2',width='15',command=billCalc).place(x=150,y=300)
    screen.mainloop()

main_screen()

它应该使用datetime.datetime.now()执行退出时间。 然后,应该从用户那里获取输入ID以在数据库中进行搜索。 之后,应该检索相应的输入时间 查找进入时间和退出时间之间的差异(以秒为单位) 然后提供帐单。

修改后的固定代码:

from tkinter import *
import datetime
import mysql.connector as mc
from tkinter import messagebox
import cv2
import matplotlib.pyplot as  plt
import sys
import time






def billCalc():
    global exitTime
    global EntryTime
    EntryTime = datetime.datetime
    exitTime = datetime.datetime.now()
    try:
        conn = mc.connect(host='localhost', user='root', password='', db='car_park_master')

    except mc.Error as e:
        print("Error %d: %s" % (e.args[0], e.args[1]))
        sys.exit(1)



    cursor = conn.cursor()

    cursor.execute("SELECT entry_time FROM user where id="+f.get())
    record = cursor.fetchone()
    EntryTime = (record[0])



    conn.close()
    print(exitTime)
    print(EntryTime)
    print(f.get())

    BillTime = exitTime - EntryTime


    print(BillTime)




def main_screen():





    screen = Tk()
    screen.geometry("300x400")
    screen.title("EXIT GATE")


    global f
    f = StringVar()

    Entry(screen, textvariable=f).place(x=150, y=200)

    print(f.get())


    Button(screen,text="Exit",height='2',width='15',command=billCalc).place(x=150,y=300)
    screen.mainloop()

main_screen()

0 个答案:

没有答案