Connecting database to program in local network - c++

I'm trying to connect a Qt program to a database existing in another computer.
I'm using Ms Access database and i already had it connected to my programme in the same computer ( with the QODBC driver) as below:
db1 = QSqlDatabase::addDatabase("QODBC");
db1.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=BDProjet.mdb");
now i'm trying to extend my programme to be a localnetwork programme so i need to connect my programme to the same database from another computer, so for testing it i'm using the loopback adresse (127.0.0.1) , i've already tried this but it's not working:
db1 = QSqlDatabase::addDatabase("QODBC");
db1.setHostName("localhost");
db1.setPort(3306);
db1.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=j\\BDProjet.mdb");
How i'm supposed to connect my database?

Try adding;
if(!db1.open())
{
// aaaargh!
qDebug() << db1.lastError();
}
else
{
// your database commands here
}
after the code that you have.

Related

Browser connects to mysql database but Qt application can't except localhost

From any ip address and browser mysql database can be connected and operated without any problems. But from my Qt application database can only be accessed when hostname is set to setHostName("localhost") not when ip of the device is entered.
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setDatabaseName("customdb");
db.setUserName("mojito");
db.setPassword("J0a1m8");
bool ok = db.open();
I get the error:
QSqlQuery::exec: database not open
When "127.0.0.1" is replaced with "localhost" connection is established. I checked and inserted relevant data to /etc/hosts but still issue occurs. So I can not type ip address and success with my Qt application code. Any suggestions ?

Connect to MSSQL Server without setting up ODBC Data Source in QT

I have an existing database using MSSQL Server and I want to connect my qt console application in the database. I think MSSQL is not supported by QT is there any chance I could connect my database without setting up ODBC?
Here is my code:
Btw when I used QMYSQL it says "QMYSQL driver is not loaded".
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("Database_Syncronizer");
db.setUserName("root");
db.setPassword("rootpassword");
if(!db.open())
qDebug()<<"Failed to connect database!";
else{
qDebug()<<"Successfully connected!";
testQuery(db);
}

How to connect to a MySQL database through ODBC from Qt application?

I have a freshly-installed MySQL server, which listens on localhost:3306. What is the correct way to connect to it from my Qt application?
It turned out that I need to add MySQL to the ODBC data sources. I did that after following this video tutorial - https://youtu.be/K3GZidOwGmM.
After I had added the DSN, I successfully connected to the MySQL server using this code:
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("Driver={MySQL ODBC 5.3 Unicode Driver};DATABASE=test;");
db.setUserName("root");
db.setPassword("password");
if (!db.open()) {
qDebug() << db.lastError().text();
} else {
qDebug("success");
}
Note: You will need to replace MySQL ODBC 5.3 Unicode Driver with the actual value listed in your DSN window. I got mine from here:

C++: can't connect to remote mysql database

I writing a c++ program that should connect to a mysql database.
it works successfully when I use the local database, but I get an error
"Can't connect to mysql database on '192.168.0.111' (111)"
when I try to connect to a database on another computer.
this is the function that test my connection:
void addb()
{
string mainServer="192.168.0.111";
string mainDbUser="root";
string mainDbPass="111";
MYSQL *connect; //database connection variable
connect=mysql_init(NULL);
if(!connect)
cout<<"Couldn't initiate connector\n";
if (mysql_real_connect(connect, mainServer.c_str(), mainDbUser.c_str(), mainDbPass.c_str(), "main" ,0,NULL,0))
{
cout<<" done\n";
}
else
{
cout<<mysql_error(connect)<<endl;
}
mysql_close (connect);
}
both computers are running Linux Mint OS.
I tried disabling my firewall on the second computer but i got the same problem.
note: I can access the database using phpmyadmin with my web browser.
Make sure the firewall allows MySQL port on the DB machine.
Make sure in the my.cnf the database is loaded with you have the proper network configuration (to listen to the public IP).
From the error message - I would suspect it is a firewall issue - so make sure the DB machine firewall allows incoming communication (and specifically - that SELinux enables it in addition to the firewall) and that the sending machine allows outgoing communication to this machine.

Is MySQL C++ Connector access to remote database possible?

I am accessing a MySQL database within a C++ app using MySQL C++ Connector. It works fine if I have the C++ and the MySQL on the same machine. So, something like the following code works fine:
sql::Connection *_con;
sql::mysql::MySQL_Driver *_driver;
_driver = sql::mysql::get_mysql_driver_instance();
_con = _driver->connect("tcp://127.0.0.1:3306", "user", "password");
However, I can't seem to access the database if it is located on another machine. So, something like this:
sql::Connection *_con;
sql::mysql::MySQL_Driver *_driver;
_driver = sql::mysql::get_mysql_driver_instance();
_con = _driver->connect("tcp://somesite.com:3306", "user", "password");
Is it just not possible or am I doing something wrong?
Did you accidentally setup your users so that they can only access your DB from the local machine?
Did you do
create user 'user'#'127.0.0.1' ...
or
create user 'user'#'%' ....
If you did the first then you won't be able to log on from a different machine.
Did you also grant the privileges correctly?
See the MySQL docs for a more in depth explanation on how to do this correctly
I have done this through a VPN so I am assuming it is possible. Are you using the correct port?