Python/PyQT QString无法插入MySQL数据库

1 投票
3 回答
3913 浏览
提问于 2025-04-15 15:04

我正在尝试通过一个叫做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转换为字符串,但结果还是一样。

任何帮助都将非常感激 :)?

L

编辑:这是完整的代码,去掉了导入部分:

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

3 个回答

1

这个程序还缺少一个重要的部分,就是commit()函数。这个函数的作用是把数据保存下来。另外,你也可以在脚本开始的时候设置autocommit(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)
1

原因很简单:

self.sql 应该放在

def submitForm(self):

里面,而不是放在

def init(self, parent=None):

里面。

因为如果放错了,它就没用!

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

撰写回答