Python/PyQT QString 不会插入到 MySQL 数据库中

标签 python mysql pyqt4 qstring

我正在尝试使用 QLineEdit 小部件从用户那里检索一些值。当 QPushButton 引发点击事件时,我希望从所有 QLineEdit 小部件中检索文本并将其存储在本地 MySQL 数据库中。但是,当我尝试在插入语句中使用字符串替换时,值不会被替换。这是我的 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 jobs (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_())

最佳答案

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

关于Python/PyQT QString 不会插入到 MySQL 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1569161/

相关文章:

mysql - 修复 MySQL 转储中的错误编码字符

python - 从 PyQt GUI 类外部访问 GUI 元素 text( )

python - 按数字对 QListWidget 进行排序

python - 键入 "PermissionError"以离开 Python 解释器时抛出 "exit()"

mysql - 使用 Ruby 和 MySQL 进行多词搜索

python - 如何使用未舍入的变量值更新 PyQt5 标签?

mysql - 从 wamp windows 远程连接到 mysql 服务器数据库

javascript - 使用 HTML/CSS/JavaScript 在 Web 应用程序中提供 PyQt4 GUI

python - 为什么 Sympy 会切断系数较小的多项式项?

python - 如何在django的sqlite3数据库中使用全文搜索?