def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget (QWidget) """ super(SqlBrowserWidget, self).__init__(parent) self.setupUi(self) self.table.addAction(self.insertRowAction) self.table.addAction(self.deleteRowAction) if len(QSqlDatabase.drivers()) == 0: E5MessageBox.information( self, self.tr("No database drivers found"), self.tr( """This tool requires at least one Qt database driver. """ """Please check the Qt documentation how to build the """ """Qt SQL plugins.""")) self.connections.tableActivated.connect( self.on_connections_tableActivated) self.connections.schemaRequested.connect( self.on_connections_schemaRequested) self.connections.cleared.connect(self.on_connections_cleared) self.statusMessage.emit(self.tr("Ready"))
def initialize(self, dbtype=DB_TYPE_MEMORY, dbfile=DB_IN_MEMORY, db_name="db"): if dbtype != Database.DB_TYPE_MEMORY: self.db_file = dbfile is_new_file = not os.path.isfile(self.db_file) self.db = QSqlDatabase.addDatabase("QSQLITE", self.db_name) self.db.setDatabaseName(self.db_file) if not self.db.open(): print("\n ** Error opening DB: SQLite driver not loaded. DB name: %s\n" % self.db_file) print("\n Available drivers: ", QSqlDatabase.drivers()) sys.exit(-1) db_status, db_error = self.is_db_ok() if db_status is False: print("db.initialize() error:", db_error) return False, db_error if is_new_file: print("is new file, or IN MEMORY, setting initial schema version") self.set_schema_version(self.DB_VERSION) self._create_tables() self._upgrade_db_schema() return True, None
def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget (QWidget) """ super(SqlConnectionDialog, self).__init__(parent) self.setupUi(self) self.databasePicker.setMode(E5PathPickerModes.OpenFileMode) self.okButton = self.buttonBox.button(QDialogButtonBox.Ok) drivers = QSqlDatabase.drivers() # remove compatibility names if "QMYSQL3" in drivers: drivers.remove("QMYSQL3") if "QOCI8" in drivers: drivers.remove("QOCI8") if "QODBC3" in drivers: drivers.remove("QODBC3") if "QPSQL7" in drivers: drivers.remove("QPSQL7") if "QTDS7" in drivers: drivers.remove("QTDS7") self.driverCombo.addItems(drivers) self.__updateDialog() msh = self.minimumSizeHint() self.resize(max(self.width(), msh.width()), msh.height())
def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget (QWidget) """ super(SqlBrowserWidget, self).__init__(parent) self.setupUi(self) self.table.addAction(self.insertRowAction) self.table.addAction(self.deleteRowAction) if len(QSqlDatabase.drivers()) == 0: E5MessageBox.information( self, self.tr("No database drivers found"), self.tr( """This tool requires at least one Qt database driver. """ """Please check the Qt documentation how to build the """ """Qt SQL plugins.""" ), ) self.connections.tableActivated.connect(self.on_connections_tableActivated) self.connections.schemaRequested.connect(self.on_connections_schemaRequested) self.connections.cleared.connect(self.on_connections_cleared) self.statusMessage.emit(self.tr("Ready"))
def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget (QWidget) """ super(SqlConnectionDialog, self).__init__(parent) self.setupUi(self) self.databaseFileButton.setIcon(UI.PixmapCache.getIcon("open.png")) self.databaseFileCompleter = E5FileCompleter() self.okButton = self.buttonBox.button(QDialogButtonBox.Ok) drivers = QSqlDatabase.drivers() # remove compatibility names if "QMYSQL3" in drivers: drivers.remove("QMYSQL3") if "QOCI8" in drivers: drivers.remove("QOCI8") if "QODBC3" in drivers: drivers.remove("QODBC3") if "QPSQL7" in drivers: drivers.remove("QPSQL7") if "QTDS7" in drivers: drivers.remove("QTDS7") self.driverCombo.addItems(drivers) self.__updateDialog() msh = self.minimumSizeHint() self.resize(max(self.width(), msh.width()), msh.height())
def initialize(self): self.db = QSqlDatabase.addDatabase("QSQLITE", self.db_name) self.db.setDatabaseName(self.db_type) if not self.db.open(): print( "\n ** Error opening DB: SQLite driver not loaded. DB name: %s\n" % self.db_type) print("\n Available drivers: ", QSqlDatabase.drivers()) sys.exit(-1) self._create_tables()
def __init__(self): super(IndexWidget, self).__init__() hbox = QHBoxLayout(self) group_box = QGroupBox("数据库操作") hbox.addWidget(group_box) vbox = QVBoxLayout() group_box.setLayout(vbox) query_btn = QPushButton("查看数据") query_btn.clicked.connect(self.query_db) vbox.addWidget(query_btn) add_btn = QPushButton("增加一行") add_btn.clicked.connect(self.add) vbox.addWidget(add_btn) delete_btn = QPushButton("删除一行") delete_btn.clicked.connect(self.delete) vbox.addWidget(delete_btn) exit_btn = QPushButton("退出") exit_btn.clicked.connect(QApplication.exit) vbox.addWidget(exit_btn) self.table_view = QTableView() hbox.addWidget(self.table_view) #ctypes.windll.LoadLibrary('D:/Program Files/mysql-8.0.20-winx64/lib/libmysql.dll') print("可用数据库驱动:", QSqlDatabase.drivers()) db = QSqlDatabase.addDatabase("QMYSQL") db.setHostName("localhost") db.setPort(3306) db.setUserName("admin") db.setPassword("123456") db.setDatabaseName("itheima") if db.open(): print("打开成功") else: print("打开失败:", db.lastError().text()) return self.table_model = QSqlTableModel(db=db) self.table_model.setTable("student") self.table_model.setEditStrategy(QSqlTableModel.OnFieldChange) self.table_model.setHeaderData(0, Qt.Horizontal, "学号") self.table_model.setHeaderData(1, Qt.Horizontal, "姓名") self.table_model.setHeaderData(2, Qt.Horizontal, "年龄") self.table_model.setHeaderData(3, Qt.Horizontal, "性别") self.table_view.setModel(self.table_model) print("index inited.")
def qtInfo(): """Returns Qt/PyQt information. """ return {"Qt/PyQt information":{ "Qt version": QT_VERSION_STR, "PyQt version": PYQT_VERSION_STR, "SIP version": SIP_VERSION_STR, "Qt library paths": QApplication.libraryPaths(), "Qt database plugins": QSqlDatabase.drivers(), "Qt image plugins": QImageReader.supportedImageFormats()}}
def initialize(self, dbfile): if self.db != None: QSqlDatabase.removeDatabase(self.db_name) self.close() self.db_file = dbfile self.db = QSqlDatabase.addDatabase("QSQLITE", self.db_name) self.db.setDatabaseName(self.db_file) if not self.db.open(): print("\n ** Error opening DB: SQLite driver not loaded. DB name: %s\n" % self.db_file) print("\n Available drivers: ", QSqlDatabase.drivers()) sys.exit(-1) self._create_tables()
def initialize(self, dbtype=DB_TYPE_MEMORY, dbfile=DB_IN_MEMORY, db_name="db"): if dbtype != Database.DB_TYPE_MEMORY: self.db_file = dbfile self.db = QSqlDatabase.addDatabase("QSQLITE", self.db_name) self.db.setDatabaseName(self.db_file) if not self.db.open(): print("\n ** Error opening DB: SQLite driver not loaded. DB name: %s\n" % self.db_file) print("\n Available drivers: ", QSqlDatabase.drivers()) sys.exit(-1) db_status, db_error = self.is_db_ok() if db_status is False: print("db.initialize() error:", db_error) return False, db_error self._create_tables() return True, None
import sys from PyQt5.QtWidgets import QApplication from PyQt5.QtSql import QSqlDatabase if __name__ == '__main__': app = QApplication(sys.argv) print('Available drivers:') drivers = QSqlDatabase.drivers() for driver in drivers: print(driver) sys.exit(app.exec_())
# model = QtSql.QSqlQueryModel(parent=None) # model.setQuery("SELECT * FROM films") # model.query().exec_() # self.tableView.setModel(model) # self.tableView.show() # # db_connect.close() con = sqlite3.connect("films.db") cur = con.cursor() rows = cur.execute("""SELECT title, year, duration FROM Films WHERE genre in ( SELECT id FROM genres WHERE title = "музыка" Or title == "анимация") And year >= 1997""" ).fetchall() for row in rows: inx = rows.index(row) self.tableWidget.insertRow(inx) # add more if there is more columns in the database. self.tableWidget.setItem(inx, 0, QTableWidgetItem(row[0])) self.tableWidget.setItem(inx, 1, QTableWidgetItem(row[1])) self.tableWidget.setItem(inx, 2, QTableWidgetItem(row[2])) con.close() if __name__ == '__main__': app = QApplication(sys.argv) print(list(map(str, QSqlDatabase.drivers()))) ex = Film_search() ex.show() sys.exit(app.exec())