如何使用“入口”点将数据输入sqlite数据库?

时间:2019-08-12 23:43:26

标签: python sqlite tkinter

我需要在“SeniorCafé.db”“订单”中输入“条目”。使用用户名“ test”和密码“ 123”登录并单击“创建新订单”后,将显示这些条目。

我尝试使用我认为可行的方式,即在创建时添加一个确定性,以便在其中获取条目并将其输入数据库。

conn = sqlite3.connect("SeniorCafé.db")
cursor = conn.cursor()

    def Create():
        if  UserE.get() == "" or FnameE.get() == "" or SnameE.get() == "" or GradeE.get() == "" or HouseE.get() == "" or DateE.get() == "" or MilkE.get() == "" or CookieE.get() == "" or NoodlesE.get() == "":
        txt_result.config(text="Please complete the required field!", fg="red")
        else:
            cursor.execute("INSERT INTO `Order` (Username, Firstname, Surname, Grade, House, Milkshakeqty, Cookieqty, noodlesqty) VALUES(?, ?, ?, ?, ?, ?, ?, ?)", (str(UserL.get()), str(FnameL.get()), str(SnameL.get()), str(GradeL.get()), str(HouseL.get()), str(Milkshakeqty.get()), str(Cookieqty.get()), str(noodlesqty.get())))

这是我的代码,

import tkinter
import _tkinter
import sqlite3
import math





from tkinter import *

def adminLogin():
    global AnameEL
    global ApwordEL # More globals :D
    global ArootA
    global f1



    ArootA = Tk() # This now makes a new window.
    ArootA.geometry('800x400')
    ArootA.title('Login') # This makes the window title 'login'

    f1 = Frame(width=200, height=200, background="#D3D3D3")
    f2 = Frame(ArootA, width=400, height=200)

    f1.pack(fill="both", expand=True, padx=0, pady=0)
    f2.place(in_=f1, anchor="c", relx=.5, rely=.5)

    AnameL = Label(f2, text='Username: ') # More labels
    ApwordL = Label(f2, text='Password: ') # ^
    AnameL.grid(row=1, sticky=W)
    ApwordL.grid(row=2, sticky=W)

    AnameEL = Entry(f2) # The entry input
    ApwordEL = Entry(f2, show='*')
    AnameEL.grid(row=1, column=1)
    ApwordEL.grid(row=2, column=1)

    AloginB = Button(f2, text='Login', command=CheckAdmin) # This makes the login button, which will go to the CheckLogin def.
    AloginB.grid(columnspan=2, sticky=W)

def CheckAdmin():
    global r

    if AnameEL.get() == "test" and ApwordEL.get() == "123" : # Checks to see if you entered the correct data.
        r = Tk() # Opens new window
        r.title('Sucess')
        loginC = Button(r, text='Add new Order', command=Signup)
        loginC.grid(columnspan=2, sticky=W)
        r.mainloop()
    else:


        r = Tk()
        r.destroy()
        f1.destroy() # Removes everything currently inside ArootA.
        ArootA.geometry('550x450')
        ArootA.title('Error')
        rlbl = Label(ArootA, text='\n[!] Invalid Login')
        rlbl.pack()
        r.mainloop()

def Signup(): # This is the signup definition, 
global SnameE # These globals just make the variables global to the entire script, meaning any definition can use them
    global UserE
    global FnameE
    global GradeE
    global HouseE
    global roots

    roots = Tk() # This creates the window, just a blank one.
    roots.title('Signup') # This renames the title of said window to 'signup'
    intruction = Label(roots, text='Please Enter new Credidentials\n') # This puts a label, so just a piece of text saying 'please enter blah'
    intruction.grid(row=0, column=0, sticky=E) # This just puts it in the window, on row 0, col 0. If you want to learn more look up a tkinter tutorial :)

    UserL = Label(roots, text='Username: ') # This just does the same as above, instead with the text new username.
    #pwordL = Label(roots, text='New Password: ') # ^^
    FnameL = Label(roots, text='First name: ')
    SnameL = Label(roots, text='Surname: ')
    GradeL = Label(roots, text='Grade: ')
    HouseL = Label(roots, text='House: ')
    DateL = Label(roots, text='Date(weekterm): ')
    CookieL = Label(roots, text='Cookie qty: ')
    MilkL = Label(roots, text='Milkshake qty: ')
    NoodlesL = Label(roots, text='Noodles qty: ')
    UserL.grid(row=1, column=0, sticky=W) # Same thing as the instruction var just on different rows. :) Tkinter is like that.
    #pwordL.grid(row=2, column=0, sticky=W) # ^^
    FnameL.grid(row=2, column=0, sticky=W)
    SnameL.grid(row=3, column=0, sticky=W)
    GradeL.grid(row=4, column=0, sticky=W)
    HouseL.grid(row=5, column=0, sticky=W)
    DateL.grid(row=6, column=0, sticky=W)
    MilkL.grid(row=7, column=0, sticky=W)
    CookieL.grid(row=8, column=0, sticky=W)
    NoodlesL.grid(row=9, column=0, sticky=W)

    UserE = Entry(roots) # This now puts a text box waiting for input.
    FnameE = Entry(roots) 
    SnameE = Entry(roots)
    GradeE = Entry(roots)
    HouseE = Entry(roots)
    DateE = Entry(roots)
    MilkE = Entry(roots)
    CookieE = Entry(roots)
    NoodlesE = Entry(roots)


    UserE.grid(row=1, column=1) # You know what this does now :D
    SnameE.grid(row=2, column=1) # ^^
    FnameE.grid(row=3, column=1)
    GradeE.grid(row=4, column=1)
    HouseE.grid(row=5, column=1)
    DateE.grid(row=6, column=1)
    MilkE.grid(row=7, column=1)
    CookieE.grid(row=8, column=1)
    NoodlesE.grid(row=9, column=1)

    conn = sqlite3.connect("SeniorCafé.db")
    cursor = conn.cursor()

    def Create():
        if  UserE.get() == "" or FnameE.get() == "" or SnameE.get() == "" or GradeE.get() == "" or HouseE.get() == "" or DateE.get() == "" or MilkE.get() == "" or CookieE.get() == "" or NoodlesE.get() == "":
        txt_result.config(text="Please complete the required field!", fg="red")
        else:
            cursor.execute("INSERT INTO `Order` (Username, Firstname, Surname, Grade, House, Milkshakeqty, Cookieqty, noodlesqty) VALUES(?, ?, ?, ?, ?, ?, ?, ?)", (str(UserL.get()), str(FnameL.get()), str(SnameL.get()), str(GradeL.get()), str(HouseL.get()), str(Milkshakeqty.get()), str(Cookieqty.get()), str(noodlesqty.get())))




    signupButton = Button(roots, text='Signup', command=Create) # This creates the button with the text 'signup', when you click it, the command 'fssignup' will run. which is the def
    signupButton.grid(columnspan=2, sticky=W)











adminLogin()

我希望这些条目将输入到数据库中。

0 个答案:

没有答案