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. sqlite override data

sqlite override data

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 1.3k 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.
  • M Offline
    M Offline
    Mogli123
    wrote on last edited by Mogli123
    #1

    Hi,

    I'm very new in qt and c++ and have the following problem.
    I have a database and want to refresh all 5 minutes the data.
    The database is displayed by a tableview.
    I tried it like this, but it doesn't work.

    .cpp

      db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(":memory:");
        if (!db.open())
        {
            QMessageBox::critical(nullptr, QObject::tr("Cannot open database"),
                QObject::tr("Unable to establish a database connection.\n"
                            "This example needs SQLite support. Please read "
                            "the Qt SQL driver documentation for information how "
                            "to build it.\n\n"
                            "Click Cancel to exit."), QMessageBox::Cancel);
            return;
        }
    
     query = new QSqlQuery;
        query->exec("CREATE TABLE CustomiseToolChange (COLOR varchar(20),"
                   "TOOLNR, TIME int, POCKETSTATUS varchar(20))");
        query->prepare("INSERT INTO CustomiseToolChange (COLOR, TOOLNR, TIME, POCKETSTATUS) "
                       "VALUES (:COLOR, :TOOLNR, :TIME, :POCKETSTATUS)");
        query->bindValue(":COLOR", "");
    
        timer = new QTimer(this);
        timer->setInterval(5000);
        timer->start();
    
        connect(timer, SIGNAL(timeout()), this, SLOT(fillDatabaseTableCustomiseToolChange_1()));
    
    ///////////////////////////////////////
    //method:
    ///////////////////////////////////////
    
    void detailScreen::fillDatabaseTableCustomiseToolChange_1()
    {
        qint32 testwerteFuerArray_1[30];
        int lengthTableCustomiseToolChange = 30;
        for (int i = 0; i < lengthTableCustomiseToolChange; i++)
        {
            int randInt = rand() % (1010000 - 0) + 1000000;
            testwerteFuerArray_1[i] = randInt;
    
        }
    
        query->first();
    
        for (int i = 0; i < 10; i++)
        {
            query->bindValue(":TOOLNR", testwerteFuerArray_1[i * 3 + 0]);
            query->bindValue(":TIME", testwerteFuerArray_1[i * 3 + 1]);
            query->bindValue(":POCKETSTATUS", testwerteFuerArray_1[i * 3 + 2]);
            query->exec();
        }
     
        qDebug()<<"query->first() ="<<query->first();
    //return always "false"
    }
    
    

    Is that the right way?
    (I have to do it with a database, I can't insert it directly in the tableview)
    Why is query->first(); always false?
    Will my table be updated too, if that works?

    table.cpp

    
     myTableCustomiseToolChange = new TableCustomise(this); 
     myTableCustomiseToolChange->setTable("CustomiseToolChange");
     myTableCustomiseToolChange->select();
    
     myTableview = new QTableView(this);
    myTableview->setModel(myTableCustomiseToolChange);
    

    The following is only a understanding question:
    Why does the model in the file table.cpp know which database should be used?
    My modle get only the tablename. myTableCustomiseToolChange->setTable("CustomiseToolChange"); no database name.

    Sorry for the many questions.

    jsulmJ 2 Replies Last reply
    0
    • M Mogli123

      Hi,

      I'm very new in qt and c++ and have the following problem.
      I have a database and want to refresh all 5 minutes the data.
      The database is displayed by a tableview.
      I tried it like this, but it doesn't work.

      .cpp

        db = QSqlDatabase::addDatabase("QSQLITE");
          db.setDatabaseName(":memory:");
          if (!db.open())
          {
              QMessageBox::critical(nullptr, QObject::tr("Cannot open database"),
                  QObject::tr("Unable to establish a database connection.\n"
                              "This example needs SQLite support. Please read "
                              "the Qt SQL driver documentation for information how "
                              "to build it.\n\n"
                              "Click Cancel to exit."), QMessageBox::Cancel);
              return;
          }
      
       query = new QSqlQuery;
          query->exec("CREATE TABLE CustomiseToolChange (COLOR varchar(20),"
                     "TOOLNR, TIME int, POCKETSTATUS varchar(20))");
          query->prepare("INSERT INTO CustomiseToolChange (COLOR, TOOLNR, TIME, POCKETSTATUS) "
                         "VALUES (:COLOR, :TOOLNR, :TIME, :POCKETSTATUS)");
          query->bindValue(":COLOR", "");
      
          timer = new QTimer(this);
          timer->setInterval(5000);
          timer->start();
      
          connect(timer, SIGNAL(timeout()), this, SLOT(fillDatabaseTableCustomiseToolChange_1()));
      
      ///////////////////////////////////////
      //method:
      ///////////////////////////////////////
      
      void detailScreen::fillDatabaseTableCustomiseToolChange_1()
      {
          qint32 testwerteFuerArray_1[30];
          int lengthTableCustomiseToolChange = 30;
          for (int i = 0; i < lengthTableCustomiseToolChange; i++)
          {
              int randInt = rand() % (1010000 - 0) + 1000000;
              testwerteFuerArray_1[i] = randInt;
      
          }
      
          query->first();
      
          for (int i = 0; i < 10; i++)
          {
              query->bindValue(":TOOLNR", testwerteFuerArray_1[i * 3 + 0]);
              query->bindValue(":TIME", testwerteFuerArray_1[i * 3 + 1]);
              query->bindValue(":POCKETSTATUS", testwerteFuerArray_1[i * 3 + 2]);
              query->exec();
          }
       
          qDebug()<<"query->first() ="<<query->first();
      //return always "false"
      }
      
      

      Is that the right way?
      (I have to do it with a database, I can't insert it directly in the tableview)
      Why is query->first(); always false?
      Will my table be updated too, if that works?

      table.cpp

      
       myTableCustomiseToolChange = new TableCustomise(this); 
       myTableCustomiseToolChange->setTable("CustomiseToolChange");
       myTableCustomiseToolChange->select();
      
       myTableview = new QTableView(this);
      myTableview->setModel(myTableCustomiseToolChange);
      

      The following is only a understanding question:
      Why does the model in the file table.cpp know which database should be used?
      My modle get only the tablename. myTableCustomiseToolChange->setTable("CustomiseToolChange"); no database name.

      Sorry for the many questions.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • M Mogli123

        Hi,

        I'm very new in qt and c++ and have the following problem.
        I have a database and want to refresh all 5 minutes the data.
        The database is displayed by a tableview.
        I tried it like this, but it doesn't work.

        .cpp

          db = QSqlDatabase::addDatabase("QSQLITE");
            db.setDatabaseName(":memory:");
            if (!db.open())
            {
                QMessageBox::critical(nullptr, QObject::tr("Cannot open database"),
                    QObject::tr("Unable to establish a database connection.\n"
                                "This example needs SQLite support. Please read "
                                "the Qt SQL driver documentation for information how "
                                "to build it.\n\n"
                                "Click Cancel to exit."), QMessageBox::Cancel);
                return;
            }
        
         query = new QSqlQuery;
            query->exec("CREATE TABLE CustomiseToolChange (COLOR varchar(20),"
                       "TOOLNR, TIME int, POCKETSTATUS varchar(20))");
            query->prepare("INSERT INTO CustomiseToolChange (COLOR, TOOLNR, TIME, POCKETSTATUS) "
                           "VALUES (:COLOR, :TOOLNR, :TIME, :POCKETSTATUS)");
            query->bindValue(":COLOR", "");
        
            timer = new QTimer(this);
            timer->setInterval(5000);
            timer->start();
        
            connect(timer, SIGNAL(timeout()), this, SLOT(fillDatabaseTableCustomiseToolChange_1()));
        
        ///////////////////////////////////////
        //method:
        ///////////////////////////////////////
        
        void detailScreen::fillDatabaseTableCustomiseToolChange_1()
        {
            qint32 testwerteFuerArray_1[30];
            int lengthTableCustomiseToolChange = 30;
            for (int i = 0; i < lengthTableCustomiseToolChange; i++)
            {
                int randInt = rand() % (1010000 - 0) + 1000000;
                testwerteFuerArray_1[i] = randInt;
        
            }
        
            query->first();
        
            for (int i = 0; i < 10; i++)
            {
                query->bindValue(":TOOLNR", testwerteFuerArray_1[i * 3 + 0]);
                query->bindValue(":TIME", testwerteFuerArray_1[i * 3 + 1]);
                query->bindValue(":POCKETSTATUS", testwerteFuerArray_1[i * 3 + 2]);
                query->exec();
            }
         
            qDebug()<<"query->first() ="<<query->first();
        //return always "false"
        }
        
        

        Is that the right way?
        (I have to do it with a database, I can't insert it directly in the tableview)
        Why is query->first(); always false?
        Will my table be updated too, if that works?

        table.cpp

        
         myTableCustomiseToolChange = new TableCustomise(this); 
         myTableCustomiseToolChange->setTable("CustomiseToolChange");
         myTableCustomiseToolChange->select();
        
         myTableview = new QTableView(this);
        myTableview->setModel(myTableCustomiseToolChange);
        

        The following is only a understanding question:
        Why does the model in the file table.cpp know which database should be used?
        My modle get only the tablename. myTableCustomiseToolChange->setTable("CustomiseToolChange"); no database name.

        Sorry for the many questions.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #3

        @Mogli123 said in sqlite override data:

        Why is query->first(); always false?

        I guess because it is an insert query, not a select.
        You can use the SQLite command line tool to check whether data was inserted or not.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mogli123
          wrote on last edited by Mogli123
          #4

          @jsulm

          Thank you for your help.

          How can I make a select query, or is that nonsens in my case?

          With that tool I can see the database?
          I looked at the database like this to show a value from the table.
          (only the last row)

          int queryValue = query->boundValue(1).toInt();
          qDebug()<<"queryValue ="<<queryValue; 
          
          jsulmJ 1 Reply Last reply
          0
          • M Mogli123

            @jsulm

            Thank you for your help.

            How can I make a select query, or is that nonsens in my case?

            With that tool I can see the database?
            I looked at the database like this to show a value from the table.
            (only the last row)

            int queryValue = query->boundValue(1).toInt();
            qDebug()<<"queryValue ="<<queryValue; 
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Mogli123 said in sqlite override data:

            How can I make a select query,

            Like any other and like shown in the documentation http://doc.qt.io/qt-5/qsqlquery.html

            "With that tool I can see the database?" - yes:

            sqlite3 PATH_TO_YOUR_DB_FILE
            
            M 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Mogli123 said in sqlite override data:

              How can I make a select query,

              Like any other and like shown in the documentation http://doc.qt.io/qt-5/qsqlquery.html

              "With that tool I can see the database?" - yes:

              sqlite3 PATH_TO_YOUR_DB_FILE
              
              M Offline
              M Offline
              Mogli123
              wrote on last edited by
              #6

              @jsulm

              My database has no path.

              Is my query because of that line "insert"?

               query->prepare("INSERT INTO CustomiseToolChange (COLOR, TOOLNR, TIME, POCKETSTATUS) "
                                 "VALUES (:COLOR, :TOOLNR, :TIME, :POCKETSTATUS)");
              
              

              If I use SELECT instead of INSERT the table doesn't show the data any more.

              jsulmJ 1 Reply Last reply
              0
              • M Mogli123

                @jsulm

                My database has no path.

                Is my query because of that line "insert"?

                 query->prepare("INSERT INTO CustomiseToolChange (COLOR, TOOLNR, TIME, POCKETSTATUS) "
                                   "VALUES (:COLOR, :TOOLNR, :TIME, :POCKETSTATUS)");
                
                

                If I use SELECT instead of INSERT the table doesn't show the data any more.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Mogli123 said in sqlite override data:

                My database has no path.

                Do you use a in-memory database? If not then it has a path. A SQLite database is either in-memory or a file.

                "If I use SELECT instead of INSERT the table doesn't show the data any more." - I don't get it? You use INSERT to insert data and you use SELECT to query data (to check whether something was inserted or not in your case). Because if you call query.frist() after executing an INSERT query it will not return anything as there is nothing to return.

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  Mogli123
                  wrote on last edited by Mogli123
                  #8

                  I use a in-memory database.
                  Ah ok thanks. I think now I understand the meaning from select and insert.

                  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