I have an error when I insert data which form QlinEdit .
You can see the button1Clicked(self) method ,
I can't execute 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())")
this code .
But I can get the text when I use print(self.IDEdit.text())
.
I don't understand why . I hope someone can help me.
#-*- 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_())
Aucun commentaire:
Enregistrer un commentaire