I'm trying to create GUI for updating data in my database, and I have two ideas. One requires a lot of more work, but works, and the other seems perfect, but gives me error. Here is the working one:
def update_kirurg(self):
query = QSqlQuery()
if self.update_attribute_name_lineEdit.text() == "oib_kir":
query.prepare("UPDATE kirurg SET oib_kir = :new_oib_kir WHERE oib_kir = :old_oib_kir")
query.bindValue(":new_oib_kir", self.update_new_value_lineEdit.text())
query.bindValue(":old_oib_kir", self.update_previous_value_lineEdit.text())
status = query.exec_()
if status is not True:
errorText = query.lastError().text()
QtGui.QMessageBox.critical(self, 'Query error', errorText)
After that I changed my GUI a bit, that's why lineEdit names are different, and I've tried this code, which would save me a ton of copy and pasting, but for some reason it doesn't work.
def update_asistira(self):
query = QSqlQuery()
query.prepare("UPDATE asistira SET :column_name = :new_column_value WHERE :condition_column = :condition_column_value")
query.bindValue(":column_name", self.update_attribute_name_lineEdit.text())
query.bindValue(":new_column_value", self.update_new_value_lineEdit.text())
query.bindValue(":condition_column", self.update_where_column_lineEdit.text())
query.bindValue(":condition_column_value", self.update_has_value_lineEdit.text())
status = query.exec_()
if status is not True:
errorText = query.lastError().text()
QtGui.QMessageBox.critical(self, 'Query error', errorText)
It gives me this error:
ERROR: Syntax error at or near "(" LINE 1: EXECUTE ('uloga', 'sadasd', 'id_operacije', '1044542ED') ^ (42601) QPSQL: Unable to create query
EDIT: I tried like this:
def update_asistira(self):
query = QSqlQuery()
status = query.exec("UPDATE asistira SET uloga = asdasd WHERE oib_kir = 40145478661")
if status is not True:
errorText = query.lastError().text()
QtGui.QMessageBox.critical(self, 'Query error', errorText)
Where asistira has this sql code (written directly in postgreSQL):
CREATE TABLE asistira
(
id_operacije character varying(20) NOT NULL,
oib_kir character(11) NOT NULL,
uloga text,
CONSTRAINT asistira_pkey PRIMARY KEY (id_operacije, oib_kir),
CONSTRAINT asistira_id_operacije_fkey FOREIGN KEY (id_operacije)
REFERENCES operacija (id_operacije) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT asistira_oib_kir_fkey FOREIGN KEY (oib_kir)
REFERENCES kirurg (oib_kir) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
And it gives me this error:
ERROR: column "asdasd" does not exist LINE 1: UPDATE asistira SET uloga = asdasd WHERE oib_kir = 40145478661 (42703) QPSQL: Unable to create query
EDIT2: Apparently you can't use (... :some_value = :another_value...). The value on the left has to be already known, otherwise it just doesn't work. But how do I make UPDATE query than?
EDIT3: I've tried this now and it gives me syntax error
query.exec("""UPDATE "%s" SET "%s" = '%s' WHERE "%s" = '%s'""" % (str(self.lineEdit.text()), str(self.lineEdit_2.text()), str(self.lineEdit_3.text()), str(self.lineEdit_4.text()), str(self.lineEdit_5.text()))
Is it possible to also manually enter table name like I did here?