Cannot insert into Database

I’m trying to insert data into a database using inbuilt python MySQLdb API’s, but the query is without effect everytime.

Please help.

---------------------------------- Code ---------------------------

#Boa:Dialog:Dialog1

import wx

import pdo

import MySQLdb

def create(parent):

return Dialog1(parent)

[wxID_DIALOG1, wxID_DIALOG1BTCANCEL, wxID_DIALOG1BTSAVE, wxID_DIALOG1CONNAME,

wxID_DIALOG1NUMBER, wxID_DIALOG1STNAME, wxID_DIALOG1STNUMBER,

] = [wx.NewId() for _init_ctrls in range(7)]

class Dialog1(wx.Dialog):

def _init_coll_flexGridSizer1_Items(self, parent):

generated method, don’t edit

parent.AddWindow(self.btSave, 0, border=5, flag=wx.ALL)

parent.AddWindow(self.btCancel, 0, border=5, flag=wx.ALL)

def _init_coll_boxSizer1_Items(self, parent):

generated method, don’t edit

parent.AddWindow(self.stName, 0, border=5, flag=wx.ALL)

parent.AddWindow(self.ConName, 0, border=5, flag=wx.ALL)

parent.AddWindow(self.stNumber, 0, border=5, flag=wx.ALL)

parent.AddWindow(self.Number, 0, border=5, flag=wx.ALL)

parent.AddSizer(self.flexGridSizer1, 0, border=0, flag=0)

def _init_sizers(self):

generated method, don’t edit

self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)

self.flexGridSizer1 = wx.FlexGridSizer(cols=0, hgap=0, rows=1, vgap=0)

self._init_coll_boxSizer1_Items(self.boxSizer1)

self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)

self.SetSizer(self.boxSizer1)

def _init_ctrls(self, prnt):

generated method, don’t edit

wx.Dialog.init(self, id=wxID_DIALOG1, name=u’’, parent=prnt,

pos=wx.Point(470, 298), size=wx.Size(223, 180),

style=wx.DEFAULT_DIALOG_STYLE, title=‘Add Contact’)

self.SetClientSize(wx.Size(215, 146))

self.stName = wx.StaticText(id=wxID_DIALOG1STNAME, label=u’Name’,

name=u’stName’, parent=self, pos=wx.Point(5, 5), size=wx.Size(27,

13), style=0)

self.ConName = wx.TextCtrl(id=wxID_DIALOG1CONNAME, name=u’ConName’,

parent=self, pos=wx.Point(5, 28), size=wx.Size(203, 21), style=0,

value=u’’)

self.stNumber = wx.StaticText(id=wxID_DIALOG1STNUMBER, label=u’Number’,

name=u’stNumber’, parent=self, pos=wx.Point(5, 59),

size=wx.Size(37, 13), style=0)

self.Number = wx.TextCtrl(id=wxID_DIALOG1NUMBER, name=u’Number’,

parent=self, pos=wx.Point(5, 82), size=wx.Size(203, 21), style=0,

value=u’’)

self.btSave = wx.Button(id=wxID_DIALOG1BTSAVE, label=u’Save’,

name=u’btSave’, parent=self, pos=wx.Point(5, 113),

size=wx.Size(75, 23), style=0)

self.btSave.Bind(wx.EVT_BUTTON, self.OnBtSaveButton,

id=wxID_DIALOG1BTSAVE)

self.btCancel = wx.Button(id=wxID_DIALOG1BTCANCEL, label=u’Cancel’,

name=u’btCancel’, parent=self, pos=wx.Point(90, 113),

size=wx.Size(75, 23), style=0)

self.btCancel.Bind(wx.EVT_BUTTON, self.OnBtCancelButton,

id=wxID_DIALOG1BTCANCEL)

self._init_sizers()

def init(self, parent):

self._init_ctrls(parent)

def OnBtSaveButton(self, event):

Name = self.ConName.GetValue()

Number = self.Number.GetValue()

if Name and Number :

db=pdo.connect(“Module=MySQLdb;User=root;Passwd=root;DB=smscentral”)

if db.active:

strInsert = “INSERT INTO contacts(Name,Number) VALUES(’”+str(Name)+"’,’"+str(Number)+"’)"

try:

db.execute(strInsert)

except:

print ‘Error Insert’

finally:

db.close()

self.Destroy()

db=MySQLdb.connect(host=‘localhost’, user=‘root’ , passwd=‘root’, db=‘smscentral’)

c=db.cursor()

strI = “INSERT INTO contacts(Name,Number) VALUES(’”+str(Name)+"’,’"+str(Number)+"’)"

c.execute(""“INSERT INTO contacts(Name,Number) VALUES(‘TAN’,‘99999999’)”"")

c.close()

db.close()

self.Destroy()

def OnBtCancelButton(self, event):

self.Destroy()

if name == ‘main’:

app = wx.PySimpleApp()

dlg = create(None)

try:

dlg.ShowModal()

finally:

dlg.Destroy()

app.MainLoop()

···

Regards
Tanay Shah

WHOA !!!

Thanks a lot man !!!

GREAT HELP !

Was trying to solve the problem since 4 hours !!!

···

2009/4/2 David Woods dwoods@wcer.wisc.edu

With MySQLdb, you either need to explicitly commit your transactions, or you need to turn autocommit on.

David

-----Original Message-----
From:
wxpython-users-bounces+dwoods=wcer.wisc.edu@lists.wxwidgets.org
[mailto:wxpython-users-bounces+dwoods=wcer.wisc.edu@lists.wxwidgets.org] ** On Behalf Of** Tanay Shah
Sent: Thursday, April 02, 2009 8:45 AM
To: wxpython-users@lists.wxwidgets.org
Subject:
[wxpython-users] Cannot insert into Database

I’m trying to insert data into a database using inbuilt python MySQLdb API’s, but the query is without effect everytime.
Please help.

---------------------------------- Code ---------------------------

#Boa:Dialog:Dialog1

import wx

import pdo

import MySQLdb

def create(parent):

return Dialog1(parent)

[wxID_DIALOG1, wxID_DIALOG1BTCANCEL, wxID_DIALOG1BTSAVE, wxID_DIALOG1CONNAME,

wxID_DIALOG1NUMBER, wxID_DIALOG1STNAME, wxID_DIALOG1STNUMBER,

] = [wx.NewId() for _init_ctrls in range(7)]

class Dialog1(wx.Dialog):

def _init_coll_flexGridSizer1_Items(self, parent):

generated method, don’t edit

parent.AddWindow(self.btSave, 0, border=5, flag=wx.ALL)

parent.AddWindow(self.btCancel, 0, border=5, flag=wx.ALL)

def _init_coll_boxSizer1_Items(self, parent):

generated method, don’t edit

parent.AddWindow(self.stName, 0, border=5, flag=wx.ALL)

parent.AddWindow(self.ConName, 0, border=5, flag=wx.ALL)

parent.AddWindow(self.stNumber, 0, border=5, flag=wx.ALL)

parent.AddWindow(self.Number, 0, border=5, flag=wx.ALL)

parent.AddSizer(self.flexGridSizer1, 0, border=0, flag=0)

def _init_sizers(self):

generated method, don’t edit

self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)

self.flexGridSizer1 = wx.FlexGridSizer(cols=0, hgap=0, rows=1, vgap=0)

self._init_coll_boxSizer1_Items(self.boxSizer1)

self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)

self.SetSizer(self.boxSizer1)

def _init_ctrls(self, prnt):

generated method, don’t edit

wx.Dialog.init(self, id=wxID_DIALOG1, name=u’', parent=prnt,

pos=wx.Point(470, 298), size=wx.Size(223, 180),

style=wx.DEFAULT_DIALOG_STYLE, title=‘Add Contact’)

self.SetClientSize(wx.Size(215, 146))

self.stName = wx.StaticText(id=wxID_DIALOG1STNAME, label=u’Name’,

name=u’stName’, parent=self, pos=wx.Point(5, 5), size=wx.Size(27,

13), style=0)

self.ConName = wx.TextCtrl(id=wxID_DIALOG1CONNAME, name=u’ConName’,

parent=self, pos=wx.Point(5, 28), size=wx.Size(203, 21), style=0,

value=u’')

self.stNumber = wx.StaticText(id=wxID_DIALOG1STNUMBER, label=u’Number’,

name=u’stNumber’, parent=self, pos=wx.Point(5, 59),

size=wx.Size(37, 13), style=0)

self.Number = wx.TextCtrl(id=wxID_DIALOG1NUMBER, name=u’Number’,

parent=self, pos=wx.Point(5, 82), size=wx.Size(203, 21), style=0,

value=u’')

self.btSave = wx.Button(id=wxID_DIALOG1BTSAVE, label=u’Save’,

name=u’btSave’, parent=self, pos=wx.Point(5, 113),

size=wx.Size(75, 23), style=0)

self.btSave.Bind(wx.EVT_BUTTON, self.OnBtSaveButton,

id=wxID_DIALOG1BTSAVE)

self.btCancel = wx.Button(id=wxID_DIALOG1BTCANCEL, label=u’Cancel’,

name=u’btCancel’, parent=self, pos=wx.Point(90, 113),

size=wx.Size(75, 23), style=0)

self.btCancel.Bind(wx.EVT_BUTTON, self.OnBtCancelButton,

id=wxID_DIALOG1BTCANCEL)

self._init_sizers()

def init(self, parent):

self._init_ctrls(parent)

def OnBtSaveButton(self, event):

Name = self.ConName.GetValue()

Number = self.Number.GetValue()

if Name and Number :

db=pdo.connect(“Module=MySQLdb;User=root;Passwd=root;DB=smscentral”)

if db.active:

strInsert = “INSERT INTO contacts(Name,Number) VALUES('”+str(Name)+“‘,’”+str(Number)+“')”

try:

db.execute(strInsert)

except:

print ‘Error Insert’

finally:

db.close()

self.Destroy()

db=MySQLdb.connect(host=‘localhost’, user=‘root’ , passwd=‘root’, db=‘smscentral’)

c=db.cursor()

strI = “INSERT INTO contacts(Name,Number) VALUES('”+str(Name)+“‘,’”+str(Number)+“')”

c.execute(“”“INSERT INTO contacts(Name,Number) VALUES(‘TAN’,‘99999999’)”“”)

c.close()

db.close()

self.Destroy()

def OnBtCancelButton(self, event):

self.Destroy()

if name == ‘main’:

app = wx.PySimpleApp()

dlg = create(None)

try:

dlg.ShowModal()

finally:

dlg.Destroy()

app.MainLoop()

Regards
Tanay Shah


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Best Regards
Tanay Shah