#!/usr/bin/env python
import sys
from PyQt6.QtSql import *
from PyQt6.QtCore import Qt
def __init__(self):
super().__init__()
# set up main window
self.resize(1024,700)
self.model.setTable("people")
self.model.setHeaderData(2, Qt.Orientation.Horizontal, "city")
self.
model.
setRelation(2,
QSqlRelation("city",
"id",
"name"))
self.view.setModel(self.model)
self.view.setSortingEnabled(True)
self.view.sortByColumn(1, Qt.SortOrder.AscendingOrder)
self.view.verticalHeader().setVisible(False)
self.view.doubleClicked.connect(self.onAddRow)
self.setCentralWidget(self.view)
self.view.resizeColumnsToContents()
self.model.select()
def onAddRow(self, s):
self.model.select()
new_rec = self.model.record()
new_rec.setValue("name", 'Eliza')
new_rec.setValue("city", 2)
res = self.model.insertRecord(-1, new_rec)
if res:
print("insert ok")
else:
print("insert failed")
print(self.model.lastError().text())
self.model.submitAll()
self.model.select()
# Establish connection to our database.
def dbConnect():
con.setDatabaseName('db.sqlite')
if not con.open():
print("DB connect error")
sys.exit(1)
def createFakeData():
query.exec("DROP TABLE people")
query.exec("DROP TABLE city")
query.exec("CREATE TABLE people(id INTEGER PRIMARY KEY, name VARCHAR(40) NOT NULL, city INTEGER NOT NULL)")
query.exec("CREATE TABLE city(id INTEGER PRIMARY KEY, name VARCHAR(40) NOT NULL)")
query.exec("INSERT INTO people(name, city) VALUES('Helen', 1)")
query.exec("INSERT INTO people(name, city) VALUES('Juan', 2)")
query.exec("INSERT INTO city(name) VALUES('Seattle')")
query.exec("INSERT INTO city(name) VALUES('Tokyo')")
def main():
dbConnect()
createFakeData()
w = MainWindow()
w.show()
app.exec()
if __name__ == '__main__':
main()
#!/usr/bin/env python
import sys
from PyQt6.QtSql import *
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import (QApplication, QMainWindow, QTableView)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# set up main window
self.resize(1024,700)
self.model = QSqlRelationalTableModel()
self.model.setTable("people")
self.model.setEditStrategy(QSqlTableModel.EditStrategy.OnFieldChange)
self.model.setHeaderData(2, Qt.Orientation.Horizontal, "city")
self.model.setRelation(2, QSqlRelation("city", "id", "name"))
self.view = QTableView()
self.view.setModel(self.model)
self.view.setItemDelegate(QSqlRelationalDelegate(self.view))
self.view.setSortingEnabled(True)
self.view.sortByColumn(1, Qt.SortOrder.AscendingOrder)
self.view.verticalHeader().setVisible(False)
self.view.doubleClicked.connect(self.onAddRow)
self.setCentralWidget(self.view)
self.view.resizeColumnsToContents()
self.model.select()
def onAddRow(self, s):
self.model.select()
new_rec = self.model.record()
new_rec.setValue("name", 'Eliza')
new_rec.setValue("city", 2)
res = self.model.insertRecord(-1, new_rec)
if res:
print("insert ok")
else:
print("insert failed")
print(self.model.lastError().text())
self.model.submitAll()
self.model.select()
# Establish connection to our database.
def dbConnect():
con = QSqlDatabase.addDatabase("QSQLITE")
con.setDatabaseName('db.sqlite')
if not con.open():
print("DB connect error")
sys.exit(1)
def createFakeData():
query = QSqlQuery()
query.exec("DROP TABLE people")
query.exec("DROP TABLE city")
query.exec("CREATE TABLE people(id INTEGER PRIMARY KEY, name VARCHAR(40) NOT NULL, city INTEGER NOT NULL)")
query.exec("CREATE TABLE city(id INTEGER PRIMARY KEY, name VARCHAR(40) NOT NULL)")
query.exec("INSERT INTO people(name, city) VALUES('Helen', 1)")
query.exec("INSERT INTO people(name, city) VALUES('Juan', 2)")
query.exec("INSERT INTO city(name) VALUES('Seattle')")
query.exec("INSERT INTO city(name) VALUES('Tokyo')")
def main():
app = QApplication(sys.argv)
dbConnect()
createFakeData()
w = MainWindow()
w.show()
app.exec()
if __name__ == '__main__':
main()
To copy to clipboard, switch view to plain text mode
Bookmarks