1

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

3 に答える 3

1

理由は非常に簡単です。

self.sql は def submitForm(self) にある必要があります:

def initにない(self、parent=None):

そうでなければ無効だからです!

于 2011-03-03T20:27:10.213 に答える
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())
于 2009-11-28T19:31:35.253 に答える