Python/PyQT QString 不会插入 MySQL 数据库 [英] Python/PyQT QString won't insert into MySQL database

查看:61
本文介绍了Python/PyQT QString 不会插入 MySQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 QLineEdit 小部件从用户那里检索一些值.当 QPushButton 引发单击事件时,我希望从所有 QLineEdit 小部件中检索文本并将其存储在本地 MySQL 数据库中.但是,当我尝试在插入语句中使用字符串替换时,值不会被替换.这是我的 sql 语句:

I am trying to trying to retrieve some values from a user using a QLineEdit widget. When a QPushButton raises a clicked event, I want the text to be retrieved from all QLineEdit widgets and stored in a local MySQL databaase. However, when I try to use string substition in the insert statement, the values don't get substituted. Here's my sql statement:

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 小部件.当按下按钮时,会触发以下内容:

All of the self.edi_xxx variables are just QLineEdit widgets. When a button is pushed, the following is fired:

self.connect(btn_Submit, QtCore.SIGNAL('clicked()'), self.submitForm)  

提交所做的只是创建一个数据库对象并将值写入数据库.但是,为了调试,我打印出构造的 SQL 语句,结果如下:INSERT INTO 作业 (incident_id, organization, organization_poc, media_type) VALUES ("", "", "", "").

All submit does is create a database object and write the values to the database. However, for debugging I print out the constructed SQL statement and this comes out: INSERT INTO jobs (incident_id, organization, organization_poc, media_type) VALUES ("", "", "", "").

我也尝试过使用 str() 函数将 QString 转换为字符串,但同样的事情发生了.

I have also tried using the str() function to convert a QString to a string but the same thing happens.

任何帮助将不胜感激:)?

Any help would be greatly appreciated :)?

L

这是减去导入的完整代码:

Here is the complete code minus the imports:

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_())

推荐答案

该程序缺少的另一部分是用于存储数据的 commit() 函数或在脚本请求时自动提交 (true)

the other part that is missing for the program is the commit() function to have data stored or autocommit(true) at the begging of the script

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 不会插入 MySQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆