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)
submit が行うことは、データベース オブジェクトを作成し、値をデータベースに書き込むことだけです。ただし、デバッグのために、構築された 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_())