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);
}
Related
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 ?
I'm writing a program in Qt that connects to a local database and writes to it using QSqlDatabase, but I've been unable to get a working connection between my program and the server.
qdb = QSqlDatabase::addDatabase("QODBC");
qdb.setConnectOptions();
QString serverName = "KIRKWOOD\TESTCLOUD";
QString ipName = "tcp:10.150.94.197,1433";
QString dbName = "Puma";
QString connectionString = QString("DRIVER={ODBC Driver 17 for SQL Server};Server=%1;Database=%2;Trusted_Connection=Yes;").arg(ipName).arg(dbName);
qdb.setDatabaseName(connectionString);
bool connectionSuccessful = qdb.open();
if (connectionSuccessful) {
writeLogWithNewLine("Connected to database.");
}
else {
writeLog("Failed to connect to database.");
writeLogWithNewLine(qdb.lastError().text());
}
When I run this code I get the following error in my log:
"Failed to connect to database.
[Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it. [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired QODBC3: Unable to connect"
When I replace ipName with serverName I get this error:
"Failed to connect to database.
[Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired QODBC3: Unable to connect"
When I change the driver to "SQL Server" I get this error:
"Failed to connect to database.
[Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). QODBC3: Unable to connect"
I've done a lot of research and made a lot of changes to my code, from changing the Driver type to changing the formatting of "Trusted_Connection=Yes" to "Integrated Security=True" but I've had no luck getting anything much different from these errors.
This is frustrating because in this same program I was able to connect to a dummy database using SQL authentication perfectly fine, and on top of that, using this same machine I'm able to connect to the database through Visual Studio's Server Explorer. I've even directly copy pasted the connection string from visual studio but still got nothing. Is there something else I need to do here?
I would greatly appreciate feedback.
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.
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:
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);