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?



  • What is the Error? Maybe QDebug the Error from daba2.lastError().text()



  • it does not show any error, just I trace it with qDebug and I find the daba2 has error, and my app (embedded app) is suddenly close. and the error is not show.
    Is that code is correct?



  • So the Connection does not open or the Query is not executed? So you mean that your Method, in your case, returns false?



  • I think daba2 can not connect with another database.yes when i click my button to call connect method to second database, my app is crashed(my app run on embedded device) and my program is closed. (also try catch is not work to find the error text)



  • @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)



  • in this code:

    daba2.setDatabaseName(dabkup)
    

    dabkup is my input method parameter and it is my backup database path and name



  • @MhM93 But @Fuel is right - the code you posted has an error: you add second database but just afterwards change database name on the first one, not setting name on the second. And then you try to open it.



  • 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


  • Qt Champions 2016

    @MhM93 said in My second connection in sqlite is not work.:

    hi
    As @artwaw says

    
     bool 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;
            }
    

  • Qt Champions 2016

    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
    

  • Qt Champions 2016

    @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)


  • Qt Champions 2016

    @MhM93

    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 pointer

    DB *My1; // just pointer. Points to nothing valid yet

    My1= new DB; // now it points to a real object



  • thank you :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.