QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection' - c++

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.

Related

connect laravel to AWS database and insert data (Connections error)

I search everywhere on internet but could not any tutorial regarding this, How can I connect I connect to AWS RDS database and RDS replica in laravel 8 and also want to know configurations.
.env file
DB_CONNECTION=mysql
DB_HOST=********.********.us-east-1.rds.amazonaws.com
DB_PORT=3306
DB_DATABASE=********
DB_USERNAME=********
DB_PASSWORD=********
got these error
SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
(SQL: select * from information_schema.tables where table_schema = ******* and table_name = migrations and table_type = 'BASE TABLE')
at C:\Users\DELL\kenduit\vendor\laravel\framework\src\Illuminate\Database\Connection.php:712
708▕ // If an exception occurs when attempting to run a query, we'll format the error
709▕ // message to include the bindings with SQL, which will make this exception a
710▕ // lot more helpful to the developer instead of just the database's errors.
711▕ catch (Exception $e) {
➜ 712▕ throw new QueryException(
713▕ $query, $this->prepareBindings($bindings), $e
714▕ );
715▕ }
716▕ }
1 C:\Users\DELL\kenduit\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDOException::("SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
")
2 C:\Users\DELL\kenduit\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDO::__construct("mysql:host=*******.czdmmigxjpgh.us-east-1.rds.amazonaws.com;port=3306;dbname=********", "******", "******", [])

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

Why am I not being able to connect to my database?

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

Calling QSslSocket::startServerEncryption, but nothing happens

I'm trying to implement an SSL server using the sample code from Qt documentation.
But after serverSocket->startServerEncryption(); is called, nothing happens - neither the encrypted() nor the sslErrors() signals are emitted (I've put breakpoints in the slots connected to them).
I test it by connecting an QSslSocket using connectToHostEncrypted to the port I'm listening on. The socket sends data, but my server does not respond (I'm using a TCP sniffer/proxy to see all the data being sent from client to server and from server to client).
This is my code for the server:
void SslServer::incomingConnection(int socketDescriptor)
{
qDebug() << "SslServer::incomingConnection()";
QSslSocket *serverSocket = new QSslSocket(this);
if (serverSocket->setSocketDescriptor(socketDescriptor)) {
connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready()));
connect(serverSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>)));
serverSocket->startServerEncryption();
} else {
delete serverSocket;
}
}
And this is how I connect to it:
server = new SslServer(this);
server->listen(QHostAddress::Any, 3333);
QSslSocket *socket = new QSslSocket(this);
socket->connectToHostEncrypted("127.0.0.1", 3333);
According to the documentation:
Both the key and the local certificate are required if you are creating an SSL server socket.
And if you don't provide them, a "regular" error(QAbstractSocket::SocketError) signal is emitted by the socket. As you found out, the server doesn't send any data in that case.
SSH is not SSL. SSH client waits for initial data from server, while SSL client first sends data. So they both waiting for data from the other side.