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. SQL Drivers and Qt on Windows - How to?
Forum Update on Monday, May 27th 2025

SQL Drivers and Qt on Windows - How to?

Scheduled Pinned Locked Moved General and Desktop
16 Posts 4 Posters 6.0k 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.
  • B Offline
    B Offline
    bu7ch3r
    wrote on last edited by
    #3

    I have the same problem. I am trying to include mysql driver into a static version of QT. I am building right now using this configuration:

    configure -release -nomake examples -nomake demos -no-exceptions -no-stl -no-rtti -no-qt3support -no-scripttools -no-openssl -no-opengl -no-webkit -no-phonon -no-style-motif -no-style-cde -no-style-cleanlooks -no-style-plastique -plugin-sql-mysql -I C:\mysql\include -L C:\mysql\lib -l libmysql -nomake tests

    Hope it shall work and help you with that.

    for(int i = 200; i > 0;)
    try
    {
    //do something
    }
    catch(...)
    {
    i--;//try again
    }

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bu7ch3r
      wrote on last edited by
      #4

      Ok after my last post 10secs ago I saw that I have another eror while compiling:

      I shall rebuild again using _CRT_SECURE_NO_WARNINGS becaus in some cpp there is strcpy wich nmake consider deprecated and unsafe... I shall come back with more info after new build.

      for(int i = 200; i > 0;)
      try
      {
      //do something
      }
      catch(...)
      {
      i--;//try again
      }

      1 Reply Last reply
      0
      • V Offline
        V Offline
        veeeee_d
        wrote on last edited by
        #5

        I have simply linked SQL and trying to use it, I'll answer in a minute.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bu7ch3r
          wrote on last edited by
          #6

          In your pro you should have CONFIG += sql. Basicaly you can use only sqllite or odbc(if somehow was compiled). But other sql drivers....no:)

          for(int i = 200; i > 0;)
          try
          {
          //do something
          }
          catch(...)
          {
          i--;//try again
          }

          1 Reply Last reply
          0
          • V Offline
            V Offline
            veeeee_d
            wrote on last edited by
            #7

            SQLite is what I want, and it works perfectly now.
            As I said, it was either pretty simple or I was just stupid... Well, guess I got both, haha.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bu7ch3r
              wrote on last edited by
              #8

              I want more than SQLite. It works ok for me too...but not mysql

              for(int i = 200; i > 0;)
              try
              {
              //do something
              }
              catch(...)
              {
              i--;//try again
              }

              1 Reply Last reply
              0
              • V Offline
                V Offline
                veeeee_d
                wrote on last edited by
                #9

                Have you tried building them like in the link mentioned above?

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  bu7ch3r
                  wrote on last edited by
                  #10

                  I have tried building them in a lot of variants:) I believe that I know all configure flags right now:P

                  If I use -qt-sql-mysq or -plugin-sql-mysql I get unresolved externals or already defined symbol pointing to msvcrt or libcmt.lib or something regarding. I have also tried to link using both mysqlbinaries (dll+lib and just lib) but with no use.
                  Another problem is that mysql binaries ar /MT while QT is /MD.
                  I think there is nothing on the web that tells you exactly how to built it static.
                  Qt also doesn't offer very good documentation about building, it is outdated or it just for deploying everything in dll's. Deploying and using all plugins with dll's indeed it's not complicated, pain overcomes when you must put everything in one .exe.

                  for(int i = 200; i > 0;)
                  try
                  {
                  //do something
                  }
                  catch(...)
                  {
                  i--;//try again
                  }

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    veeeee_d
                    wrote on last edited by
                    #11

                    Alright, guys, I had to revive this thread.
                    All of a sudden, the database seems not to be working properly.

                    This is the code I have now:

                    @ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                    db.setDatabaseName("banana_test");
                    qDebug() << db.open();
                    QSqlQuery query("CREATE TABLE banana ( id TEXT );");
                    qDebug() << "Create: " << query.exec() << "/" << db.lastError().text();
                    QSqlQuery query2("DROP TABLE banana;");
                    qDebug() << "Drop:" << query2.exec() << "/" << db.lastError().text();@

                    As you can see, it's a fairly simple code, and it should be working, but everything except for the DB::open returns false. Any idea why?

                    Update: If I use prepare() and then exec(), the query works. What is this?

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #12

                      It's well defined behavior as stated in the API docs:

                      bq. *From "QSqlQuery::exec() ":http://doc.qt.nokia.com/4.7/qsqlquery.html#exec-2 docs:
                      Executes a previously prepared SQL query. Returns true if the query executed successfully; otherwise returns false.
                      [emphasis by me]

                      If you do not want to prepare the query beforehand, call exec with the QString argument.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        bu7ch3r
                        wrote on last edited by
                        #13

                        [code]
                        QSqlQuery query("CREATE TABLE banana ( id TEXT );");
                        [/code]
                        Here is the problem. When you instance te QSqlQuerry object. The sql "create" statement is run. When you call exec() there is no other querry to be run. You can instantiate QSqlQuery with no string in constructor argument, then call onject.(->) prepare("sql statement") then call exec() and it will work.

                        Try to see what QSqlQuery query("CREATE TABLE banana ( id TEXT );"); returns and you will see that it returns true.

                        Also, if you have two databases when you call "addDatabase("QSQLITE")" add second param to the function to make a difference, otherwise it will close your last connection.

                        for(int i = 200; i > 0;)
                        try
                        {
                        //do something
                        }
                        catch(...)
                        {
                        i--;//try again
                        }

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #14

                          [quote author="bu7ch3r" date="1323069729"]
                          Try to see what QSqlQuery query("CREATE TABLE banana ( id TEXT );"); returns and you will see that it returns true.
                          [/quote]

                          That's nonsense. This statement

                          @
                          QSqlQuery query("CREATE TABLE banana ( id TEXT );");
                          @

                          creates an object. There is no "return value" at all.

                          But there's something additional, that I've overlooked for the QString-argument constructor of QSqlQuery too:

                          bq. From "QSqlQuery::QSqlQuery ( const QString & query = QString(), QSqlDatabase db = QSqlDatabase() ) ":http://doc.qt.nokia.com/4.7/qsqlquery.html#QSqlQuery-2 docs:
                          Constructs a QSqlQuery object using the SQL query and the database db. If db is not specified, or is invalid, the application's default database is used. If query is not an empty string, it will be executed.
                          [emphasis by me]

                          So, you actually try to execute your query twice.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            bu7ch3r
                            wrote on last edited by
                            #15

                            Sorry Volker, you are right, in fact c++ is right that constructors don't return values :D. (It was morning here and didn't drink my coffe:P). I'll add my post to most stupid things I have said in my life regarding C++.

                            As your quote says if you pass a string to constructor the query will execute.

                            The sql statement is CREATE, it is normal when an sql querry that creates table to fail on second call, because the table was created first time.

                            [code]QSqlQuery query("CREATE TABLE banana ( id TEXT );");//Table is created[/code]
                            query.exec(); failse because CREATE fails because target table it's already there.

                            I think veeeee_d didn't know that the sql statement it's executed when QSqlQuerry("sql_with_statement") is called.

                            Again sorry for bad post :)

                            for(int i = 200; i > 0;)
                            try
                            {
                            //do something
                            }
                            catch(...)
                            {
                            i--;//try again
                            }

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              goetz
                              wrote on last edited by
                              #16

                              [quote author="bu7ch3r" date="1323092077"](It was morning here and didn't drink my coffe:P)
                              [/quote]

                              Eeek... let's put that into a "the boot sequence hasn't finished" folder :-) - together with my stuff in that weird mode :D

                              I wonder how economics in general and programming in particular would ever work if there was no coffee :-)

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              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