My second connection in sqlite is not work.
-
I have a main database that i created it as follow:
bool DB::openConnection() { daba = QSqlDatabase::addDatabase("QSQLITE","MainDB");//not dbConnection QString path = "IODB"; daba.setDatabaseName(path); if(!daba.open()) { //msg=("Error to open database"); return false; } else { msg=("The Database is open sucessfully"); createTables(); // qDebug()<<msg; DefaultFactory(); return true; } }
it works well untill I want to add another database(back up database) to merge data. this is that code:
bool DB::MergeData(QString dabkup) { try{ daba2 = QSqlDatabase::addDatabase("QSQLITE","BkupDB");//not dbConnection daba.setDatabaseName(dabkup); if(!daba2.open()) { qDebug("db backup can not open"); return false; } qDebug("db backup is open"); //----------------------------------------------------------- QSqlQuery q("select * from userinfo;",QSqlDatabase::database("BkupDB")); QSqlQueryModel *lst=new QSqlQueryModel(); lst->setQuery(q); int pid=0; int id=0; bool admin=false,active=false; //insert userinfo at first step for(int i = 0; i < lst->rowCount(); ++i) { pid=lst->record(i).value(1).toInt(); admin=lst->record(i).value(2).toInt(); active=lst->record(i).value(3).toInt(); q.prepare("select count(UI_PID) from userinfo where UI_PID=:pid;"); q.bindValue(0,pid); //insert into userinfo main database //from backup database qDebug()<<"pid:"<<pid<<" admin:"<<admin<<" active:"<<active<<" "; if(q.exec()) while(q.next()) { if(q.value(0).toInt()==0) { id=GetMaxUserID(1).toInt(); IE_userinfo(id,pid,admin,active); } } } MergeAuthenticate(); daba2.close(); }catch(...){ qDebug()<<daba2.lastError().text(); }
the second connection is not open, could any one help me?
-
-
@MhM93 said in My second connection in sqlite is not work.:
daba2 = QSqlDatabase::addDatabase("QSQLITE","BkupDB");//not dbConnection
daba.setDatabaseName(dabkup);I see here that you use the wrong Variable. you need to use daba2.setDatabaseName(dabkup)
-
thanks for both reply(@Fuel and @artwaw )
Sorry but I think I need more describe. could you please about it or write a little example code for me?
When I search I see these codes:
[https://forum.qt.io/topic/76810/use-two-or-more-sqlite-database-at-the-same-time/3]db= QSqlDatabase::addDatabase("QSQLITE", "first_connection"); fallas= QSqlDatabase::addDatabase("QSQLITE", "second_connection");
after define connection I set the database name with the database name and path.
please write an example for me. sorry -
@MhM93 said in My second connection in sqlite is not work.:
hi
As @artwaw saysbool DB::MergeData(QString dabkup) { try{ daba2 = QSqlDatabase::addDatabase("QSQLITE","BkupDB"); daba.setDatabaseName(dabkup); <<<<<<<<<<<< should be daba2
-
thanks for answer and excuse me for silly mistake, I change it but again my app crash at this line
(I use qt4.8.6)bool DB::MergeData(QString dabkup) { qDebug("db backup start"); try{ daba2 = QSqlDatabase::addDatabase("QSQLITE","BkupDB"); // = in this line }catch(...){ qDebug()<<daba2.lastError().text(); } qDebug("1"); daba2.setDatabaseName(dabkup); qDebug("2"); if(!daba2.open()) { qDebug("db backup can not open"); return false; }
-
That is odd if it crashed there.
Do you allocate DB before you call MergeData ?
Please Show calling code. -
class PenDrive : public QWidget { Q_OBJECT public: explicit PenDrive(QWidget *parent = 0,SerialPort *finger=0); ~PenDrive(); DB *database;
and:
void PenDrive::on_btnUploadUser_clicked() { md=new MessageDialog(this); if(!CheckUSB()) { md->error( tr("FAIL"), tr("Sorry.The USB is not detected at udisk0.")); return; } QFile::copy("/media/usb/IODBbkup", "IODBbkup"); qDebug("before call database"); if(database->MergeData("IODBbkup")){ md->success(tr("Step one"),tr("Successfully finished step one, \n Please wait to write data on sensor.")); fdb->WriteFinger(NULL); } else md->error(tr("Step one"),tr("Error occured in step one.")); }
and merg code:
bool DB::MergeData(QString dabkup) { try{ qDebug("db backup start"); daba2 = QSqlDatabase::addDatabase("QSQLITE","BkupDB");//not dbConnection qDebug("1"); daba2.setDatabaseName(dabkup); qDebug("2"); if(!daba2.open()) { qDebug("db backup can not open"); return false; } qDebug("db backup is open"); //----------------------------------------------------------- QSqlQuery q("select * from userinfo;",QSqlDatabase::database("BkupDB")); QSqlQueryModel *lst=new QSqlQueryModel(); lst->setQuery(q); int pid=0; int id=0; bool admin=false,active=false; //insert userinfo at first step for(int i = 0; i < lst->rowCount(); ++i) { pid=lst->record(i).value(1).toInt(); admin=lst->record(i).value(2).toInt(); active=lst->record(i).value(3).toInt(); q.prepare("select count(UI_PID) from userinfo where UI_PID=:pid;"); q.bindValue(0,pid); //insert into userinfo main database //from backup database qDebug()<<"pid:"<<pid<<" admin:"<<admin<<" active:"<<active<<" "; if(q.exec()) while(q.next()) { if(q.value(0).toInt()==0) { id=GetMaxUserID(1).toInt(); IE_userinfo(id,pid,admin,active); } } } MergeAuthenticate(); daba2.close(); }catch(...){ qDebug()<<daba2.lastError().text(); } qDebug()<<daba2.lastError().text(); return false; }
output is :
before call database db backup start
-
@MhM93 said in My second connection in sqlite is not work.:
DB *database;
Do you new this ?
DB *database = new DB;Else you crash if u call functions on it.
-
@mrjj said in My second connection in sqlite is not work.:
That is odd if it crashed there.
Do you allocate DB before you call MergeData ?
Please Show calling code.really thanks, again silly mistake. I add new and it is true. but in whole of my app does not use new and all of them worked.
could you explain it to me?(So sorry) -
Hi
If you declare something as pointer ( with *) you need to call new
to have it be an actual object. else it just points to random location in memory.So
DB My1; // fine. its actual object, not pointerDB *My1; // just pointer. Points to nothing valid yet
My1= new DB; // now it points to a real object