How to connect a MySql database with QT over a LAN?

2

Hello, at the moment I'm noob in handling servers with databases. I am using Qt Creator 5.3 to develop my project, and I decided to incorporate a Mysql 5.7 bank to it. I'm trying to establish a simple connection between my application and the MySql database, however I always encounter the following error message:

InsomecasesIlookedforsimilarproblems,andinmostcaseswereabletosolvetheproblembycopyingthelibmysql.dllfilefromtheC:\ProgramFiles\MySQL\MySQLServer5.7\libfoldertotheprojectexecutablefolderC:\Users\Syn\Desktop\build-Bank_MySql-Desktop_Qt_5_3_MinGW_32bit-Debug\debug.Butevenfollowingthisstep,andaddingthelibpathofthedlltotheenvironmentvariables,theproblempersists.SinceIdidnotmessaroundwiththisdatabaseandthistypeofconnectionbefore,Ihavenoideawhatitmightbe.HereisthetestcodeI'musing:

#include<QCoreApplication>#include<QtSql>#include<QSqlDatabase>#include<QSqlQuery>#include<QDebug>intmain(intargc,char*argv[]){QCoreApplicationa(argc,argv);QSqlDatabasedb;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'm trying to access the database table and display its contents in the console, but I always come across the same error message. Would anyone else know of another step I might have missed or some solution? Thanks in advance.

    
asked by anonymous 23.08.2016 / 22:30

2 answers

0

Well, after breaking my head with some research I found out where the problem comes from, but not necessarily the complete solution. I was using Mysql 5.7 from Oracle. I tried to test the connection with the built-in mysql of Xampp and connected normally. At first axei that could be compatibility problem, but both my Qt creator and Mysql are 32-bit. In any case the solution to my problem will be to switch to another mysql editor. Thanks for everyone's response. Thanks!

    
24.08.2016 / 21:49
1

Yuri,

I noticed that at the opening of your MySQL connection driver you specify the same DatabaseName as the table that you are trying to do SELECT forward (log_user). I think the problem is this, unless you have a database called "log_user" too (which does not make much sense, a bd just for user logging).

Please check the correct name of your database and make this change.

Remembering that the zekk comment is very important because this is the correct way to perform a specific query in a database through QSqlQuery.

I hope I have been able to help.

    
24.08.2016 / 12:42