gpt4 book ai didi

python - 无法在 PySide2 上加载 QMYSQL 驱动程序

转载 作者:行者123 更新时间:2023-12-05 02:49:16 25 4
gpt4 key购买 nike

如何使用 Pyside2 (pip) 和 python3.8 安装和加载 Qmysql 驱动程序?我已经尝试下载 git:qtbase 并从那里编译驱动程序,但我很幸运。

最佳答案

这个答案不仅包括Linux的安装,还包括其他操作系统的安装,除此之外,它还适用于pyqt5


Qt 使用的二进制文件与 PyQt5/PySide2 使用的二进制文件相同,因为它们使用相同的基本代码,因此您必须编译插件。

在这种情况下,要编译 mysql 插件,您必须遵循 the official manual ,总结起来就是:

  1. 安装依赖项,在本例中为 mysql-connector-c
  2. 安装与编译pyqt5/pyside2相同版本的Qt和开发工具,例如Windows上的MSVC、Ubuntu上的build-essentials、MacOS上的XCode等。
  3. 下载源代码,在本例中为 qtbase repository .
  4. 编译插件。

要找出 Qt 的版本和编译库的版本,可以使用以下方法:

  • PyQt5
python -c "from PyQt5.QtCore import QT_VERSION_STR; print('Qt version', QT_VERSION_STR)"
  • PySide2
python -c "from PySide2.QtCore import qVersion; print('Qt version', qVersion())"

以上根据操作系统生成 libqsqlmysql.so、qsqlmysql.dll 或 libqsqlmysql.dylib。该文件必须粘贴在路径中:

  • PyQt5:
python -c "import os; from PyQt5.QtCore import QLibraryInfo; print('QT_SQL_DRIVER_PATH', os.path.join(QLibraryInfo.location(QLibraryInfo.PrefixPath), 'plugins', 'sqldrivers'))"
  • PySide2:
python -c "import os; from PySide2.QtCore import QLibraryInfo; print('QT_SQL_DRIVER_PATH', os.path.join(QLibraryInfo.location(QLibraryInfo.PrefixPath), 'plugins', 'sqldrivers'))"

为了涵盖所有案例,我创建了一个生成二进制文件的 Github Actions:

mysql_plugin.yml

name: generate_mysql_plugin

on: [push]

jobs:
ci:
name: ${{ matrix.os.name }} Qt-${{ matrix.qt.qt_version }}
runs-on: ${{ matrix.os.runs-on }}
strategy:
fail-fast: false
matrix:
os:
- name: Windows
extension: "dll"
runs-on: windows-2019
- name: Linux
extension: "so"
runs-on: ubuntu-20.04
- name: MacOS
extension: "dylib"
runs-on: macos-10.15
qt:
- name: 5.15
qt_version: 5.15.0
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install Qt
uses: jurplel/install-qt-action@v2
with:
version: ${{ matrix.qt.qt_version }}
dir: ${{ github.workspace }}/qt/
- name: clone qtbase
run: git clone -b ${{ matrix.qt.qt_version }} https://code.qt.io/qt/qtbase.git
- name: Compile mysql plugin on Windows
if: matrix.os.name == 'Windows'
shell: cmd
run: |
choco install wget
wget https://downloads.mysql.com/archives/get/p/19/file/mysql-connector-c-6.1.11-winx64.zip
unzip mysql-connector-c-6.1.11-winx64.zip
copy /y "mysql-connector-c-6.1.11-winx64\lib\libmysql.dll" .
call "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Auxiliary/Build/vcvars64.bat"
cd qtbase/src/plugins/sqldrivers
qmake -- MYSQL_INCDIR="${{ github.workspace }}\mysql-connector-c-6.1.11-winx64\include" MYSQL_LIBDIR="${{ github.workspace }}\mysql-connector-c-6.1.11-winx64\lib"
nmake sub-mysql
nmake install
- name: Compile mysql plugin on Linux
if: matrix.os.name == 'Linux'
run: |
wget https://downloads.mysql.com/archives/get/p/19/file/mysql-connector-c-6.1.11-linux-glibc2.12-x86_64.tar.gz
tar zxvf mysql-connector-c-6.1.11-linux-glibc2.12-x86_64.tar.gz
sudo cp mysql-connector-c-6.1.11-linux-glibc2.12-x86_64/lib/*.so /usr/lib/x86_64-linux-gnu
sudo apt-get install freetds-dev
cd qtbase/src/plugins/sqldrivers
qmake
cd mysql
qmake
make
make install
- name: Compile mysql plugin on MacOS
if: matrix.os.name == 'MacOs'
run: |
brew install wget
wget https://cdn.mysql.com/archives/mysql-connector-c/mysql-connector-c-6.1.11-macos10.12-x86_64.tar.gz
tar zxvf mysql-connector-c-6.1.11-macos10.12-x86_64.tar.gz
sudo cp mysql-connector-c-6.1.11-macos10.12-x86_64/lib/libmysqlclient.dylib mysql-connector-c-6.1.11-macos10.12-x86_64/lib/libmysqlclient_r.dylib
sudo cp mysql-connector-c-6.1.11-macos10.12-x86_64/lib/libmysqlclient.18.dylib mysql-connector-c-6.1.11-macos10.12-x86_64/lib/libmysqlclient_r.18.dylib

sudo cp mysql-connector-c-6.1.11-macos10.12-x86_64/lib/*.dylib /usr/local/lib
cd qtbase/src/plugins/sqldrivers
qmake -- MYSQL_PREFIX="${{ github.workspace }}/mysql-connector-c-6.1.11-macos10.12-x86_64"
make sub-mysql
cd mysql
make install
- name: upload
uses: actions/upload-artifact@v2
with:
path: qtbase/src/plugins/sqldrivers/plugins/sqldrivers/*qsqlmysql.${{ matrix.os.extension }}
name: mysqlplugin-${{ matrix.os.name }}-Qt${{ matrix.qt.name }}

前面的代码生成了你可以找到的插件 here .


在 Ubuntu 的特定情况下,它可以简化为:

  • 复制 libqsqlmysql.so文件到 QT_SQL_DRIVER_PATH。
  • 执行sudo apt install libmysqlclient-dev

在 Windows 的特定情况下,它可以简化为:

关于python - 无法在 PySide2 上加载 QMYSQL 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64056483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com