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. What is the problem in QSqlQuery?
QtWS25 Last Chance

What is the problem in QSqlQuery?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsqlqueryqsqldatabaseqtsql
11 Posts 3 Posters 3.5k 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
    mrjj
    Lifetime Qt Champion
    wrote on 24 Aug 2016, 12:01 last edited by mrjj
    #2

    hi
    Well, it wants a database open first :)
    You seems not to have any?

    http://stackoverflow.com/questions/7669987/what-is-the-correct-way-of-qsqldatabase-qsqlquery

    _ 1 Reply Last reply 24 Aug 2016, 12:06
    0
    • M mrjj
      24 Aug 2016, 12:01

      hi
      Well, it wants a database open first :)
      You seems not to have any?

      http://stackoverflow.com/questions/7669987/what-is-the-correct-way-of-qsqldatabase-qsqlquery

      _ Offline
      _ Offline
      _compiler
      wrote on 24 Aug 2016, 12:06 last edited by
      #3

      @mrjj I do not need connection now. this is just a class. Does this class can not be used without connecting?

      R M 2 Replies Last reply 24 Aug 2016, 12:08
      0
      • _ _compiler
        24 Aug 2016, 12:06

        @mrjj I do not need connection now. this is just a class. Does this class can not be used without connecting?

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 24 Aug 2016, 12:08 last edited by
        #4

        @_compiler
        as the Qt docs for QSqlQuery class say:

        Warning: You must load the SQL driver and open the connection before a QSqlQuery is created. Also, the connection must remain open while the query exists; otherwise, the behavior of QSqlQuery is undefined.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        1
        • _ _compiler
          24 Aug 2016, 12:06

          @mrjj I do not need connection now. this is just a class. Does this class can not be used without connecting?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 24 Aug 2016, 12:09 last edited by
          #5

          @_compiler said:
          Well your class can , but when you do

          • qry.prepare(query);

          it wants to talk to DB to check stuff.

          1 Reply Last reply
          0
          • _ Offline
            _ Offline
            _compiler
            wrote on 24 Aug 2016, 12:10 last edited by
            #6

            real class ...

            class QueryBuilder
            {
            public:
                QueryBuilder();
            
                QSqlQuery insert(const QString &tableName, QMap<QString,QVariant>/*name,value*/ &values)
                {
                        QSqlQuery qry;
            
                        QString str = "INSERT INTO " + tableName +" (";
                        QString val = "VALUES(";
            
                        QMapIterator<QString, QVariant> it(values);
                        while (it.hasNext()) {
                            it.next();
            
                            str += it.key();
                            val += ":" + it.key();
            
                            if(it.hasNext())
                            {
                                str += ", ";
                                val += ", ";
                            }else
                            {
                                str += ") ";
                                val += ") ";
                            }
                        }
            
                        str += val;
            
                        qry.prepare(str);
            
                        ///...
            
                        return qry;
                }
            }
            
            M 1 Reply Last reply 24 Aug 2016, 12:12
            0
            • _ _compiler
              24 Aug 2016, 12:10

              real class ...

              class QueryBuilder
              {
              public:
                  QueryBuilder();
              
                  QSqlQuery insert(const QString &tableName, QMap<QString,QVariant>/*name,value*/ &values)
                  {
                          QSqlQuery qry;
              
                          QString str = "INSERT INTO " + tableName +" (";
                          QString val = "VALUES(";
              
                          QMapIterator<QString, QVariant> it(values);
                          while (it.hasNext()) {
                              it.next();
              
                              str += it.key();
                              val += ":" + it.key();
              
                              if(it.hasNext())
                              {
                                  str += ", ";
                                  val += ", ";
                              }else
                              {
                                  str += ") ";
                                  val += ") ";
                              }
                          }
              
                          str += val;
              
                          qry.prepare(str);
              
                          ///...
              
                          return qry;
                  }
              }
              
              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 24 Aug 2016, 12:12 last edited by
              #7

              @_compiler
              ok, but prepare need open database.

              _ 1 Reply Last reply 24 Aug 2016, 12:14
              0
              • M mrjj
                24 Aug 2016, 12:12

                @_compiler
                ok, but prepare need open database.

                _ Offline
                _ Offline
                _compiler
                wrote on 24 Aug 2016, 12:14 last edited by
                #8

                @mrjj i got it . How else can I do ?

                M 1 Reply Last reply 24 Aug 2016, 12:18
                0
                • _ _compiler
                  24 Aug 2016, 12:14

                  @mrjj i got it . How else can I do ?

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 24 Aug 2016, 12:18 last edited by mrjj
                  #9

                  well just wait until later to call qry.prepare(str); ?
                  The rest of the code just create the string.

                  but please notice what @raven-worx says. !

                  So maybe you should rethink the design of QueryBuilder to not use
                  QSqlQuery before a database is created and open.

                  what is the role of QueryBuilder ?

                  _ 1 Reply Last reply 24 Aug 2016, 12:27
                  0
                  • M mrjj
                    24 Aug 2016, 12:18

                    well just wait until later to call qry.prepare(str); ?
                    The rest of the code just create the string.

                    but please notice what @raven-worx says. !

                    So maybe you should rethink the design of QueryBuilder to not use
                    QSqlQuery before a database is created and open.

                    what is the role of QueryBuilder ?

                    _ Offline
                    _ Offline
                    _compiler
                    wrote on 24 Aug 2016, 12:27 last edited by
                    #10

                    @mrjj QueryBuilder class set of query.

                    for example ;

                    QueryBuilder b;
                    
                    QSqlQuery q1, q2, q3, q4;
                    
                    q1 = b.insert("bla bla ...");
                    q2 = b.update("bla bla ...");
                    q3 = b.remove("bla bla ...");
                    q4 = b.select("bla bla ...");
                    
                    SqlWorkerThread *thread = new SqlWorkerThread(0);
                    
                    //signal slot definitions ....
                    
                    thread.addQuery(q1);
                    thread.addQuery(q2);
                    thread.addQuery(q3);
                    thread.addQuery(q4);
                    
                    thread.start();
                    
                    M 1 Reply Last reply 24 Aug 2016, 12:34
                    0
                    • _ _compiler
                      24 Aug 2016, 12:27

                      @mrjj QueryBuilder class set of query.

                      for example ;

                      QueryBuilder b;
                      
                      QSqlQuery q1, q2, q3, q4;
                      
                      q1 = b.insert("bla bla ...");
                      q2 = b.update("bla bla ...");
                      q3 = b.remove("bla bla ...");
                      q4 = b.select("bla bla ...");
                      
                      SqlWorkerThread *thread = new SqlWorkerThread(0);
                      
                      //signal slot definitions ....
                      
                      thread.addQuery(q1);
                      thread.addQuery(q2);
                      thread.addQuery(q3);
                      thread.addQuery(q4);
                      
                      thread.start();
                      
                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 24 Aug 2016, 12:34 last edited by
                      #11

                      @_compiler

                      Ok, i see, its a helper class.

                      Well you need to open a db then.

                      You dont need to do it in QueryBuilder.

                      If you open a db in main , QSqlQuery
                      will use this DB. You dont need pointer or reference.
                      Its handled internally.

                      1 Reply Last reply
                      0

                      11/11

                      24 Aug 2016, 12:34

                      • Login

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