Nominate our 2022 Qt Champions!

SQL Drivers and Qt on Windows - How to?

  • Alright, alright. This is bugging me for a while now, I was making a server and I have now completed every single part of it, from packet sending/receiving to encryption, decryption, signal-sending and everything that would be relevant EXCEPT for databases. The reason is simple: I can't seem to get how Qt actually does its database system.
    I don't have any LIB or DLL file that would go for them and I can't seem to be able to make them myself. The documentation is not clear at all when it comes to using and building these drivers, so I must be doing something really wrong or it just is brain-dead simple and I'm just looking everywhere except for the right place.

    Someone, please?

    Thanks in advance.

  • Qt comes with sqllite driver "embedded". If you need to connect to another database you have to compile it on your own. So if this is your problem, this "should": help. If you have already done it, maybe you need to add the sql keyword to your project file, such as

    @QT += core gui sql

    otherwise I don't understand what the problem really is.

  • 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.

  • 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.

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

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

  • 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.

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

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

  • 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.

  • 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");
    qDebug() <<;
    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?

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

    bq. *From "QSqlQuery::exec() ": 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.

  • [code]
    QSqlQuery query("CREATE TABLE banana ( id TEXT );");
    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.

  • [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.

    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() ) ": 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.

  • 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 :)

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

    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 :-)

Log in to reply