如何在PyQt中向QLineEdit插入文本?

时间:2015-07-02 02:39:16

标签: python sqlite pyqt

插入来自QLineEdit的数据时出错。 您可以在button1Clicked(self)方法中看到,我无法执行:

q.exec_("insert into COMPANY (id, name, age, address, salary) values (self.IDEdit.text(),self.NameEdit.text(),self.AgeEdit.text(),self.AddressEdit.text(),self.SalaryEdit.text())")

但是当我使用print(self.IDEdit.text())时,我可以获得该文字。

我不明白为什么。我希望有人可以帮助我。

#-*- coding: utf-8 -*- 
import sys
import sqlite3
from PyQt4 import QtCore, QtGui
from 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 into 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_())

1 个答案:

答案 0 :(得分:1)

正如代码格式显示的那样,您正在发送小部件功能的字符串,而不是实际评估它。相反,您需要将相关行更改为:

q.exec_("insert into 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()))

这看起来有些复杂,但只是simple string formatting