Qt Forum

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

    Qt Academy Launch in California!

    Problem with DB connection [SOLVED]

    General and Desktop
    2
    5
    4476
    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.
    • L
      lzr_mihnea last edited by

      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");
      
      db.setConnectOptions();
      
      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:");
      db.setDatabaseName(dsn);

      if(db.open())
      {
          qDebug() << "Opened!";
          db.close();
      } 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.

      Regards!

      [[merged two identical posts, Tobias]]

      1 Reply Last reply Reply Quote 0
      • M
        mbnoimi last edited by

        You don't need to use DSN and you've to use addDatabase("QMYSQL"); .... try to read this post:
        https://qt-project.org/forums/viewthread/12380

        1 Reply Last reply Reply Quote 0
        • L
          lzr_mihnea last edited by

          It worked!
          Thank you very much!

          1 Reply Last reply Reply Quote 0
          • M
            mbnoimi last edited by

            Could you please tag this thread as [SOLVED]

            1 Reply Last reply Reply Quote 0
            • L
              lzr_mihnea last edited by

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

              1 Reply Last reply Reply Quote 0
              • First post
                Last post