Good evening,
I am not sure whether this is the right section, since my question is not about the GUI part of PyQt, but database (QtSql).
I am trying to get familiar with the PyQt5.QtSql module, because I use PyQt5 a lot and especially
I Tried to set a connection to an existing database on my local machine in order to run some tests.
Althought
Here is my code and, attached, a screenshot of my environment - in case it might be some problem of paths and filenames.
I already tried to call
I am not sure whether this is the right section, since my question is not about the GUI part of PyQt, but database (QtSql).
I am trying to get familiar with the PyQt5.QtSql module, because I use PyQt5 a lot and especially
QTableViews. I Tried to set a connection to an existing database on my local machine in order to run some tests.
Althought
connection.isOpen() does not return False, it is simply empty and doesn't seem to catch anything from the database. Weird thing is, it doesn't raise any exception either. For example, if I run a query to create a table and then try to print database's tables, it will just return an empty list. Here is my code and, attached, a screenshot of my environment - in case it might be some problem of paths and filenames.
I already tried to call
.addDatabase() with a connection name as parameter, but it doesn't make any difference. import sys
from PyQt5.QtSql import (QSqlDatabase, QSqlQuery)
from PyQt5.QtWidgets import (
QApplication,
QMainWindow,
QWidget,
QTableView,
QPushButton,
QLabel,
QHBoxLayout,
QVBoxLayout,
QStyle
)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setupUi(title_txt='My First Database App', button_txt='Refresh')
self.getData()
def setupUi(self, **kwargs):
container = QWidget()
main_layout = QVBoxLayout()
# Top section
top_section_layout = QHBoxLayout()
top_section_container = QWidget()
top_section_layout.addWidget(QLabel(kwargs['title_txt']))
refresh_btn = QPushButton(kwargs['button_txt'])
refresh_btn.setIcon(QApplication.style().standardIcon(QStyle.SP_BrowserReload))
refresh_btn.setFixedSize(refresh_btn.sizeHint())
top_section_layout.addWidget(refresh_btn)
top_section_container.setLayout(top_section_layout)
# Table section
search_result_tbl = QTableView()
# Wrapping up
main_layout.addWidget(top_section_container)
main_layout.addWidget(search_result_tbl)
container.setLayout(main_layout)
self.setCentralWidget(container)
def getData(self):
connection = QSqlDatabase.addDatabase('QSQLITE', 'testDB.sqlite')
connection.open()
if not connection.isOpen():
print('Connection error occurred.')
else:
print(connection.tables())
def setObjectsName(self):
pass
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
