python - How to insert text to QLineEdit in PyQt? -


i have error when insert data qlineedit. can see in button1clicked(self) method, can't execute :

q.exec_("insert company (id, name, age, address, salary) values (self.idedit.text(),self.nameedit.text(),self.ageedit.text(),self.addressedit.text(),self.salaryedit.text())") 

but can text when use print(self.idedit.text()).

i don't understand why. hope can me.

#-*- coding: utf-8 -*-  import sys import sqlite3 pyqt4 import qtcore, qtgui pyqt4.qtsql import *   def createconnection():      db=qsqldatabase.adddatabase("qsqlite")      db.setdatabasename("test.db")     db.open()  def createtable():      q=qsqlquery() #     q.exec_("create table if not exists company (id int primary key not null,name text not null,age int not null,address char(50),salary real)")      q.exec_("commit")   class model(qsqltablemodel):      def __init__(self,parent):          qsqltablemodel.__init__(self,parent)          self.settable("company")          self.select()          self.seteditstrategy(qsqltablemodel.onmanualsubmit)   class testwidget(qtgui.qwidget):      def __init__(self):          qtgui.qwidget.__init__(self)          vbox=qtgui.qvboxlayout(self)          self.view=qtgui.qtableview()          self.model=model(self.view)          self.view.setmodel(self.model)          vbox.addwidget(self.view)          self.resize(900,300)          self.component()       def component(self):         id = qtgui.qlabel('id :', self)         id.move(30, 180)          self.idedit = qtgui.qlineedit(self)          self.idedit.move(70, 180)         self.idedit.setfocus()          name = qtgui.qlabel('name :', self)         name.move(180, 180)         self.nameedit = qtgui.qlineedit(self)         self.nameedit.move(230, 180)          age = qtgui.qlabel('age :', self)         age.move(340, 180)         self.ageedit = qtgui.qlineedit(self)         self.ageedit.move(380, 180)          address = qtgui.qlabel('address :', self)         address.move(500, 180)         self.addressedit = qtgui.qlineedit(self)         self.addressedit.move(560, 180)          salary = qtgui.qlabel('salary :', self)         salary.move(670, 180)         self.salaryedit = qtgui.qlineedit(self)         self.salaryedit.move(730, 180)          button1= qtgui.qpushbutton('submit', self)         button1.setgeometry(730, 240, 70, 30)          self.connect(button1, qtcore.signal('clicked()'),self.button1clicked)         def button1clicked(self):         q=qsqlquery()         q.exec_("insert company (id, name, age, address, salary) values (self.idedit.text(), self.nameedit.text(),self.ageedit.text(),self.addressedit.text(),self.salaryedit.text())")          q.exec_("commit")          print (self.idedit.text())          print (self.nameedit.text())          print (self.ageedit.text())          print (self.addressedit.text())          print (self.salaryedit.text())    if __name__=="__main__":      app=qtgui.qapplication(sys.argv)      createconnection()     createtable()      w=testwidget()      w.show()      sys.exit(app.exec_()) 

as code formatting shows, you're sending strings of widget function , not evaluating it. instead you'd need change relevant line like:

q.exec_("insert company (id, \                               name, \                               age, \                               address, \                               salary) values ( \                               '{0}', \                               '{1}', \                               '{2}', \                               '{3}', \                               '{4}')".format(self.idedit.text(),                                              self.nameedit.text(),                                              self.ageedit.text(),                                              self.addressedit.text(),                                              self.salaryedit.text())) 

this looks complicated simple string formatting.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -