|
| 1 | +import sys |
| 2 | + |
| 3 | +from PyQt5.QtSql import * |
| 4 | +from PyQt5.QtCore import Qt, QModelIndex |
| 5 | +from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QPushButton, \ |
| 6 | + QTableWidget, QTableWidgetItem, QMessageBox, QHBoxLayout, QLineEdit, QLabel, QGridLayout |
| 7 | + |
| 8 | + |
| 9 | +class Example(QWidget): |
| 10 | + def __init__(self, parent=None): |
| 11 | + super(Example, self).__init__(parent) |
| 12 | + |
| 13 | + self.table = QTableWidget(0, 3) |
| 14 | + self.table.setHorizontalHeaderLabels(['ID', 'NOMBRE', 'APELLIDO']) |
| 15 | + self.table.setAlternatingRowColors(True) |
| 16 | + self.table.setEditTriggers(QTableWidget.NoEditTriggers) |
| 17 | + self.table.setSelectionBehavior(QTableWidget.SelectRows) |
| 18 | + self.table.setSelectionMode(QTableWidget.SingleSelection) |
| 19 | + |
| 20 | + self.lblID = QLabel("ID:") |
| 21 | + self.txtID = QLineEdit() |
| 22 | + self.txtID.setPlaceholderText("Numero identificador unico") |
| 23 | + |
| 24 | + self.lblName = QLabel("Nombre:") |
| 25 | + self.txtName = QLineEdit() |
| 26 | + self.txtName.setPlaceholderText("Nombre de la persona") |
| 27 | + |
| 28 | + self.lblApellido = QLabel("Apellido:") |
| 29 | + self.txtApellido = QLineEdit() |
| 30 | + self.txtApellido.setPlaceholderText("Apellido de la persona") |
| 31 | + |
| 32 | + grid = QGridLayout() |
| 33 | + grid.addWidget(self.lblID, 0, 0) |
| 34 | + grid.addWidget(self.txtID, 0, 1) |
| 35 | + grid.addWidget(self.lblName, 1, 0) |
| 36 | + grid.addWidget(self.txtName, 1, 1) |
| 37 | + grid.addWidget(self.lblApellido, 2, 0) |
| 38 | + grid.addWidget(self.txtApellido, 2, 1) |
| 39 | + |
| 40 | + btnCargar = QPushButton('Cargar Datos') |
| 41 | + btnCargar.clicked.connect(self.cargarDatos) |
| 42 | + |
| 43 | + btnInsertar = QPushButton('Insertar') |
| 44 | + btnInsertar.clicked.connect(self.insertarDatos) |
| 45 | + |
| 46 | + btnEliminar = QPushButton('Eliminar') |
| 47 | + btnEliminar.clicked.connect(self.eliminarDatos) |
| 48 | + |
| 49 | + hbx = QHBoxLayout() |
| 50 | + hbx.addWidget(btnCargar) |
| 51 | + hbx.addWidget(btnInsertar) |
| 52 | + hbx.addWidget(btnEliminar) |
| 53 | + |
| 54 | + vbx = QVBoxLayout() |
| 55 | + vbx.addLayout(grid) |
| 56 | + vbx.addLayout(hbx) |
| 57 | + vbx.setAlignment(Qt.AlignTop) |
| 58 | + vbx.addWidget(self.table) |
| 59 | + |
| 60 | + self.setWindowTitle("PyQT :: SQLite Data Access") |
| 61 | + self.resize(362, 320) |
| 62 | + self.setLayout(vbx) |
| 63 | + |
| 64 | + def cargarDatos(self, event): |
| 65 | + index = 0 |
| 66 | + query = QSqlQuery() |
| 67 | + query.exec_("select * from person") |
| 68 | + |
| 69 | + while query.next(): |
| 70 | + ids = query.value(0) |
| 71 | + nombre = query.value(1) |
| 72 | + apellido = query.value(2) |
| 73 | + |
| 74 | + self.table.setRowCount(index + 1) |
| 75 | + self.table.setItem(index, 0, QTableWidgetItem(str(ids))) |
| 76 | + self.table.setItem(index, 1, QTableWidgetItem(nombre)) |
| 77 | + self.table.setItem(index, 2, QTableWidgetItem(apellido)) |
| 78 | + |
| 79 | + index += 1 |
| 80 | + |
| 81 | + def insertarDatos(self, event): |
| 82 | + ids = int(self.txtID.text()) |
| 83 | + nombre = self.txtName.text() |
| 84 | + apellido = self.txtApellido.text() |
| 85 | + |
| 86 | + query = QSqlQuery() |
| 87 | + query.exec_("insert into person values({0}, '{1}', '{2}')".format(ids, nombre, apellido)) |
| 88 | + |
| 89 | + def eliminarDatos(self, event): |
| 90 | + selected = self.table.currentIndex() |
| 91 | + if not selected.isValid() or len(self.table.selectedItems()) < 1: |
| 92 | + return |
| 93 | + |
| 94 | + ids = self.table.selectedItems()[0] |
| 95 | + query = QSqlQuery() |
| 96 | + query.exec_("delete from person where id = " + ids.text()) |
| 97 | + |
| 98 | + self.table.removeRow(selected.row()) |
| 99 | + self.table.setCurrentIndex(QModelIndex()) |
| 100 | + |
| 101 | + def db_connect(self, filename, server): |
| 102 | + db = QSqlDatabase.addDatabase(server) |
| 103 | + db.setDatabaseName(filename) |
| 104 | + if not db.open(): |
| 105 | + QMessageBox.critical(None, "Cannot open database", |
| 106 | + "Unable to establish a database connection.\n" |
| 107 | + "This example needs SQLite support. Please read the Qt SQL " |
| 108 | + "driver documentation for information how to build it.\n\n" |
| 109 | + "Click Cancel to exit.", QMessageBox.Cancel) |
| 110 | + return False |
| 111 | + return True |
| 112 | + |
| 113 | + def db_create(self): |
| 114 | + query = QSqlQuery() |
| 115 | + query.exec_("create table person(id int primary key, " |
| 116 | + "firstname varchar(20), lastname varchar(20))") |
| 117 | + query.exec_("insert into person values(101, 'Danny', 'Young')") |
| 118 | + query.exec_("insert into person values(102, 'Christine', 'Holand')") |
| 119 | + query.exec_("insert into person values(103, 'Lars', 'Gordon')") |
| 120 | + query.exec_("insert into person values(104, 'Roberto', 'Robitaille')") |
| 121 | + query.exec_("insert into person values(105, 'Maria', 'Papadopoulos')") |
| 122 | + |
| 123 | + def init(self, filename, server): |
| 124 | + import os |
| 125 | + if not os.path.exists(filename): |
| 126 | + self.db_connect(filename, server) |
| 127 | + self.db_create() |
| 128 | + else: |
| 129 | + self.db_connect(filename, server) |
| 130 | + |
| 131 | +if __name__ == '__main__': |
| 132 | + app = QApplication(sys.argv) |
| 133 | + ejm = Example() |
| 134 | + ejm.init('datafile', 'QSQLITE') |
| 135 | + ejm.show() |
| 136 | + sys.exit(app.exec_()) |
| 137 | + |
0 commit comments