Python的/ PyQt的QString的不会插入到MySQL数据库(Python/PyQT QS

2019-10-17 22:36发布

我想尝试从使用QLineEdit的小部件,用户检索某些值。 当一个QPushButton提出了一个点击事件,我希望文字从所有QLineEdit的部件被检索并存储在本地的MySQL databaase。 然而,当我尝试使用字符串substition在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_poc,介质类型)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_())

Answer 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的有toUtf8()

所以更改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())


Answer 2:

原因很简单:

self.sql应在高清submitForm(个体经营):

不是在DEF 的init(个体,父母=无):

因为otherweise它的无效!



Answer 3:

是缺少该程序的另一部分是在脚本的乞讨已存储的数据的提交()函数或自动提交(真)

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)


文章来源: Python/PyQT QString won't insert into MySQL database