| | |
wxPython and Sqlite3 database problem
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
0
#12 Oct 12th, 2009
Your advice help me fix the first problem, now I get a NameError after I run the script and click save.
Traceback (most recent call last):
File "C:\Users\Panic\Desktop\EminentGeekTechnology\Looking For Who\addnew.py", line 77, in OnSave
self.database.execute('INSERT INTO contacts (fname) VALUES (null,?),' [fname])
NameError: global name 'fname' is not defined
Traceback (most recent call last):
File "C:\Users\Panic\Desktop\EminentGeekTechnology\Looking For Who\addnew.py", line 77, in OnSave
self.database.execute('INSERT INTO contacts (fname) VALUES (null,?),' [fname])
NameError: global name 'fname' is not defined
Python Syntax (Toggle Plain Text)
#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) self.database = Database.db 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]) 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()
Last edited by Megabyte89; Oct 12th, 2009 at 9:51 am. Reason: spelling
0
#14 Oct 13th, 2009
replace fname throughout with self.fname
That is because self.fname is accessible throughout the class
try and see what happens
That is because self.fname is accessible throughout the class
try and see what happens
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
"Try to be expert in everything and you become expert in nothing" - Me
---------------------------------
Windows Vista Home Premium SP2| MINGW 4.4.0|PHP 5.3.1|Python 2.6.4|JDK 1.6
Theist: It's okay, can you imagine anything else that doesn't exist?
"Try to be expert in everything and you become expert in nothing" - Me
---------------------------------
Windows Vista Home Premium SP2| MINGW 4.4.0|PHP 5.3.1|Python 2.6.4|JDK 1.6
0
#15 Oct 13th, 2009
replace fname throughout with self.fname
That is because self.fname is accessible throughout the class
try and see what happens
That is because self.fname is accessible throughout the class
try and see what happens
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
"Try to be expert in everything and you become expert in nothing" - Me
---------------------------------
Windows Vista Home Premium SP2| MINGW 4.4.0|PHP 5.3.1|Python 2.6.4|JDK 1.6
Theist: It's okay, can you imagine anything else that doesn't exist?
"Try to be expert in everything and you become expert in nothing" - Me
---------------------------------
Windows Vista Home Premium SP2| MINGW 4.4.0|PHP 5.3.1|Python 2.6.4|JDK 1.6
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
0
#16 Oct 13th, 2009
I tried that and I get the same error, I am probably doing something wrong.
Traceback (most recent call last):
File "C:\Users\Panic\Desktop\EminentGeekTechnology\Looking For Who\addnew.py", line 79, in OnSave
self.database.execute('INSERT INTO contacts (fname) VALUES (null,?),' [fname])
NameError: global name 'fname' is not defined
Traceback (most recent call last):
File "C:\Users\Panic\Desktop\EminentGeekTechnology\Looking For Who\addnew.py", line 79, in OnSave
self.database.execute('INSERT INTO contacts (fname) VALUES (null,?),' [fname])
NameError: global name 'fname' is not defined
Python Syntax (Toggle Plain Text)
#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:") self.fname = wx.TextCtrl(panel, -1, "") fnameLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD)) self.fname.SetInsertionPoint(0) lnameLabel = wx.StaticText(panel, -1, " Last Name:") self.lname = wx.TextCtrl(panel, -1, "") lnameLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD)) phoneLabel = wx.StaticText(panel, -1, " Phone:") self.phone = wx.TextCtrl(panel, -1, "") phoneLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD)) addressLabel = wx.StaticText(panel, -1, " Address:") self.address = wx.TextCtrl(panel, -1, "") addressLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD)) emailLabel = wx.StaticText(panel, -1, " Email:") self.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, self.fname, lnameLabel, self.lname, phoneLabel, self.phone, addressLabel, self.address, emailLabel, self.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) self.database = Database.db 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]) 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()
•
•
Join Date: Dec 2006
Posts: 1,197
Reputation:
Solved Threads: 341
0
#17 Oct 13th, 2009
You want to keep all of the sqlite stuff in Database.py and call the add, change, delete functions from the WX program. An example using addition.
Python Syntax (Toggle Plain Text)
import sqlite3 ##import addnew class DBClass: def __init__(self): #Creating a connection to the database. self.connection = sqlite3.connect('contacts.db') #Creating a cursor object to interact with the databae self.db = self.connection.cursor() #creating tables self.db.execute("CREATE TABLE IF NOT EXISTS contacts (fname VARCHAR, lname VARCHAR NOT NULL PRIMARY KEY, phonenumber VARCHAR, address VARCHAR, email VARCHAR)") self.connection.commit() ##................................................................................................ def __del__(self): self.db.close() ##................................................................................................ def add_record(self, add_tuple): """ receives a tuple and adds it to the database does not do any checking of data, so will add duplicate records, or malformed data to call this: self.database.add_record(tuple_to_add) """ self.db.execute("insert into contacts (fname, lname, phonenumber, address, email) values (?, ?, ?, ?, ?)", add_tuple) self.connection.commit() ##................................................................................................ def print_all(self): """ Retrieves all rows as a sequence and prints that sequence: """ self.db.execute("select * from contacts") for rec in self.db.fetchall(): print rec ##==================================================================== ## simulate calling this from the wxpython program ##==================================================================== class WXProgram: def __init__(self): self.database = DBClass() f_name = "abc.txt" last_name = "Smith" phone="555-5555" address="123 Main St." email="Smith@gmail.com" self.database.add_record( (f_name, last_name, phone, address, email) ) f_name = "def.txt" last_name = "Jones" phone="555-1111" address="345 First St." email="Jones@gmail.com" to_add = (f_name, last_name, phone, address, email) self.database.add_record(to_add) print "printing records" self.database.print_all() ## we don't have to explictly close the class, but it can be ## done by assigning self.database some other value self.database = None ##==================================================================== if __name__ == "__main__": WX=WXProgram()
Linux counter #99383
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
0
#18 Oct 14th, 2009
Well with all your help and the help of this website here. I re-did my add script and finally got it to work. Thanks
Now I am off to do a script that deletes, searches, and edits.
Now I am off to do a script that deletes, searches, and edits.
0
#19 Oct 15th, 2009
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
"Try to be expert in everything and you become expert in nothing" - Me
---------------------------------
Windows Vista Home Premium SP2| MINGW 4.4.0|PHP 5.3.1|Python 2.6.4|JDK 1.6
Theist: It's okay, can you imagine anything else that doesn't exist?
"Try to be expert in everything and you become expert in nothing" - Me
---------------------------------
Windows Vista Home Premium SP2| MINGW 4.4.0|PHP 5.3.1|Python 2.6.4|JDK 1.6
![]() |
Similar Threads
- c # database problem (C#)
- Database problem!! (Visual Basic 4 / 5 / 6)
- database problem (C#)
- JSP, database problem (Urgent) (JSP)
- Database Problem......... (MySQL)
- picture saving in database. problem (Visual Basic 4 / 5 / 6)
- Populating the DataBase problem (ASP)
- updating database problem (PHP)
- Problem printing from IE6 (Web Browsers)
Other Threads in the Python Forum
- Previous Thread: Simple HTML Parsing Question
- Next Thread: Need an interface for python 2.6.3 and mysql-5.1.31
Views: 984 | Replies: 18
| Thread Tools | Search this Thread |
Tag cloud for Python
application array beginner c++ c/c++ change character class client code command conditions convert count create csv ctypes database dictionary django dll error examples excel exe extensions fdlib file float format framework ftp function game graphics gui homework image images import input library line linux list lists logging loop loops microcontroller mouse mysql mysqldb number numbers output parse parsing path port prime processing program programming py2exe pygame pygtk pyqt python random raw_input recursion recursive redirect remote scrolledtext server socket ssh stdout string strings syntax table terminal text thread threading tkinter transparency tuple tutorial ubuntu unicode variable variables veloicty web windows wxpython






