Reputation: 1017
I have been trying a module named MySQLdb for python, as it all seemed to be working ok, but when I submit my data, nothing is being inserted into the actual DB. I am going off of what I know through PHP, and I have looked up the documentation, but there doesn't seem to be anything useful. I just wanted to practice with Python and Databases. Here is my current Code (Please don't judge the code, only the parts where mysql is involved, unless of course I am majorly messing up!):
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()
Upvotes: 0
Views: 1209
Reputation: 55952
Using MySQLdb, you can do something like:
self.db = connect("127.0.0.1", "user", "pass", "dbname")
self.cursor = self.db.cusor()
self.cursor.execute("""INSERT INTO test (fName,lName, DOB, email)
VALUES ('self.firstName', 'self.lastName', 'self.DOBirth', 'self.emailAddress')""")
self.db.commit()
Found in the tutorial http://zetcode.com/db/mysqlpython/
it looks like you are using some examples from _mysql
module?
Upvotes: 1