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

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:

Related

How to get valid ODBC connection string from Microsoft Windows "ODBC Data Source Administrator (64-bit)" - Firebird case?

I have installed Firebird ODBC driver on my computer and I have entered data for the new connection inside Microsoft Windows "ODBC Data Source Administrator (64-bit)" and "Test Connection" was successful and I can also use this DSN (Data Source Name) from Power BI Desktop.
But now I am trying to use Firebird ODBC driver from other application (custom M Language connector, that works atop Firebird ODBC driver and that I develop to enable DirectQuery access mode in Power BI Desktop, ODBC drivers natively does not support DirectQuery access mode) and there I am required to provide ODBC connection string as one string.
I have formed (more or less intuitively) such string, e.g.:
User=SYSDBA;Password=masterkey;Database=D:\DB\ERP.FDB;DataSource=192.168.1.3;Port=3050;Dialect=3;Charset=UTF8;Role=;Connection lifetime=15;Pooling=true;MinPoolSize=0;MaxPoolSize=50;Packet Size=8192;ServerType=0;Initial Catalog=test;
But this string is rejected by Power BI Desktop with error message:
Unable to connect
We encountered an error while trying to connect.
Details: "ODBC: ERROR [08004] [ODBC Firebird Driver]connection lost to database
ERROR [01S00] [ODBC Firebird Driver]Invalid connection string attribute
ERROR [01S00] [ODBC Firebird Driver]Invalid connection string attribute"
My intention and wish is to grab already existing ODBC connection string that sits inside "ODBC Data Source Administrator (64-bit)" and use it for my connection, but the problem is that "ODBC Data Source Administrator (64-bit)" does not provide easily accessible way to get such fully formatted connection string from DSN entry.
So, my question is: how to read connection string from "ODBC Data Source Administrator (64-bit)" DSN entry. Specifically, how to do it from page that is used for Firebird ODBC driver.
You should read https://firebirdsql.org/file/documentation/html/en/refdocs/fbodbc20/firebird-odbc-driver-20-manual.html#fbodbc205-connection
If you already have configured DSN, you don't need anything else, just use "DSN=xxxx;".

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);
}

Connecting database to program in local network

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.

Establishing a MySQL connection without the MySql Connector for C++

How can I connect to a MySql server from my MFC C++ (MSVC2013) without the MySql Connector?
I think it seems to be a stupid question, but no, I haven't found anything about this on Google, and the Oracle connector is very buggy and is a pain...
You should be using CDatabase class to connect to your DB. Here is the example:
CDatabase cDb;
CString strConnectionString = _T("ODBC;DSN=DrumisD4WData;DRIVER={SQL Server};SERVER=DEV001-W2K8\\DRUMIS64;DATABASE=master");
TRY
{
cDb.OpenEx(strConnectionString, CDatabase::noOdbcDialog);
cDb.Close();
}
CATCH(CDBException, e)
{
AfxMessageBox(e->m_strError);
}
END_CATCH;

c++ mysql connection failure

I try to connect to my MySql database with c++ but it won't connect and I cant figure out why. The Instance of the MYSQL object was initialised and I also tried to connect to my database with Java and Php and it worked...
The Mysql server is not hosted by me, it's hosted by an German server provider so I suggest on the server side will be everything ok.
Also the libs and includes are linked correct since the initialisation of my MYSQL instance works fine.
The mysql version I use is 5.0.X
this is the line of code where I try to connect to the database:
MYSQL *connect = mysql_init(NULL);
mysql_real_connect(connect, "xxxxx.db.1and1.com", "dboXXXXXX", "XXXXXX", "dbXXXXXX", 0, NULL, 0);