no connection with qsqlite
-
Hello, I'm new to the forum and I hope you can help me.
I successfully created my first form with QDesigner in python3 and then wanted to connect to my SQLite database.
What I have done wrong ? I use SQlite 3.3.3 and pyside6-6.2.3
from PySide6.QtWidgets import QApplication,QMainWindow from PySide6 import QtSql from QT.frm_main import Ui_frm_main class Frm_main(QMainWindow, Ui_frm_main): def __init__(self): super().__init__() self.setupUi(self) mod_offene_Leistungen = QtSql.QSqlRelationalTableModel() mod_offene_Leistungen.setTable("Leistungen") mod_offene_Leistungen.select() self.tbl_offene_Leistungen.setModel(mod_offene_Leistungen) db=QtSql.QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName("Rechnungen.db") app=QApplication() frm_main=Frm_main() frm_main.show() app.exec()
-
Maybe you should open the database as described in the documentation?
-
Maybe you should open the database as described in the documentation?
@Christian-Ehrlicher can you send me the code according to the documentation above ?
-
Hi and welcome to devnet,
Do you mean the code from the Python documentation of the class ?
-
Hi and welcome to devnet,
Do you mean the code from the Python documentation of the class ?
-
As already suggested by @Christian-Ehrlicher, you are missing a call to the open method of your db object.
-
As already suggested by @Christian-Ehrlicher, you are missing a call to the open method of your db object.
-
@SGaist the code should then look like this ??
db=QtSql.QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName("Rechnungen.db") db.open()It doesn’t work..
sry for the stupid questions
@FloCGN said in no connection with qsqlite:
It doesn’t work
What exactly does not work now?
What does db.open() return? -
@FloCGN said in no connection with qsqlite:
It doesn’t work
What exactly does not work now?
What does db.open() return?I want to display the "Leistungen" table in the "offene Leistungen" field. But nothing happens

when I write in the terminal db.open(), then this happens:

from PySide6.QtWidgets import QApplication,QMainWindow from PySide6 import QtSql from QT.frm_main import Ui_frm_main class Frm_main(QMainWindow, Ui_frm_main): def __init__(self): super().__init__() self.setupUi(self) mod_offene_Leistungen = QtSql.QSqlRelationalTableModel() mod_offene_Leistungen.setTable("Leistungen") mod_offene_Leistungen.select() self.tbl_offene_Leistungen.setModel(mod_offene_Leistungen) db=QtSql.QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName("Rechnungen.sqlite") db.open() app=QApplication() frm_main=Frm_main() frm_main.show() app.exec() -
Where is that file located on your system ?
SQLite will create the file if it does not exists hence if your path is wrong you will have an empty database. -
Where is that file located on your system ?
SQLite will create the file if it does not exists hence if your path is wrong you will have an empty database. -
Then did you check the content of the file ?
What about adding error checks to your code ? -
@SGaist the content of the database file ? I think its right
error checks are debugging functions ? I debugged it, but there were no error messages. I don't know if I did it right either. Like I said, I'm a total beginner
@FloCGN said in no connection with qsqlite:
I think its right
You think or you know?
"error checks are debugging functions ?" - no.
What does db.open() return? And please do not post some screen-shot, print it out!print(db.open())If it returns false then check https://doc.qt.io/qt-5/qsqldatabase.html#lastError
-
@FloCGN said in no connection with qsqlite:
I think its right
You think or you know?
"error checks are debugging functions ?" - no.
What does db.open() return? And please do not post some screen-shot, print it out!print(db.open())If it returns false then check https://doc.qt.io/qt-5/qsqldatabase.html#lastError