Problem with DB connection [SOLVED]

  • I've been trying to connect my program to a database, unsuccessful
    @#include <QCoreApplication>
    #include <QtSql>
    #include <QtDebug>

    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    // QString servername = "LOCALHOST\SQLEXPRESS";
    QString servername = "LOCALHOST\MYSQL";
    QString dbname = "test.dat";

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    QString dsn = QString("DRIVER=(SQL Native Client);SERVER=%1;DATABASE=%2;UID=root;PWD=root;").arg(servername).arg(dbname);

    // QString dsn = QString("DRIVER=(SQL Native Client);SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg(dbname);

    // db.setDatabaseName(":memory:");

        qDebug() << "Opened!";
    } else {
        qDebug() << "Error: " << db.lastError().text() ;
    return a.exec(&#41;;


    The error I keep getting is "out of memory Error opening database".
    I use UniServer, with Apache and MySQL.

    If anyone has any ideas, I'd be grateful.


    [[merged two identical posts, Tobias]]

  • You don't need to use DSN and you've to use addDatabase("QMYSQL"); .... try to read this post:

  • It worked!
    Thank you very much!

  • Could you please tag this thread as [SOLVED]

  • the tag was added by someone, I also updated the title.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.