Why am I not being able to connect to my database? - c++

I've followed numerous tutorials to the tee, yet I still cannot connect to my database from Qt. I always get this error:
Closed! "[Microsoft][ODBC SQL Server Driver][DBMSLPCN]SQL Server does not exist or
access denied. [Microsoft][ODBC SQL Server Driver][DBMSLPCN]ConnectionOpen
(Connect()). [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute
QODBC3: Unable to connect"
or if I use SQL NATIVE CLIENT (i.e. instead of SQL SERVER) as the DRIVER, I get the following error:
Closed! "[Microsoft][ODBC Driver Manager] Data source name not found and no default
driver specified QODBC3: Unable to connect"
Been sitting here for hours trying to figure this out, but I can't see what I'm doing wrong. The server, database, etc all seem to have the right names. I just don't see why it's not connecting.
Code below
#include "login.h"
#include "ui_login.h"
login::login(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::login)
{
ui->setupUi(this);
QSqlDatabase db = QSqlDatabase::addDatabase ("QODBC","Chessgames");
db.setConnectOptions();
db.setDatabaseName("DRIVER={SQL SERVER};SERVER=MYNAME\\SQLEXPRESS;DATABASE=Chessgames;Uid=sa;Password=xxxxx;");
if(db.open())
{
qDebug() << "Opened!";
db.close();
}
else
{
qDebug() << "Closed! " << db.lastError().text();
}
}
login::~login()
{
delete ui;
}

Got it to open by changing the hostname to the ip address and port.
db.setDatabaseName("DRIVER={SQL SERVER};SERVER=192.168.X.X;Port=port#;
DATABASE=Chessgames;Uid=sa;Password=xxxxx;");

Related

QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection'

i'm trying to connect my qt application to a Mysql database and don't know why it's showing the following error message : QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
can someone help me please, this is my code :
void MainWindow::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setUserName("root");
db.setPassword("");
db.setDatabaseName("pmkfinal");
if(db.open()){
QMessageBox::information(this,"Connection","Database Connected Successfully");
}else{
QMessageBox::information(this,"Connection","Database not Connected Successfully");
}
}
Thank you All !!
Do not connect to the database repeatedly. Or you can provide an alternate connection name to avoid the warning message.

QT Login Timeout expired QODBC3:Unable to connect ERROR

I am trying to connect my SQL Server to the qt project with this code
QString servername = "OLI-PC";
QString dbname = "Translator";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setConnectOptions();
qDebug()<<"Dupa connection details";
QString dsn = QString("DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes").arg(servername).arg(dbname);
db.setDatabaseName(dsn);
qDebug()<<"Dupa set data base";
if(db.open())
{
qDebug()<<"open";
}
else
{
qDebug()<<"Error = "<<db.lastError().text();
}
And it all worked as expected. I could open the database, read from it, perform certain queries, but not after I used a VPN, I can no longer connect to the database.
I have tried to disconnect from the VPN but with no success I still cannot connect.
The error code is:
One solution that worked for me was to change the driver to SQL Server Native Client 11.0:
DRIVER={SQL Server Native Client 11.0};
You can see the name of the drivers in ODBC Data Source Administrator.
check this post: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

How to connect Qt Desktop application to mysql database running on AWS or any online mysql database

I am trying to connect a Qt desktop application to mySql db on AWS or online mysql database hosted on free domain. can somebody point me in the correct direction for the same?
I tried creating account on AWS and create mysql db but connection does not work
db = QSqlDatabase::addDatabase("QODBC");
userName = "sql9298899";
password = "**********";
databaseName = "sql9298899";
hostName = "sql9.freesqldatabase.com";
//create database connection here
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(hostName);
db.setUserName(userName);
db.setPassword(password);
db.setDatabaseName(databaseName);
DB.open always returns false with lastError as :
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect

Qt ODBC Microsot SQL Server 2014 issue which happens once a month

I have developed for windows server 2016, a server application written in C++ with Qt/ODBC/MS Sql Server 2014 which works well. Users log into the application with their login/password. The server application make requests to MS SQL server thanks to ODBC driver.
But about once a month, the login functionnality stops to work and it seems that the application cannot access to the database anymore.
The login process uses this peace of code :
SQLServerConnexion::SQLServerConnexion(const std::string &dbname)
{
dbname_ = QString(dbname.c_str());
database_ = QSqlDatabase::addDatabase("QODBC", generatedUUID().c_str());
database_.setDatabaseName(dbname_);
if (database_.open()) {
qDebug() << dbname_ << "opened";
}
else {
qDebug() << "Cannot open " << dbname_;
}
}
dbname follows the following pattern "DB_NAME; MARS_Connection=YES;"
When I restart the application, users can log into the application.
I don't understand why this issue happens about once a month. Have you got some ideas to solve that issue ?

How to connect to Alexa with Qt?

I'm writing an Qt application and try to use Alexa API. I received access token, but I can't use API because of "Host not found" and "Connection closed".
My QNetworkAccessManager defined as
amazonHelper.data()->setNetworkAccessManager(view.data()->engine()->networkAccessManager());
...
void AmazonHelper::setNetworkAccessManager(QNetworkAccessManager *qnam) {
qDebug() << "setNetworkAccessManager()";
_manager = qnam;
connect(_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
}
After executing
_manager->connectToHostEncrypted("https://avs-alexa-na.amazon.com");
I receive an error "Host not found".
After executing
QNetworkRequest request(QUrl("https://avs-alexa-na.amazon.com/v20160207/directives"));
request.setRawHeader("Authorization", "Bearer %1" + _accessToken.toUtf8());
_manager->get(request);
I receive an error "Connection closed".
What is a right way to use Amazon Alexa API?
Thanks in advance!
UPD1:
I found QNetworkRequest::SpdyAllowedAttribute in Qt documentation but when I tried to set this attribute I got the follow error: 'SpdyAllowedAttribute' is not a member of 'QNetworkRequest'
UPD2:
I tried to use libcurlcpp but after setting CURLOPT_HTTP_VERSION to CURL_HTTP_VERSION_2_0 got exception (https://github.com/JosephP91/curlcpp/issues/84)