Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Unsolved What is the problem in QSqlQuery?

    General and Desktop
    qsqlquery qsqldatabase qtsql
    3
    11
    2776
    Loading More Posts
    • 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.
    • _
      _compiler last edited by

      class QueryBuilder
      {
      public:
          QueryBuilder();
          QSqlQuery insert(const QString &query)
          {
              QSqlQuery qry;
              qry.prepare(query);
              
              return qry;
          }
      }
      
      #include "querybuilder.h"
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QueryBuilder b;
          b.insert("bla bla ...");  // ---> Output : QSqlQuery::prepare: database not open
      
          return a.exec();
      }
      
      

      can you help ?

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by 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

        _ 1 Reply Last reply Reply Quote 0
        • _
          _compiler @mrjj last edited by

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

          raven-worx mrjj 2 Replies Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @_compiler last edited by

            @_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 Reply Quote 1
            • mrjj
              mrjj Lifetime Qt Champion @_compiler last edited by

              @_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 Reply Quote 0
              • _
                _compiler last edited by

                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;
                    }
                }
                
                mrjj 1 Reply Last reply Reply Quote 0
                • mrjj
                  mrjj Lifetime Qt Champion @_compiler last edited by

                  @_compiler
                  ok, but prepare need open database.

                  _ 1 Reply Last reply Reply Quote 0
                  • _
                    _compiler @mrjj last edited by

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

                    mrjj 1 Reply Last reply Reply Quote 0
                    • mrjj
                      mrjj Lifetime Qt Champion @_compiler last edited by 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 ?

                      _ 1 Reply Last reply Reply Quote 0
                      • _
                        _compiler @mrjj last edited by

                        @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();
                        
                        mrjj 1 Reply Last reply Reply Quote 0
                        • mrjj
                          mrjj Lifetime Qt Champion @_compiler last edited by

                          @_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 Reply Quote 0
                          • First post
                            Last post