Hi all,
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())
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)
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 ("", "", "", "").
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
EDIT: 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_())