qt-cmake: command not found
-
@MH24
I don't know what qmake has to do with anything when you are showing your executable built and running.You do not show your
addDatabase()
call from your code so we can see you trying to use the MySQL driver. But:QSqlDatabase: QMySQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMARIADB QMYSQL QMYSQL3
So did you at least try with
QMYSQL
notQMySQL
? -
@JonB said in qt-cmake: command not found:
@MH24
I don't know what qmake has to do with anything when you are showing your executable built and running.You do not show your
addDatabase()
call from your code so we can see you trying to use the MySQL driver. But:QSqlDatabase: QMySQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMARIADB QMYSQL QMYSQL3
So did you at least try with
QMYSQL
notQMySQL
?Well,
qmake
was mentioned in the official docs for Qt5 driver installation.Yes, my mistake was that after I had moved the drivers' shared objects to the sqldriver folder I had written
QMySQL
instead ofQMYSQL
in my code copied from official docs. However, my database is not open so I dont know whether the driver worksThis is my code:
#include <QSqlDatabase> #include <QSql> #include <QSqlQueryModel> #include <QSqlQuery> #include <QSqlError> #include <QSqlDriver> //class declaration, rest of the functions etc QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setDatabaseName("uniapp"); db.setUserName("root"); db.setPassword("******"); bool ok = db.open(); QSqlQuery query; query.exec("INSERT INTO employee (id, name)" "VALUES (1001, 'XYZ')");
This gives
QSqlQuery::exec: database not open
.
If I remove the lines of query from the code there is no error even if I give wrong password etc.
Apologies if this thread has diverted, but I need to be sure Qt is connecting to my MySQL database.
I have the server running on my machine withmysql -u root -p
andSHOW DATABASES;
shows my database. -
@MH24 said in qt-cmake: command not found:
bool ok = db.open();
This gives
QSqlQuery::exec: database not open.
You are supposed to test the
ok
result, else what is the point of assigning it?Presumably it is returning false, hence the error message. In which case what does
db.lastError()
return, as per the documentation? -
@JonB said in qt-cmake: command not found:
@MH24 said in qt-cmake: command not found:
bool ok = db.open();
This gives
QSqlQuery::exec: database not open.
You are supposed to test the
ok
result, else what is the point of assigning it?Presumably it is returning false, hence the error message. In which case what does
db.lastError()
return, as per the documentation?It returns this:
QSqlError("1698", "QMYSQL: Unable to connect", "Access denied for user 'root'@'localhost'")
-
@MH24
So that is your answer. I think that indicates the QMYSQL driver from Qt is working OK. I am also guessing this is not an incorrect password issue, I assume that would give a different message (though I cannot be sure).You might try a database other than
uniapp
, such asmysql
orinformation_schema
, to see whether that alters the situation. -
@MH24 said in qt-cmake: command not found:
Access denied for user 'root'@'localhost'
Very obvoius where the problem is - the user root@localhost has no access rights to this database (and imo never should have access)
-
@JonB
I tried using different database but same error. I tried granting privileges to the database using
grant all privileges on user_database.* to user_user@'localhost' ; grant all privileges on user_database.* to user_user@'%' ;
but to no avail.
-
@MH24
It vaguely, vaguely rings a bell that you have to do something special aboutroot
, possibly to do with how MySQL gets installed initially. Lots of hits for Googlingmysql root localhost permission
. I may have done the stuff mentioned in https://stackoverflow.com/a/46908573 in the past. (Be careful you don't lock yourself out, however!)But in any case what you have done now is better, as @Christian-Ehrlicher said you should not be using
root
user for your application.