我一直在尝试一个名为 MySQLdb for python 的模块,因为它似乎一切正常,但是当我提交我的数据时,没有任何东西被插入到实际的数据库中。我将通过 PHP 了解我所知道的内容,并且我已经查看了文档,但似乎没有任何有用的东西。我只是想练习 Python 和数据库。这是我目前的代码(请不要判断代码,只判断涉及mysql的部分,当然除非我主要搞砸了!):
from MySQLdb import *
from Tkinter import *
#Standard Imports (IMO)
import sys, random, math
class dataStoreTest:
def __init__(self):
self.root = Tk()
self.root.title("Database Test")
self.inputFrame = Frame(self.root)
#TextVariables
self.fName = StringVar()
self.lName = StringVar()
self.DOB = StringVar()
self.email = StringVar()
#MySQLdb Variables
self.connection = connect("127.0.0.1", "user", "pass", "dbname")
self.setupGUI()
self.root.mainloop()
def setupGUI(self):
#Title/Header
title = Label(self.root, text="DataBase Test", fg="Black")
title.pack()
#Input Fields
fNameLab = Label(self.root, text="First Name: ", width=14, fg="steelblue")
fNameLab.pack()
fNameEnt = Entry(self.root, textvariable=self.fName, width=14, bg="gray")
fNameEnt.pack()
lNameLab = Label(self.root, text="Last Name: ", width=14, fg="steelblue")
lNameLab.pack()
lNameEnt = Entry(self.root, textvariable=self.lName, width=14, bg="gray")
lNameEnt.pack()
dobLab = Label(self.root, text="DOB (YYYY-DD-MM):", width=18, fg="steelblue")
dobLab.pack()
dobEnt = Entry(self.root, textvariable=self.DOB, width=14, bg="gray")
dobEnt.pack()
emailLab = Label(self.root, text="E-Mail Address:", width=20, fg="steelblue")
emailLab.pack()
emailEnt = Entry(self.root, textvariable=self.email, width=35, bg="gray")
emailEnt.pack()
subBut = Button(self.root, command=self.verify, text="Submit")
subBut.pack()
def verify(self):
self.firstName = self.fName.get()
self.lastName = self.lName.get()
self.DOBirth = self.DOB.get()
self.emailAddress = self.email.get()
#Basic Validation
if self.firstName != "" and self.lastName != "" and self.DOBirth != "" and self.emailAddress != "":
if "@" in self.emailAddress and self.emailAddress.find("@") < (len(self.emailAddress)- 3):
print("All there!")
self.storeData()
else:
print("Invalid Email Address")
else:
print("Information Is Missing, Please Check Your Inputs.")
def storeData(self):
if self.connection:
print("DB Connection was a great success...")
print("Now entering data...")
self.connection.query("""INSERT INTO test (fName,lName, DOB, email)
VALUES ('self.firstName', 'self.lastName', 'self.DOBirth', 'self.emailAddress')""")
else:
print("Failed To Connect to DataBase :c ")
if __name__ == "__main__":
dataStoreTest()