问题描述
如何使用 Python3.8 和 Pyside2 (pip) 安装和加载 Qmysql 驱动程序?我已经尝试下载 git:qtbase 并从那里编译驱动程序,但我很幸运.
How can i install and load the Qmysql driver using Pyside2 (pip) with python3.8? I've already tried to download git:qtbase and compiled the driver from there but I had any luck.
推荐答案
这个答案不仅涵盖了 Linux 的安装,还涵盖了其他操作系统,此外它还适用于 pyqt5
Qt 使用的二进制文件与 PyQt5/PySide2 使用的二进制文件相同,因为它们使用相同的基本代码,因此您必须编译插件.
The binaries used by Qt are the same ones used by PyQt5/PySide2 since they use the same base code so you will have to compile the plugins.
在这种情况下,要编译 mysql 插件,您必须遵循 官方手册,总结起来就是:
In this case, to compile the mysql plugin you must follow the official manual, which in summary is:
- 安装依赖项,在本例中为 mysql-connector-c
- 安装与编译 pyqt5/pyside2 相同版本的 Qt,以及 Windows 上的 MSVC、Ubuntu 上的 build-essentials、MacOS 上的 XCode 等开发工具.
- 下载源代码,在本例中为 qtbase 存储库.
- 编译插件.
要通过编译库的版本找出 Qt 的版本,可以使用以下命令:
To find out the version of Qt with the version that the library was compiled with, the following can be used:
- 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.该文件必须粘贴在路径中:
The above generates libqsqlmysql.so, qsqlmysql.dll or libqsqlmysql.dylib depending on the OS. That file must be pasted in the path:
- 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 来生成二进制文件:
To cover all the cases I have created a Github Actions that generates the binaries:
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-winx64liblibmysql.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-winx64include" MYSQL_LIBDIR="${{ github.workspace }}mysql-connector-c-6.1.11-winx64lib"
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 }}
前面的代码生成了插件,您可以在此处找到该插件.
The previous code generates the plugin that you can find here.
在 Ubuntu 的特定情况下,它可以简化为:
In the specific case of Ubuntu it can be reduced to:
- 将 libqsqlmysql.so 文件复制到 QT_SQL_DRIVER_PATH.莉>
- 执行
sudo apt install libmysqlclient-dev
- Copy the libqsqlmysql.so file to QT_SQL_DRIVER_PATH.
- Execute
sudo apt install libmysqlclient-dev
在 Windows 的特定情况下,它可以简化为:
In the specific case of Windows it can be reduced to:
- 将 qsqlmysql.dll 文件复制到 QT_SQL_DRIVER_PATH.莉>
- 下载windows 的 mysql-connector-c 并复制脚本旁边的 libmysql.dll.
- Copy the qsqlmysql.dll file to QT_SQL_DRIVER_PATH.
- Download the mysql-connector-c for windows and copy the libmysql.dll next to your script.
这篇关于无法在 PySide2 上加载 QMYSQL 驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!