从textentrydialog

时间:2017-06-29 17:56:54

标签: python string python-2.7 sqlite

我正在尝试将值作为用户在textentrydialog中给出的输入插入表中。我收到错误:sqlite3.OperationalError:near"%&#34 ;:第184行的语法错误。第一个输入是整数,其他3是字符串

def OnAddNew(self, event):
    dlg = GetData(parent = self.panel) 
    dlg.ShowModal()
    if dlg.result_name:
        print "Elements: "+dlg.result_elements+"\n"
        print "Name: "+dlg.result_name+"\n"
        print "Formula: "+dlg.result_formula+"\n"
        print "File: "+dlg.result_file+"\n"
    else:
      print "No Input found\n"
    dlg.Destroy()

class GetData(wx.Dialog):

    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, wx.ID_ANY, "New Molecule", size= (650,220))
        self.panel = wx.Panel(self,wx.ID_ANY)

        self.lblelements = wx.StaticText(self.panel, label="Elements", pos=(20,20))
        self.elements = wx.TextCtrl(self.panel, value="", pos=(110,20), size=(500,-1))
        self.lblnam = wx.StaticText(self.panel, label="Name", pos=(20,60))
        self.name = wx.TextCtrl(self.panel, value="", pos=(110,60), size=(500,-1))
        self.lblform = wx.StaticText(self.panel, label="Formula", pos=(20,100))
        self.formula = wx.TextCtrl(self.panel, value="", pos=(110,100), size=(500,-1))
        self.lblfl = wx.StaticText(self.panel, label="File", pos=(20,140))
        self.file = wx.TextCtrl(self.panel, value="", pos=(110,140), size=(500,-1))
        self.saveButton =wx.Button(self.panel, label="Save", pos=(110,170))
        self.closeButton =wx.Button(self.panel, label="Cancel", pos=(210,170))
        self.saveButton.Bind(wx.EVT_BUTTON, self.SaveConnString)
        self.closeButton.Bind(wx.EVT_BUTTON, self.OnQuit)
        self.Bind(wx.EVT_CLOSE, self.OnQuit)
        self.Show()

    def OnQuit(self, event):
        self.result_name = None
        self.Destroy()

    def SaveConnString(self, event):
        self.result_elements = self.elements.GetValue()
        self.result_name = self.name.GetValue()
        self.result_formula = self.formula.GetValue()
        self.result_file = self.file.GetValue()
        self.conn = sqlite3.connect('RAMAN.db')
        cursor= self.conn.execute("SELECT max(MOL_NUMBER) FROM MOLECULE")
        maxvalue = cursor.fetchone()[0]       
        cursor= self.conn.execute("INSERT INTO MOLECULE VALUES (%d, %s, %s, %s)", ("maxvalue + 1", "name", "formula", "file"))
        #split the elements (because user will give input as 6,8,...) and find the max value of id from table link, execute query to insert values in the link table as (id + 1, elements, maxvalue + 1)
        self.Destroy()

1 个答案:

答案 0 :(得分:0)

sqlite使用?作为参数的占位符,而不是%X

此外,"maxvalue + 1"只是一个常量字符串。你想要maxvalue + 1没有引号。