I am trying to connect to postgresql with qt. I managed that last one, but, just like in my previous question, an invisible internal error occurs. The difference is that now it happens when a query fails (for example, when the table does not exist).
The program should print None to the screen. But the program ends before that. Exactly when I call obj.prepare(query).
This is my code:
from PySide2.QtSql import QSqlDatabase, QSqlQuery
from PySide2.QtWidgets import QApplication, QMainWindow
app = QApplication([])
class SqlError(Exception):
pass
def connect(host, port, dbname, user, password):
db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName(host)
db.setPort(port)
db.setDatabaseName(dbname)
if(not db.open(user, password)):
raise SqlError(db.lastError().text())
def execute(query, *args, **kwargs):
obj = QSqlQuery()
if(not obj.prepare(query)):
raise SqlError(obj.lastError().text())
if(kwargs):
for var, value in kwargs.items():
obj.bindValue(var, value)
elif(args):
for value in args:
obj.addBindValue(value)
if(not obj.exec_()):
raise SqlError(obj.lastError().text())
connect(host="localhost",
port=0,
dbname="mydatabase",
user="user",
password="password")
print(execute("SELECT * FROM c_venta"))
Attached screenshot of my idle. This behavior is similar to what happens when running from cmd and importing my script from the interpreter.
What do I need to catch that error, what am I doing wrong? I've looked at several tutorials and I'm doing the same.
Edit: I tried the script importing it from the interpreter and it closes said interpreter without saying anything.
Edit 2: I tried variants of a certain answer and it didn't work for me either. https://stackoverflow.com/a/33741755/12913664
Edit 3: Pass the result of QSqlDatabase.database() to QSqlQuery (same result), to prepare (says it only accepts one argument), and tried saving the connection to a global variable and using db.exec (same result).