wxPython and Sqlite3 database problem

Thread Solved

Join Date: Oct 2009
Posts: 10
Reputation: Megabyte89 is an unknown quantity at this point 
Solved Threads: 0
Megabyte89 Megabyte89 is offline Offline
Newbie Poster
 
0
  #11
Oct 12th, 2009
Thank you all for your help. I will see if it works.
Last edited by Megabyte89; Oct 12th, 2009 at 9:46 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: Megabyte89 is an unknown quantity at this point 
Solved Threads: 0
Megabyte89 Megabyte89 is offline Offline
Newbie Poster
 
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

  1. #Importing modules
  2.  
  3. import os
  4. import wx
  5. import sqlite3
  6. import Database
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. #This will create a class
  17.  
  18. class contact(wx.Frame):
  19. def __init__(self):
  20. wx.Frame.__init__(self, None, -1, 'Add Contact', size = (455,170))
  21. panel = wx.Panel(self, -1)
  22.  
  23. #This will center the Frame
  24.  
  25. self.Centre()
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. #This will create input fields for the user
  33.  
  34. fnameLabel = wx.StaticText(panel, -1, " First Name:")
  35. fname = wx.TextCtrl(panel, -1, "")
  36. fnameLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
  37. fname.SetInsertionPoint(0)
  38.  
  39. lnameLabel = wx.StaticText(panel, -1, " Last Name:")
  40. lname = wx.TextCtrl(panel, -1, "")
  41. lnameLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
  42.  
  43. phoneLabel = wx.StaticText(panel, -1, " Phone:")
  44. phone = wx.TextCtrl(panel, -1, "")
  45. phoneLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
  46.  
  47. addressLabel = wx.StaticText(panel, -1, " Address:")
  48. address = wx.TextCtrl(panel, -1, "")
  49. addressLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
  50.  
  51. emailLabel = wx.StaticText(panel, -1, " Email:")
  52. email = wx.TextCtrl(panel, -1, "")
  53. emailLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
  54.  
  55.  
  56. #This will size the fields
  57.  
  58. sizer = wx.FlexGridSizer(cols = 4, hgap = 6, vgap = 6)
  59. sizer.AddMany([fnameLabel, fname, lnameLabel, lname, phoneLabel, phone, addressLabel, address, emailLabel, email,])
  60. panel.SetSizer(sizer)
  61.  
  62. save = wx.Button(panel, label='Save',pos=(150, 90), size=(80, 25))
  63. close = wx.Button(panel, label='Close',pos=(250, 90), size=(80, 25))
  64.  
  65. save.Bind(wx.EVT_BUTTON, self.OnSave, save)
  66. close.Bind(wx.EVT_BUTTON, self.OnClose, close)
  67.  
  68. self.database = Database.db
  69.  
  70.  
  71. def OnSave(self, event):
  72. dlg = wx.MessageBox("Are you sure you want to save contact?", 'Message', wx.YES_NO | wx.ICON_QUESTION)
  73.  
  74.  
  75. if (dlg == wx.YES):
  76. connection = sqlite3.connect('contacts.db')
  77. self.database.execute('INSERT INTO contacts (fname) VALUES (null,?),' [fname])
  78. self.database.execute('INSERT INTO contacts (lname) VALUES (null,?),' [lname])
  79. self.database.execute('INSERT INTO contacts (phonenumber) VALUES (null,?),' [phone])
  80. self.database.execute('INSERT INTO contacts (address) VALUES (null,?),' [address])
  81. self.database.execute('INSERT INTO contacts (email) VALUES (null,?),' [email])
  82. connection.commit()
  83. database.close()
  84. if (dlg == wx.NO):
  85. self.Destroy()
  86. else:
  87. return
  88.  
  89. def OnClose(self, event):
  90. dlg = wx.MessageBox("Are you sure you want to cancel?", 'Message', wx.YES_NO | wx.ICON_QUESTION)
  91.  
  92. if (dlg == wx.YES):
  93. self.Destroy()
  94. else:
  95. return
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. if __name__ == '__main__':
  103. myApp = wx.App()
  104. frame = contact()
  105. frame.Show()
  106. myApp.MainLoop()
Last edited by Megabyte89; Oct 12th, 2009 at 9:51 am. Reason: spelling
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: Megabyte89 is an unknown quantity at this point 
Solved Threads: 0
Megabyte89 Megabyte89 is offline Offline
Newbie Poster
 
0
  #13
Oct 12th, 2009
Still need help with this.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,919
Reputation: evstevemd is a jewel in the rough evstevemd is a jewel in the rough evstevemd is a jewel in the rough 
Solved Threads: 177
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Posting Virtuoso
 
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
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,919
Reputation: evstevemd is a jewel in the rough evstevemd is a jewel in the rough evstevemd is a jewel in the rough 
Solved Threads: 177
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Posting Virtuoso
 
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
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: Megabyte89 is an unknown quantity at this point 
Solved Threads: 0
Megabyte89 Megabyte89 is offline Offline
Newbie Poster
 
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


  1. #Importing modules
  2.  
  3. import os
  4. import wx
  5. import sqlite3
  6. import Database
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. #This will create a class
  17.  
  18. class contact(wx.Frame):
  19. def __init__(self):
  20. wx.Frame.__init__(self, None, -1, 'Add Contact', size = (455,170))
  21. panel = wx.Panel(self, -1)
  22.  
  23. #This will center the Frame
  24.  
  25. self.Centre()
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. #This will create input fields for the user
  33.  
  34. fnameLabel = wx.StaticText(panel, -1, " First Name:")
  35. self.fname = wx.TextCtrl(panel, -1, "")
  36. fnameLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
  37. self.fname.SetInsertionPoint(0)
  38.  
  39. lnameLabel = wx.StaticText(panel, -1, " Last Name:")
  40. self.lname = wx.TextCtrl(panel, -1, "")
  41. lnameLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
  42.  
  43. phoneLabel = wx.StaticText(panel, -1, " Phone:")
  44. self.phone = wx.TextCtrl(panel, -1, "")
  45. phoneLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
  46.  
  47. addressLabel = wx.StaticText(panel, -1, " Address:")
  48. self.address = wx.TextCtrl(panel, -1, "")
  49. addressLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
  50.  
  51. emailLabel = wx.StaticText(panel, -1, " Email:")
  52. self.email = wx.TextCtrl(panel, -1, "")
  53. emailLabel.SetFont(wx.Font (10, wx.SWISS, wx.NORMAL, wx.BOLD))
  54.  
  55.  
  56. #This will size the fields
  57.  
  58. sizer = wx.FlexGridSizer(cols = 4, hgap = 6, vgap = 6)
  59. sizer.AddMany([fnameLabel, self.fname, lnameLabel, self.lname, phoneLabel, self.phone, addressLabel, self.address, emailLabel, self.email,])
  60. panel.SetSizer(sizer)
  61.  
  62. save = wx.Button(panel, label='Save',pos=(150, 90), size=(80, 25))
  63. close = wx.Button(panel, label='Close',pos=(250, 90), size=(80, 25))
  64.  
  65. save.Bind(wx.EVT_BUTTON, self.OnSave, save)
  66. close.Bind(wx.EVT_BUTTON, self.OnClose, close)
  67.  
  68. self.database = Database.db
  69.  
  70.  
  71.  
  72.  
  73. def OnSave(self, event):
  74. dlg = wx.MessageBox("Are you sure you want to save contact?", 'Message', wx.YES_NO | wx.ICON_QUESTION)
  75.  
  76.  
  77. if (dlg == wx.YES):
  78. connection = sqlite3.connect('contacts.db')
  79. self.database.execute('INSERT INTO contacts (fname) VALUES (null,?),' [fname])
  80. self.database.execute('INSERT INTO contacts (lname) VALUES (null,?),' [lname])
  81. self.database.execute('INSERT INTO contacts (phonenumber) VALUES (null,?),' [phone])
  82. self.database.execute('INSERT INTO contacts (address) VALUES (null,?),' [address])
  83. self.database.execute('INSERT INTO contacts (email) VALUES (null,?),' [email])
  84. connection.commit()
  85. database.close()
  86. if (dlg == wx.NO):
  87. self.Destroy()
  88. else:
  89. return
  90.  
  91. def OnClose(self, event):
  92. dlg = wx.MessageBox("Are you sure you want to cancel?", 'Message', wx.YES_NO | wx.ICON_QUESTION)
  93.  
  94. if (dlg == wx.YES):
  95. self.Destroy()
  96. else:
  97. return
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. if __name__ == '__main__':
  105. myApp = wx.App()
  106. frame = contact()
  107. frame.Show()
  108. myApp.MainLoop()
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,197
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 341
woooee woooee is offline Offline
Nearly a Posting Virtuoso
 
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.
  1. import sqlite3
  2. ##import addnew
  3.  
  4. class DBClass:
  5. def __init__(self):
  6. #Creating a connection to the database.
  7. self.connection = sqlite3.connect('contacts.db')
  8.  
  9. #Creating a cursor object to interact with the databae
  10. self.db = self.connection.cursor()
  11.  
  12. #creating tables
  13. self.db.execute("CREATE TABLE IF NOT EXISTS contacts (fname VARCHAR, lname VARCHAR NOT NULL PRIMARY KEY, phonenumber VARCHAR, address VARCHAR, email VARCHAR)")
  14. self.connection.commit()
  15.  
  16. ##................................................................................................
  17. def __del__(self):
  18. self.db.close()
  19.  
  20. ##................................................................................................
  21. def add_record(self, add_tuple):
  22. """ receives a tuple and adds it to the database
  23. does not do any checking of data, so will add
  24. duplicate records, or malformed data
  25. to call this: self.database.add_record(tuple_to_add)
  26. """
  27. self.db.execute("insert into contacts (fname, lname, phonenumber, address, email) values (?, ?, ?, ?, ?)", add_tuple)
  28. self.connection.commit()
  29.  
  30. ##................................................................................................
  31. def print_all(self):
  32. """ Retrieves all rows as a sequence and prints that sequence:
  33. """
  34. self.db.execute("select * from contacts")
  35. for rec in self.db.fetchall():
  36. print rec
  37.  
  38. ##====================================================================
  39. ## simulate calling this from the wxpython program
  40. ##====================================================================
  41. class WXProgram:
  42. def __init__(self):
  43. self.database = DBClass()
  44. f_name = "abc.txt"
  45. last_name = "Smith"
  46. phone="555-5555"
  47. address="123 Main St."
  48. email="Smith@gmail.com"
  49. self.database.add_record( (f_name, last_name, phone, address, email) )
  50.  
  51. f_name = "def.txt"
  52. last_name = "Jones"
  53. phone="555-1111"
  54. address="345 First St."
  55. email="Jones@gmail.com"
  56. to_add = (f_name, last_name, phone, address, email)
  57. self.database.add_record(to_add)
  58.  
  59. print "printing records"
  60. self.database.print_all()
  61.  
  62. ## we don't have to explictly close the class, but it can be
  63. ## done by assigning self.database some other value
  64. self.database = None
  65.  
  66. ##====================================================================
  67. if __name__ == "__main__":
  68. WX=WXProgram()
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: Megabyte89 is an unknown quantity at this point 
Solved Threads: 0
Megabyte89 Megabyte89 is offline Offline
Newbie Poster
 
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,919
Reputation: evstevemd is a jewel in the rough evstevemd is a jewel in the rough evstevemd is a jewel in the rough 
Solved Threads: 177
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Posting Virtuoso
 
0
  #19
Oct 15th, 2009
Have a good go!
  1. self.enjoy
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 984 | Replies: 18
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC