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?

What is the problem in QSqlQuery?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsqlqueryqsqldatabaseqtsql
11 Posts 3 Posters 4.4k 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 Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on 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
    0
    • mrjjM mrjj

      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 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?

      raven-worxR mrjjM 2 Replies Last reply
      0
      • _ _compiler

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

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on 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

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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 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;
                }
            }
            
            mrjjM 1 Reply Last reply
            0
            • _ _compiler

              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;
                  }
              }
              
              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @_compiler
              ok, but prepare need open database.

              _ 1 Reply Last reply
              0
              • mrjjM mrjj

                @_compiler
                ok, but prepare need open database.

                _ Offline
                _ Offline
                _compiler
                wrote on last edited by
                #8

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

                mrjjM 1 Reply Last reply
                0
                • _ _compiler

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • mrjjM mrjj

                    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 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();
                    
                    mrjjM 1 Reply Last reply
                    0
                    • _ _compiler

                      @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();
                      
                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 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

                      • Login

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