def sql2csv(db: QSqlDatabase, tables: list, filetype: str): rows = [] if not db.isOpen(): try: db.open() except Exception as e: print(str(e)) for table in tables: query = db.exec_( f"SELECT * FROM {table} ORDER BY Platform ASC, Name ASC") while query.next(): if table == "games": rows.append({ "Platform": query.value(1), "Name": query.value(2), "Region": query.value(3), "Code": query.value(4), "Game": query.value(5), "Box": query.value(6), "Manual": query.value(7), "Year": query.value(8), "Genre": query.value(9), "Comment": query.value(10), "Publisher": query.value(11), "Developer": query.value(12), "Platforms": query.value(13) }) elif table == "consoles": rows.append({ "Platform": query.value(1), "Name": query.value(2), "Region": query.value(3), "Country": query.value(4), "Serial number": query.value(5), "Console": query.value(6), "Box": query.value(7), "Manual": query.value(8), "Year": query.value(9), "Comment": query.value(10) }) elif table == "accessories": rows.append({ "Platform": query.value(1), "Name": query.value(2), "Region": query.value(3), "Country": query.value(4), "Accessory": query.value(5), "Box": query.value(6), "Manual": query.value(7), "Year": query.value(8), "Comment": query.value(9) }) with open(f"{table}.{filetype}", 'w', encoding='utf8') as f: writer = csv.DictWriter( f, dialect="excel-tab" if filetype == "tsv" else "excel", fieldnames=list(rows[0].keys())) writer.writeheader() for row in rows: writer.writerow(row) rows.clear()
import sys from PySide2.QtCore import QSize, Qt from PySide2.QtSql import QSqlDatabase, QSqlTableModel from PySide2.QtWidgets import ( QApplication, QLineEdit, QMainWindow, QTableView, QVBoxLayout, QWidget, ) db = QSqlDatabase("QSQLITE") db.setDatabaseName("chinook.sqlite") db.open() class MainWindow(QMainWindow): def __init__(self): super().__init__() container = QWidget() layout = QVBoxLayout() self.search = QLineEdit() self.search.textChanged.connect(self.update_filter) self.table = QTableView() layout.addWidget(self.search) layout.addWidget(self.table)
if __name__ == "__main__": #Set up the application window app = QGuiApplication(sys.argv) #Expose the list to the Qml code m_Database = QSqlDatabase() m_Database = QSqlDatabase.addDatabase("QSQLITE") m_Database.setDatabaseName("C:\dev\Work\python\SQLTest\o3.db") # Don't know why it is not working with (.mde file?, ...) # m_Database = QSqlDatabase.addDatabase("QODBC"); # m_Database.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)}; FIL={MS Access}; DBQ=C:\\OnDemand3DApp\\Users\\Common\\MasterDB\\lucion.mde;") if (not m_Database.open()): qDebug("ERROR") else: qDebug("DB Opened") my_StudyModel = myStudyModel() my_SeriesModel = studySeriesModel() my_StudyModel.selectRow.connect(my_SeriesModel.refreshUID) # Temporarily, setting default path manually path = "C:\\dev\\Work\\TestData" dirModel = myDirModel() dirModel.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs) dirModel.setRootPath(path) #Load the QML file