2
Hello, at the moment I am Noob in manipulation of servers with databases. I am using Qt Creator 5.3 to develop my project, and have decided to incorporate a Mysql 5.7 database into it. I am trying to establish a simple connection between my application and the Mysql database, but I always come across the following error message:
I searched some similar foruns, and in most cases managed to solve the problem by copying the libmysql.dll file from the C: Program Files folder Mysql Mysql Server 5.7 lib to the executable folder of my project C: Users Syn Desktop build-Banco_mysql-Desktop_qt_5_3_mingw_32bit-Debug debug. But even following this step, and adding the lib path of the dll to the environment variables, the problem persists. Since I didn’t mess with databases and this kind of connection before, I have no idea what it might be. Below is the test code I’m using:
#include <QCoreApplication>
#include <QtSql>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
int main(int argc, char *argv[]){
QCoreApplication a(argc, argv);
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setDatabaseName("log_user");
db.setUserName("root");
if(db.open()){
qDebug() << "Conexão foi aberta com sucesso!";
}
else{
qDebug() << db.lastError().text();
qApp->quit();
}
QSqlQuery qery("select * from log_user");
if(qery.exec()){
while(qery.next())
qDebug() << qery.value(0).toString() << "|" << qery.value(1).toString();
}
else{
qDebug() << "Erro Fatal: " << qery.lastError().text();
}
return a.exec();
}
In the code I am trying to access the bank table and display its content on the console, but I always come across the same error message. Would anyone know any other step I might have missed or any solution? Already, I appreciate the collaboration.
Note: includes the Qt += sql line in . pro in my project, but it seems q the problem is not there.
According to the documentation of
QSqlQuery
, it is necessary to indicate the db, something like that:QSqlQuery qery("select * from log_user", db)
. See if it works that way.– stderr
You really need to use mysql, Voce could use sqlite, easier to connect, easier in several other aspects, the opposite is, if in your project you have foreseen the use of sqlprocedures, so only with other db msmo.
– Armando Marques Sobrinho
@zekk grateful for the answer but it still hasn’t worked :/, points out the same error "Unable to connect".
– Yuri Pires
Ah @Armando Marques Sobrinho , I intend to use the bank with a web application later, so I opted for mysql, I read in some places q Sqlite seems to have a limitation when using remotely :/
– Yuri Pires
Dude, I’ll tell you, I particularly believe that this limitation is the lack that sqlite has by the difficulty of implementing "storedprocedures" that in mysql is well served, but I have this site: www.irvb.org.br, it uses a remote sqlite database and all my applications use sqlite normally
– Armando Marques Sobrinho
although I myself have already begun to lean towards the dbs side "nosql", type xml, mongodb, etc, with the trend "websapps" they are a "hand on the wheel"
– Armando Marques Sobrinho
Interesting. I will give a studied in sqlite, to see if my application communicates better. Thank you, for the reply.
– Yuri Pires