问题描述
- PyQt:获取lineEdit值插入到mysql失败
-
我想把lineEdit的输入值一边显示到tablewidegt,一边插入到mysql。由于显示程序是提取mysql的数据再显示,所以显示界面是空白,并且mysql的数据也没有改变.from PyQt4 import QtCore, QtGui,QtSql import sys from test2 import Ui_MainWindow #test2是我用QT desigenr写的UI程序 import chardet class StartQt4(QtGui.QMainWindow,Ui_MainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.setupUi(self) self.db = QtSql.QSqlDatabase.addDatabase("QMYSQL") self.db.setHostName("localhost") self.db.setDatabaseName("test") #set address self.db.setUserName("root") self.db.setPassword("") if (self.db.open()): self.showdata() else: print "failed" QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.add) def add(self): name0 = str(self.lineEdit.text()) name1 = str(self.lineEdit_2.text()) name2 = str(self.lineEdit_3.text()) print type(name0) self.q=QtSql.QSqlQuery(self.db) self.q.exec_("insert into qiu (name0,nam1,name2) value ('%s','%s','%s')" %(''.join(name0),''.join(name1),''.join(name2))) self.q.exec_("commit") lastrow=self.tableWidget.rowCount() self.tableWidget.insertRow(lastrow) self.showdata() def showdata(self): q=QtSql.QSqlQuery(self.db) q.exec_("select * from qiu") for i in range(0,self.tableWidget.rowCount()): q.next() name0 = q.value(0).toString() name1 = q.value(1).toString() name2 = q.value(2).toString() self.tableWidget.setItem(i, 0, QtGui.QTableWidgetItem(name0)) self.tableWidget.setItem(i, 1, QtGui.QTableWidgetItem(name1)) self.tableWidget.setItem(i, 2, QtGui.QTableWidgetItem(name2)) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) myapp = StartQt4() myapp.show() sys.exit(app.exec_())
解决方案
先打印sql语句 看是否语法正确
时间: 2025-01-03 15:59:00