How to connect a Mysql database with QT on a local network?

Asked

Viewed 828 times

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:

inserir a descrição da imagem aqui

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.

  • 1

    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.

  • 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.

  • @zekk grateful for the answer but it still hasn’t worked :/, points out the same error "Unable to connect".

  • 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 :/

  • 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

  • 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"

  • Interesting. I will give a studied in sqlite, to see if my application communicates better. Thank you, for the reply.

Show 2 more comments

2 answers

1

Yuri,

I noticed that at the opening of your Mysql connection driver you specify the same Databasename (database name) as the table that you try to perform the SELECT ahead (log_user). I believe the problem is this, unless you have a database called "log_user" too (which doesn’t make much sense, a bd just for user log).

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

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

I hope I’ve been able to help.

  • It did help, thanks for the answer. The name of the bank is correct, is 'log_user' what happens is that I am testing all types of transactions with the bank first, and then perform the Integration for my application. That’s why I created this log_user database for the tests.

  • Is your problem solved or can I help you with something else? I just remembered one detail: Mysql is case-sensitive for tables and columns, so make sure the table name is written exactly as stated in the database.

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 oracle’s Mysql 5.7. I decided to test the connection to Xampp’s integrated mysql and connected normally. At first I thought it might 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. Thank you for your reply. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.