Every time I try to save data to my contacts database I get an error. The script runs fine, but when it come to clicking save I get this error.
Traceback (most recent call last):
File "C:\Users\Panic\Desktop\EminentGeekTechnology\Looking For Who\addnew.py", line 71, in OnSave
self.database.execute('INSERT INTO contacts (fname) VALUES (null,?),' [fname])
AttributeError: 'contact' object has no attribute 'database'
#Importing modules
import os
import wx
import sqlite3
import Database
#This will create a class
class contact(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Add Contact', size = (455,170))
panel = wx.Panel(self, -1)
#This will center the Frame
self.Centre()
#This will create input fields for the user
fnameLabel = wx.StaticText(panel, -1, " First Name:")
fname = wx.TextCtrl(panel, -1, "")
fnameLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
fname.SetInsertionPoint(0)
lnameLabel = wx.StaticText(panel, -1, " Last Name:")
lname = wx.TextCtrl(panel, -1, "")
lnameLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
phoneLabel = wx.StaticText(panel, -1, " Phone:")
phone = wx.TextCtrl(panel, -1, "")
phoneLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
addressLabel = wx.StaticText(panel, -1, " Address:")
address = wx.TextCtrl(panel, -1, "")
addressLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
emailLabel = wx.StaticText(panel, -1, " Email:")
email = wx.TextCtrl(panel, -1, "")
emailLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
#This will size the fields
sizer = wx.FlexGridSizer(cols = 4, hgap = 6, vgap = 6)
sizer.AddMany([fnameLabel, fname, lnameLabel, lname, phoneLabel, phone, addressLabel, address, emailLabel, email,])
panel.SetSizer(sizer)
save = wx.Button(panel, label='Save',pos=(150, 90), size=(80, 25))
close = wx.Button(panel, label='Close',pos=(250, 90), size=(80, 25))
save.Bind(wx.EVT_BUTTON, self.OnSave, save)
close.Bind(wx.EVT_BUTTON, self.OnClose, close)
def OnSave(self, event):
dlg = wx.MessageBox("Are you sure you want to save contact?", 'Message', wx.YES_NO | wx.ICON_QUESTION)
if (dlg == wx.YES):
connection = sqlite3.connect('contacts.db')
self.database.execute('INSERT INTO contacts (fname) VALUES (null,?),' [fname])
self.database.execute('INSERT INTO contacts (lname) VALUES (null,?),' [lname])
self.database.execute('INSERT INTO contacts (phonenumber) VALUES (null,?),' [phone])
self.database.execute('INSERT INTO contacts (address) VALUES (null,?),' [address])
self.database.execute('INSERT INTO contacts (email) VALUES (null,?),' [email])
self.connection.commit()
database.close()
if (dlg == wx.NO):
self.Destroy()
else:
return
def OnClose(self, event):
dlg = wx.MessageBox("Are you sure you want to cancel?", 'Message', wx.YES_NO | wx.ICON_QUESTION)
if (dlg == wx.YES):
self.Destroy()
else:
return
if __name__ == '__main__':
myApp = wx.App()
frame = contact()
frame.Show()
myApp.MainLoop()