When I run following code:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("SecureChat");
db.setUserName("root");
db.setPassword("zTmUHsbEKZZlWhfofM");
bool ok = db.open();
qDebug() << db.lastError();
I receive error:
QT/C QSqlDatabase: QMYSQL driver not loaded on OSx
How to fix it on Mac m1?
CodePudding user response:
The original solution I have found here thanks to the original author of the question and answer - chriam.
I will describe in this post some key points that are not mentioned in the original solution.
You have to install MySQL from Oracle cloud
Use
QT maintenanceTool
and choose the optionAdd or remove components.
From the list, choose your current QT version and put a mark onSources
, then click next and wait for files to download.Follow insctruction here to install
ninja
cd
to your Src folder in my case:cd /Users/lamens/Qt/6.3.2/Src
Run the following command and wait for its complitaion
./configure -sql-mysql -- -DCMAKE_INCLUDE_PATH="/usr/local/mysql/include" -DCMAKE_LIBRARY_PATH="/usr/local/mysql/lib"
cd
to your sqldrivers folder in my case:cd /Users/lamens/Qt/6.3.2/macos/plugins/sqldrivers
Run
mkdir build_sqldrivers
and thencd build_sqldrivers
Run command:
/Users/<user>/Qt/<qt_version>/macos/bin/qt-cmake -G Ninja /Users/<user>/Qt/<qt_version>/Src/qtbase/src/plugins/sqldrivers -DCMAKE_INSTALL_PREFIX=/Users/<user>/Qt/<qt_version>/macos -DMySQL_INCLUDE_DIR="/usr/local/mysql/include" -DMySQL_LIBRARY="/usr/local/mysql/lib/libmysqlclient.dylib" -DCMAKE_OSX_ARCHITECTURES="arm64
Where<user>
is your system user and<qt_version>
is your QT version :D.sed -i -e 's/-arch x86_64/-arch arm64/g' /Users/<user>/Qt/<qt_version>/macos/plugins/sqldrivers/build_sqldrivers/build.ninja
if this fails, change at thebuild.ninja
(it is at thebuild_sqldrivers
folder) file all occurrences ofarch x86_64
to thearch arm64
.Run at the
build_sqldrivers
foldercmake --build .
Run at the
build_sqldrivers
foldercmake --install .
Then locate your lib using:
find ~/Qt -name libqsqlmysql.dylib
and move newly generatedlibqsqlmysql.dylib
to the/Users/<user>/Qt/<qt_version>/macos/plugins/sqldrivers
folder.Voilaa!