Execute sql queries on multiple databases
I want execute queries on my main and backup database, how do i do it? I currently have two connections running until I go to execute a query, I get the error database not open. I'm open to suggestions outside of this program to maintaining an efficient contemporaneous backup system.
My code:
Code:
{
ui->setupUi(this);
//Setup DB connection
db2.setDatabaseName(Path_to_DB);
if(checkFile.isWritable())
{
if(db2.open())
{
qDebug() << "Connected to database file";
}
}else{
qDebug() << "Database file not found";
}
dbb.setDatabaseName(Path_to_DBBackup);
if(checkFile2.isWritable())
{
if(dbb.open())
{
qDebug() << "Connected to database backup file";
}
}else{
qDebug() << "Database file not found";
}
QString q
= QString("SELECT * FROM users WHERE email = '%1'").
arg(email
);
if(qry.exec(q))
{
if (qry.next())
{
return false;
}
}
else{qDebug() << "Query unsuccessful!";}
}
Everything works when I query just one db...
Thanks in advance.
Re: Execute sql queries on multiple databases
You are creating a QSqlQuery [line 34] operating on the default connection, which is neither of the two connections your code opens.
Re: Execute sql queries on multiple databases
Can you show me how to query multiple databases in qt?
Re: Execute sql queries on multiple databases
Defining QSqlQuery you specify the database to which it relates, eg
Re: Execute sql queries on multiple databases
This thread is solved now thanks.