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 maintenanceTooland 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
ninjacdto your Src folder in my case:cd /Users/lamens/Qt/6.3.2/SrcRun 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"cdto your sqldrivers folder in my case:cd /Users/lamens/Qt/6.3.2/macos/plugins/sqldriversRun
mkdir build_sqldriversand thencd build_sqldriversRun 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="arm64Where<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.ninjaif this fails, change at thebuild.ninja(it is at thebuild_sqldriversfolder) file all occurrences ofarch x86_64to thearch arm64.Run at the
build_sqldriversfoldercmake --build .Run at the
build_sqldriversfoldercmake --install .Then locate your lib using:
find ~/Qt -name libqsqlmysql.dyliband move newly generatedlibqsqlmysql.dylibto the/Users/<user>/Qt/<qt_version>/macos/plugins/sqldriversfolder.Voilaa!
