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. Could not insert data into Sqlite database
Forum Updated to NodeBB v4.3 + New Features

Could not insert data into Sqlite database

Scheduled Pinned Locked Moved Unsolved General and Desktop
38 Posts 5 Posters 10.3k Views 2 Watching
  • 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.
  • mrjjM mrjj

    Hi
    setCellWidget can be heavy.
    But you only show 5 row here.
    How many do you have?

    Is it the on_actionAdd_train_triggered that takes time?

    M Offline
    M Offline
    mark_ua_1999
    wrote on last edited by
    #23

    @mrjj When I click ok in Dialog window it takes a lot of time to add new row in mainwindow have no ideas why

    mrjjM 1 Reply Last reply
    0
    • M mark_ua_1999

      @mrjj When I click ok in Dialog window it takes a lot of time to add new row in mainwindow have no ideas why

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

      @mark_ua_1999
      well you loop a file in on_actionAdd_train_triggered but do nothing with what it reads.
      I do not think reading from database is the slow part and setCellWidget x5 should be that heavy unless its
      a Pi board or slower pc.

      M 1 Reply Last reply
      0
      • mrjjM mrjj

        @mark_ua_1999
        well you loop a file in on_actionAdd_train_triggered but do nothing with what it reads.
        I do not think reading from database is the slow part and setCellWidget x5 should be that heavy unless its
        a Pi board or slower pc.

        M Offline
        M Offline
        mark_ua_1999
        wrote on last edited by
        #25

        @mrjj I will be trying to show visual result adding the new train firstly and only than write to db it must help

        mrjjM 1 Reply Last reply
        0
        • M mark_ua_1999

          @mrjj I will be trying to show visual result adding the new train firstly and only than write to db it must help

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

          @mark_ua_1999
          Sounds like a good plan. IF you only show 5 at a time, its odd it should be slow.

          M 1 Reply Last reply
          0
          • mrjjM mrjj

            @mark_ua_1999
            Sounds like a good plan. IF you only show 5 at a time, its odd it should be slow.

            M Offline
            M Offline
            mark_ua_1999
            wrote on last edited by
            #27

            @mrjj I am trying to create and insert information in db in mainwindow the db is opened
            but the program crashes after if(query.exec("CREATE TABLE "+e.train_name+" "
            "(id integer primary key)" )) what could be a matter
            void MainWindow::on_actionAdd_train_triggered()
            {
            Dialog e;
            if(e.exec())
            {
            if(e.duplicate == "")
            {
            Add(e.Cities);

                QSqlQuery query;
                 //таблиця міст через які їхатиме поїзд
                // train_name = s;
                 QSqlDatabase db_ = QSqlDatabase::addDatabase("QSQLITE");
                db_.setDatabaseName("C:\\SQLiteDatabaseBrowserPortable\\Data\\cities.db");
                 db_.open();
                 if(db_.isOpen())
                 {
                     ui->lineEdit->setText("oPEN " + e.train_name);
                 }
            
                 if(query.exec("CREATE TABLE "+e.train_name+" "
                                                             "(id integer primary key)"
            
                                                             ))       
                         ui->lineEdit->setText("Created");
                 db_.close();
            
            mrjjM 1 Reply Last reply
            0
            • M mark_ua_1999

              @mrjj I am trying to create and insert information in db in mainwindow the db is opened
              but the program crashes after if(query.exec("CREATE TABLE "+e.train_name+" "
              "(id integer primary key)" )) what could be a matter
              void MainWindow::on_actionAdd_train_triggered()
              {
              Dialog e;
              if(e.exec())
              {
              if(e.duplicate == "")
              {
              Add(e.Cities);

                  QSqlQuery query;
                   //таблиця міст через які їхатиме поїзд
                  // train_name = s;
                   QSqlDatabase db_ = QSqlDatabase::addDatabase("QSQLITE");
                  db_.setDatabaseName("C:\\SQLiteDatabaseBrowserPortable\\Data\\cities.db");
                   db_.open();
                   if(db_.isOpen())
                   {
                       ui->lineEdit->setText("oPEN " + e.train_name);
                   }
              
                   if(query.exec("CREATE TABLE "+e.train_name+" "
                                                               "(id integer primary key)"
              
                                                               ))       
                           ui->lineEdit->setText("Created");
                   db_.close();
              
              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #28

              @mark_ua_1999
              The easiest way to find crash is to use the debugger.
              Place a break point and single step the code.
              Then you can see what makes it crash.

              M 1 Reply Last reply
              0
              • mrjjM mrjj

                @mark_ua_1999
                The easiest way to find crash is to use the debugger.
                Place a break point and single step the code.
                Then you can see what makes it crash.

                M Offline
                M Offline
                mark_ua_1999
                wrote on last edited by mark_ua_1999
                #29

                @mrjj it makes the crash if(query.exec("CREATE TABLE "+e.train_name+" "
                "(id integer primary key)"
                )) but why?

                mrjjM 1 Reply Last reply
                0
                • M mark_ua_1999

                  @mrjj it makes the crash if(query.exec("CREATE TABLE "+e.train_name+" "
                  "(id integer primary key)"
                  )) but why?

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

                  @mark_ua_1999
                  That i cant guess. see nothing that should be able to crash.
                  must be inside query.exec() then?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #31

                    Is it me or are you re-creating a new database connection each time you add a train ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 1 Reply Last reply
                    3
                    • SGaistS SGaist

                      Is it me or are you re-creating a new database connection each time you add a train ?

                      M Offline
                      M Offline
                      mark_ua_1999
                      wrote on last edited by
                      #32

                      @SGaist yes I am recreating db connection every time

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #33

                        Why do you do that ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        M 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Why do you do that ?

                          M Offline
                          M Offline
                          mark_ua_1999
                          wrote on last edited by
                          #34

                          @SGaist I habe no others ideas and what is your idea how to do it to work good

                          JonBJ 1 Reply Last reply
                          0
                          • M mark_ua_1999

                            @SGaist I habe no others ideas and what is your idea how to do it to work good

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #35

                            @mark_ua_1999
                            You do not want to open & close database on every event (incredibly slow)!! In some shape or form, you want to:

                            1. Open the database once on, say, application start up.
                            2. Store the connection in a "global" or at least "persistent" variable in a class outside of any event function.
                            3. Access the open connection throughout your application events etc.
                            4. Close the database connection prior to application exit.

                            And equally you don't want to be going CREATE TABLE all the time, you create the table once outside of events (e.g. on database open connection if necessary) and then assume it's always present (unless you have some very unusual requirement).

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #36

                              @JNBarchan Number 2 is wrong.

                              There's absolutely no need to store the connection in any local or global variable. QSqlDatabase already does everything you need in that aspect. It's explained in the detailed documentation of the class.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              JonBJ 1 Reply Last reply
                              1
                              • SGaistS SGaist

                                @JNBarchan Number 2 is wrong.

                                There's absolutely no need to store the connection in any local or global variable. QSqlDatabase already does everything you need in that aspect. It's explained in the detailed documentation of the class.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #37

                                @SGaist
                                I don't understand. If you have multiple instances (e.g. so you can't use "default"), Qt wants us to later access these via the optional connectionName parameter, rather than keeping the return result from addDatabase() in scope, and closing & freeing on exit? OK, that's not a pattern I'm familiar with from elsewhere.

                                In the (large) codebase I have inherited, which is my exposure to Qt, the persistent QMainWindow class (and/or a global singleton class) holds a created connection instance (this is done via Python 3.5 mysql module, if that matters) in a member variable, and passes it around explicitly to other dialogs etc....

                                If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.

                                Hmm, I don't see any explicit deletion, or even closure. I guess Python will do that automatically on application exit (though thinking aloud I don't know when that happens vis-a-vis "QCoreApplication instance is deleted").... Should I revisit existing code architecture in this light, or maybe this is a common Python approach, I don't know.

                                EDIT:
                                Blimey! You have made me look at the existing code. So far as I can see, there are two database connections going on! One is via this Python mysql module, and another is via QSqlDatabase (which is still stored in a persistent variable), which I guess are two quite distinct things... :(

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #38

                                  If you have multiple connections, keep the connection names then use QSqlDatabase::database to retrieve the QSqlDatabase object of interest when needed.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  3

                                  • Login

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