Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

python - Unable to load QMYSQL Driver on PySide2

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This answer covers not only the installation for Linux but for the other OS, besides that it also applies for pyqt5


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


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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...