|
|
@@ -0,0 +1,112 @@
|
|
|
+import os
|
|
|
+import sys
|
|
|
+
|
|
|
+from PyQt5.QtCore import QSize, Qt
|
|
|
+from PyQt5.QtSql import QSqlDatabase, QSqlTableModel
|
|
|
+from PyQt5.QtWidgets import (
|
|
|
+ QApplication,
|
|
|
+ QComboBox,
|
|
|
+ QDataWidgetMapper,
|
|
|
+ QDoubleSpinBox,
|
|
|
+ QFormLayout,
|
|
|
+ QLabel,
|
|
|
+ QLineEdit,
|
|
|
+ QMainWindow,
|
|
|
+ QSpinBox,
|
|
|
+ QWidget,
|
|
|
+ QVBoxLayout,
|
|
|
+ QHBoxLayout,
|
|
|
+ QPushButton,
|
|
|
+)
|
|
|
+
|
|
|
+basedir = os.path.dirname(__file__)
|
|
|
+
|
|
|
+
|
|
|
+class MainWindow(QMainWindow):
|
|
|
+ def __init__(self):
|
|
|
+ super().__init__()
|
|
|
+
|
|
|
+ form = QFormLayout()
|
|
|
+
|
|
|
+ self.track_id = QSpinBox()
|
|
|
+ self.track_id.setRange(0, 2147483647)
|
|
|
+ self.track_id.setDisabled(True)
|
|
|
+ self.name = QLineEdit()
|
|
|
+ self.album = QComboBox()
|
|
|
+ self.genre = QComboBox()
|
|
|
+ self.composer = QLineEdit()
|
|
|
+
|
|
|
+ self.milliseconds = QSpinBox()
|
|
|
+ self.milliseconds.setRange(0, 2147483647)
|
|
|
+ self.milliseconds.setSingleStep(1)
|
|
|
+
|
|
|
+ self.bytes = QSpinBox()
|
|
|
+ self.bytes.setRange(0, 2147483647)
|
|
|
+ self.bytes.setSingleStep(1)
|
|
|
+
|
|
|
+ self.unit_price = QDoubleSpinBox()
|
|
|
+ self.unit_price.setRange(0, 999)
|
|
|
+ self.unit_price.setSingleStep(0.01)
|
|
|
+ self.unit_price.setPrefix("$")
|
|
|
+
|
|
|
+ form.addRow(QLabel("Track ID"), self.track_id)
|
|
|
+ form.addRow(QLabel("Track name"), self.name)
|
|
|
+ form.addRow(QLabel("Composer"), self.composer)
|
|
|
+ form.addRow(QLabel("Milliseconds"), self.milliseconds)
|
|
|
+ form.addRow(QLabel("Bytes"), self.bytes)
|
|
|
+ form.addRow(QLabel("Unit price"), self.unit_price)
|
|
|
+
|
|
|
+ print("driver list: ", ' '.join(QSqlDatabase.drivers()))
|
|
|
+ self.db = QSqlDatabase("QSQLITE")
|
|
|
+ self.db.setDatabaseName(os.path.join(basedir, "Chinook_Sqlite.sqlite"))
|
|
|
+ self.db.open()
|
|
|
+
|
|
|
+ self.model = QSqlTableModel(db=self.db)
|
|
|
+
|
|
|
+ self.mapper = QDataWidgetMapper()
|
|
|
+ self.mapper.setModel(self.model)
|
|
|
+
|
|
|
+ self.mapper.addMapping(self.track_id, 0)
|
|
|
+ self.mapper.addMapping(self.name, 1)
|
|
|
+ self.mapper.addMapping(self.composer, 5)
|
|
|
+ self.mapper.addMapping(self.milliseconds, 6)
|
|
|
+ self.mapper.addMapping(self.bytes, 7)
|
|
|
+ self.mapper.addMapping(self.unit_price, 8)
|
|
|
+
|
|
|
+ self.model.setTable("Track")
|
|
|
+ self.model.select()
|
|
|
+
|
|
|
+ self.mapper.toFirst()
|
|
|
+
|
|
|
+ self.setMinimumSize(QSize(400, 400))
|
|
|
+
|
|
|
+ controls = QHBoxLayout()
|
|
|
+ prev_rec = QPushButton("Previous")
|
|
|
+ prev_rec.clicked.connect(self.mapper.toPrevious)
|
|
|
+ next_rec = QPushButton("Next")
|
|
|
+ next_rec.clicked.connect(self.mapper.toNext)
|
|
|
+ save_rec = QPushButton("Save Changes")
|
|
|
+ save_rec.clicked.connect(self.mapper.submit)
|
|
|
+
|
|
|
+ controls.addWidget(prev_rec)
|
|
|
+ controls.addWidget(next_rec)
|
|
|
+ controls.addWidget(save_rec)
|
|
|
+
|
|
|
+ form.setLabelAlignment(
|
|
|
+ Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTrailing | Qt.AlignmentFlag.AlignVCenter)
|
|
|
+
|
|
|
+ layout = QVBoxLayout()
|
|
|
+ layout.addLayout(form)
|
|
|
+ layout.addLayout(controls)
|
|
|
+
|
|
|
+ widget = QWidget()
|
|
|
+ widget.setLayout(layout)
|
|
|
+
|
|
|
+ self.setCentralWidget(widget)
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ app = QApplication(sys.argv)
|
|
|
+ window = MainWindow()
|
|
|
+ window.show()
|
|
|
+ sys.exit(app.exec_())
|