coding utf-8 import sys from PyQt4 import QtGui QtCore QtSql def creat

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore, QtSql
def createConnection():
db = QtSql.QSqlDatabase.addDatabase("QPSQL")
db.setHostName("localhost")
db.setDatabaseName("test")
db.setUserName("xa4a")
db.setPassword("")
if not db.open():
QtGui.QMessageBox.critical(0, QtGui.qApp.tr("Cannot open database"),
QtGui.qApp.tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information "
"how to build it.\n\nClick Cancel to exit."),
QtGui.QMessageBox.Cancle, QtGui.QMessageBox.NoButton)
return False
return True
def initializeModel(model):
model.setTable("employee")
model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
model.setRelation(2, QtSql.QSqlRelation("city", "id", "name"))
model.setRelation(3, QtSql.QSqlRelation("country", "id", "name"))
model.setHeaderData(0, QtCore.Qt.Horizontal,
QtCore.QVariant(QtCore.QObject.tr(model, "ID")))
model.setHeaderData(1, QtCore.Qt.Horizontal,
QtCore.QVariant(QtCore.QObject.tr(model, "Name")))
model.setHeaderData(2, QtCore.Qt.Horizontal,
QtCore.QVariant(QtCore.QObject.tr(model, "City")))
model.setHeaderData(3, QtCore.Qt.Horizontal,
QtCore.QVariant(QtCore.QObject.tr(model, "Country")))
model.select()
def createView(title, model):
view = QtGui.QTableView()
view.setModel(model)
view.setItemDelegate(QtSql.QSqlRelationalDelegate(view))
view.setWindowTitle(title)
return view
def createRelationalTables():
query = QtSql.QSqlQuery()
query.exec_("create table employee(id int, name varchar(20), city int, country int)")
query.exec_("insert into employee values(1, 'Espen', 5000, 47)")
query.exec_("insert into employee values(2, 'Harald', 80000, 49)")
query.exec_("insert into employee values(3, 'Sam', 100, 41)")
query.exec_("create table city(id int, name varchar(20))")
query.exec_("insert into city values(100, 'San Jose')")
query.exec_("insert into city values(5000, 'Oslo')")
query.exec_("insert into city values(80000, 'Munich')")
query.exec_("create table country(id int, name varchar(20))")
query.exec_("insert into country values(41, 'USA')")
query.exec_("insert into country values(47, 'Norway')")
query.exec_("insert into country values(49, 'Germany')")
def addRecord(model ):
row = model.rowCount()
model.insertRow(row)
val = (0, 'pippo', 1, 1)
for column in range(1,4):
model.setData(model.index(row,column), QtCore.QVariant(val[column]))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
if not createConnection():
sys.exit(1)
createRelationalTables()
model = QtSql.QSqlRelationalTableModel()
initializeModel(model)
view = createView(model.tr("Relational Table Model"), model)
addRecord(model )
view.show()
sys.exit(app.exec_())