Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. My second connection in sqlite is not work.

My second connection in sqlite is not work.

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MhM93M Offline
    MhM93M Offline
    MhM93
    wrote on last edited by MhM93
    #1

    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?

    H.Ghassami

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Fuel
      wrote on last edited by
      #2

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

      1 Reply Last reply
      2
      • MhM93M Offline
        MhM93M Offline
        MhM93
        wrote on last edited by
        #3

        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?

        H.Ghassami

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Fuel
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • MhM93M Offline
            MhM93M Offline
            MhM93
            wrote on last edited by
            #5

            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)

            H.Ghassami

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Fuel
              wrote on last edited by
              #6

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

              1 Reply Last reply
              3
              • MhM93M Offline
                MhM93M Offline
                MhM93
                wrote on last edited by
                #7

                in this code:

                daba2.setDatabaseName(dabkup)
                

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

                H.Ghassami

                artwawA 1 Reply Last reply
                0
                • MhM93M MhM93

                  in this code:

                  daba2.setDatabaseName(dabkup)
                  

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

                  artwawA Offline
                  artwawA Offline
                  artwaw
                  wrote on last edited by
                  #8

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

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  1 Reply Last reply
                  3
                  • MhM93M Offline
                    MhM93M Offline
                    MhM93
                    wrote on last edited by
                    #9

                    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

                    H.Ghassami

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @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
                      
                      1 Reply Last reply
                      0
                      • MhM93M Offline
                        MhM93M Offline
                        MhM93
                        wrote on last edited by MhM93
                        #11

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

                        H.Ghassami

                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          That is odd if it crashed there.
                          Do you allocate DB before you call MergeData ?
                          Please Show calling code.

                          1 Reply Last reply
                          0
                          • MhM93M Offline
                            MhM93M Offline
                            MhM93
                            wrote on last edited by
                            #13
                            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
                            

                            H.Ghassami

                            mrjjM 1 Reply Last reply
                            0
                            • MhM93M MhM93
                              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
                              
                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

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

                              1 Reply Last reply
                              2
                              • MhM93M Offline
                                MhM93M Offline
                                MhM93
                                wrote on last edited by
                                #15

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

                                H.Ghassami

                                mrjjM 1 Reply Last reply
                                0
                                • MhM93M MhM93

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

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @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

                                  1 Reply Last reply
                                  2
                                  • MhM93M Offline
                                    MhM93M Offline
                                    MhM93
                                    wrote on last edited by
                                    #17

                                    thank you :)

                                    H.Ghassami

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved