Connect to MYSQL failed in Qt
-
I had linked the library and include in the .pro profile file
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/libmysql else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/libmysqld else:unix: LIBS += -L$$PWD/lib/libmysql INCLUDEPATH += $$PWD/include DEPENDPATH += $$PWD/include
and included the mysql file:
#include <mysql.h>
And this is my mysql connect code:
MYSQL_RES* result; MYSQL_ROW row; MYSQL* connection(mysql_init(NULL)); connection = mysql_real_connect(connection, "123", "root", "", "pos", 0, NULL, 0); if (!connection) { ui->error->setText("Unable to connect to server!Please contact your Administrator"); }
But when i try to build,i get these error:
error: undefined reference to 'mysql_init'
error: undefined reference to 'mysql_real_connect'
error: linker command failed with exit code 1 (use -v to see invocation) clang++: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [Makefile:73: libPos_x86_64.so] Error 1
Someone please help me solve this 🙏
-
Hi,
Based on your .pro file content, you are not linking against the MySQL library.
By the way, are you aware of the QtSql module ?
-
@ELEMENTICY said in Connect to MYSQL failed in Qt:
And yea,i copy the mysql include and lib folder to the project folder
But you don't link against the mysql libs in your pro file.
What does not work with the QtSql plugin?
-
@Christian-Ehrlicher i do,
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/libmysql else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/libmysqld else:unix: LIBS += -L$$PWD/lib/libmysql
And yea,use qt sql it just give like 11 error
And if i use official mysql mysql.h,it only give 2 error which is undefineAnd yea,or i need to change my profile's lib code to this?
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/libmysql.lib else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/libmysqld.lib else:unix: LIBS += -L$$PWD/lib/libmysql.lib
-
will it bcuz i use qt6+ so it doesnt work?(i mean qt sql)
-
-
@Christian-Ehrlicher bcuz it give too much error,i rather use official mysql header
Qt6 has alot of stuff doesnt exist.Like web browser
So i rather use the official mysql header and it give less errorThese are the only error:
error: undefined reference to 'mysql_init'
error: undefined reference to 'mysql_real_connect'
error: linker command failed with exit code 1 (use -v to see invocation) clang++: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [Makefile:73: libPos_x86_64.so] Error 1
Use Qt sql give like 11 error or more/less
-
The SQL module is supported in Qt 6. However the MySQL plugin might not be provided because Oracle changed their licensing.
As for your build issue: -L is for giving path to search, it's -l for stating the name of the library.
-
So the QtSql module has no errors - please stay specific!
else:unix: LIBS += -L$$PWD/lib/libmysql.lib
This is for sure not correct.
-
@Christian-Ehrlicher but qtsql mysql plugin doesnt provided as @SGaist said it,i tried add it by following youtube tutorial,it still doesnt.Lemme try it again and send the error here
-
-
@ELEMENTICY said in Connect to MYSQL failed in Qt:
This is for sure not correct.
oh,how do i fix it?
Re-read my previous answer.
-
No space no "lib" prefix.
It's explained in the documentation
-
@SGaist my current profile code:
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/ -llibmysql else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/ -llibmysqld else:unix: LIBS += -L$$PWD/lib/ -llibmysql
documentation:
LIBS += -L"3rdparty/CatWhisperer/lib" -lCatWhisperer
i got this error:
:-1: error: error: cannot find -llibmysql
proof that path exists:
-
@ELEMENTICY said in Connect to MYSQL failed in Qt:
And yea,use qt sql it just give like 11 error
What does this mean?
-
@ELEMENTICY did you read what I wrote ? No "lib" prefix.
-
@SGaist
current code:win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/libmysql else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/libmysqld else:unix: LIBS += -L$$PWD/lib/libmysql
idk if i misunderstanded what u mean,but it still give error ;/
or u mean i have to remove that -L