本文介绍了无法在PySide2上加载QMYSQL驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案



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


在这种情况下,要编译mysql插件,您必须遵循,其摘要为:


  1. 安装依赖项,在这种情况下为

  2. 安装与pyqt5 / pyside2编译版本相同的Qt以及开发工具,例如Windows上的MSVC,Ubuntu上的build-Esentials,MacOS上的XCode,等等。

  3. 下载源代码,在这种情况下为。

  4. 编译插件。

要找出Qt的版本与编译库的版本,可以使用以下代码:二手:



  • PyQt5


  python -c从PyQt5.QtCore导入QT_VERSION_STR;打印( Qt版本,QT_VERSION_STR) 



  • PySide2


  python -c来自PySide2.QtCore import qVersion;打印( Qt版本,qVersion()); 

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



  • PyQt5:


  python -c"导入操作系统;从PyQt5.QtCore导入QLibraryInfo; print('QT_SQL_DRIVER_PATH',os.path.join(QLibraryInfo.location(QLibraryInfo.PrefixPath),'plugins','sqldrivers))'; 



  • PySide2:


  python -c" import os;从PySide2.QtCore导入QLibraryInfo; print('QT_SQL_DRIVER_PATH',os.path.join(QLibraryInfo.location(QLibraryInfo.PrefixPath),'plugins','sqldrivers))'; 




为了涵盖所有情况,我创建了一个Github Actions来生成二进制文件: / p>

 名称:generate_mysql_plugin 

:[推]

职位:
ci:
名称:$ {{matrix.os.name}} Qt-$ {{matrix.qt.qt_version}}
运行:$ {{matrix.os.runs-on}}
策略:
故障快速:假
矩阵:
os:
-名称:Windows
扩展名: dll
运行时间:Windows-2019
-名称:Linux
扩展名: so
运行:ubuntu-20.04
-名称:MacOS
扩展名: dylib;
运行:macos-10.15
qt:
-名称:5.15
qt_version:5.15.0
步骤:
-名称:Checkout
使用:actions / checkout @ v1
-名称:Install Qt
使用:jurplel / install-qt-action @ v2
其中:
版本:$ {{矩阵。 qt.qt_version}}
目录:$ {{github.workspace}} / qt /
-名称:clone qtbase
运行:git clone -b $ {{matrix.qt.qt_version} } https://code.qt.io/qt/qtbase.git
-名称:在Windows
上编译mysql插件,如果:matrix.os.name =='Windows'
shell: cmd
运行:|
choco install wget
wget https://downloads.mysql.com/archives/get/p/19/file/mysql-connector-c-6.1.11-winx64.zip
解压缩mysql-connector-c-6.1.11-winx64.zip
复制/ y mysql-connector-c-6.1.11-winx64\lib\libmysql.dll 。
调用 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
-名称:在Linux
上编译mysql插件,如果:matrix.os.name =='Linux'
运行:|
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
进行安装
-名称:在MacOS
上编译mysql插件,如果:matrix.os.name =='MacOs'
运行:|
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;
使子mysql
cd mysql
使安装
-名称:上传
用途:actions / upload-artifact @ v2
与:
路径:qtbase / src / plugins / sqldrivers / plugins / sqldrivers / * qsqlmysql。$ {{matrix.os.extension}}
名称:mysqlplugin-$ {{matrix.os.name}}-Qt $ { {matrix.qt.name}}

前面的代码生成了可以在。




可以将其简化为:



  • 复制文件移至QT_SQL_DRIVER_PATH。

  • 执行 sudo apt install libmysqlclient-dev




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



  • 复制,然后将libmysql.dll复制到脚本旁边。


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.

解决方案

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.

In this case, to compile the mysql plugin you must follow the official manual, which in summary is:

  1. Install the dependencies, in this case the mysql-connector-c
  2. Install Qt of the same version with which pyqt5/pyside2 was compiled and development tools such as MSVC on windows, build-essentials on Ubuntu, XCode on MacOS, etc.
  3. Download the source code, in this case the qtbase repository.
  4. Compile the plugin.

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())"

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'))"


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-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 }}

The previous code generates the plugin that you can find here.


In the specific case of Ubuntu it can be reduced to:

  • Copy the libqsqlmysql.so file to QT_SQL_DRIVER_PATH.
  • Execute sudo apt install libmysqlclient-dev

In the specific case of Windows it can be reduced to:

这篇关于无法在PySide2上加载QMYSQL驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-22 21:27