我正在尝试使用QLineEdit小部件从用户检索某些值。当QPushButton引发单击事件时,我希望从所有QLineEdit小部件中检索文本并将其存储在本地MySQL数据库中。但是,当我尝试在insert语句中使用字符串替换时,值不会被替换。这是我的sql语句:
sql = 'INSERT INTO jobs (incident_id, organization, organization_poc, media_type) VALUES ("%s", "%s", "%s", "%s")' % (self.edi_IncidentID.text(), self.edi_OrganizationAffected.text(), self.edi_OrganizationContact.text(), self.edi_MediaType.text())
所有self.edi_xxx变量都只是QLineEdit小部件。按下按钮时,会触发以下内容:
self.connect(btn_Submit, QtCore.SIGNAL('clicked()'), self.submitForm)
所有提交都是创建数据库对象并将值写入数据库。但是,为了调试我打印出构造的SQL语句,这就出现了: INSERT INTO作业(incident_id,organization,organization_poc,media_type)VALUES(“”,“”,“”,“”)。
我也尝试使用str()函数将QString转换为字符串,但同样的事情发生了。
非常感谢任何帮助:)?
→
编辑:这是完整的代码减去导入:
class Database():
def __init__(self):
self.db_host = "localhost"
self.db_user = "***********"
self.db_pass = "***********"
self.db_name = "incidents"
def openConn(self):
self.db = MySQLdb.connect(self.db_host, self.db_user, self.db_pass, self.db_name)
def closeConn(self):
self.db.close()
def writeValues(self, sql):
self.openConn()
self.cursor = self.db.cursor()
self.cursor.execute(sql)
self.cursor.fetchone()
self.closeConn()
class NewIncidentForm(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setWindowTitle('New Incident')
lbl_IncidentID = QtGui.QLabel('Incident ID:')
lbl_MediaType = QtGui.QLabel('Media Type:')
lbl_OrganizationAffected = QtGui.QLabel('Organization Affected:')
lbl_OrganizationContact = QtGui.QLabel('Organization Point of Contact: ')
self.edi_IncidentID = QtGui.QLineEdit()
self.edi_MediaType = QtGui.QLineEdit()
self.edi_OrganizationAffected = QtGui.QLineEdit()
self.edi_OrganizationContact = QtGui.QLineEdit()
btn_Submit = QtGui.QPushButton('Submit')
grid = QtGui.QGridLayout()
grid.setSpacing(10)
grid.addWidget(lbl_IncidentID, 1, 0)
grid.addWidget(self.edi_IncidentID, 1, 1)
grid.addWidget(lbl_MediaType, 3, 0)
grid.addWidget(self.edi_MediaType, 3, 1)
grid.addWidget(lbl_OrganizationAffected, 4, 0)
grid.addWidget(self.edi_OrganizationAffected, 4, 1)
grid.addWidget(lbl_OrganizationContact, 5, 0)
grid.addWidget(self.edi_OrganizationContact, 5, 1)
grid.addWidget(btn_Submit, 15, 0)
self.sql = 'INSERT INTO jobs (incident_id, organization, organization_poc, media_type) VALUES ("%s", "%s", "%s", "%s")' % (self.edi_IncidentID.text(), self.edi_OrganizationAffected.text(), self.edi_OrganizationContact.text(), self.edi_MediaType.text())
self.connect(btn_Submit, QtCore.SIGNAL('clicked()'), self.submitForm)
self.setLayout(grid)
self.resize(350, 300)
def submitForm(self):
db = Database()
db.writeValues(self.sql)
app = QtGui.QApplication(sys.argv)
qb = NewIncidentForm()
qb.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
:d
QString有__str__
个函数,所以试试这个:
self.sql = 'INSERT INTO jobs (incident_id, organization, organization_poc, media_type) VALUES ("%s", "%s", "%s", "%s")' % (''.join(self.edi_IncidentID.text()), ''.join(self.edi_OrganizationAffected.text()), ''.join(self.edi_OrganizationContact.text()), ''.join(self.edi_MediaType.text()))
添加''.join()
或者QString必须是Utf8()
将self.edi_IncidentIS.text()
更改为:
self.edi_IncidentIS.text().toUtf8()
整个指示:
self.sql = 'INSERT INTO jobs (incident_id, organization, organization_poc, media_type) VALUES ("%s", "%s", "%s", "%s")' % (self.edi_IncidentID.text().toUtf8(), self.edi_OrganizationAffected.text().toUtf8(), self.edi_OrganizationContact.text().toUtf8(), self.edi_MediaType.text().toUtf8())
答案 1 :(得分:1)
原因很简单:
self.sql应该在 def submitForm(self):
不在 def init (self,parent = None):
因为其他人认为它是无效的!
答案 2 :(得分:1)
程序缺少的另一部分是commit()函数,用于在脚本的乞讨时存储数据或自动提交(true)
def closeConn(self):
self.db.close()
self.db.commit()
def submitForm(self):
db = Database()
self.sql = 'INSERT INTO jobs (incident_id, organization, organization_poc, media_type) VALUES ("%s", "%s", "%s", "%s")' % (self.edi_IncidentID.text(), self.edi_OrganizationAffected.text(), self.edi_OrganizationContact.text(), self.edi_MediaType.text())
db.writeValues(self.sql)