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. Problem with DB connection [SOLVED]

Problem with DB connection [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.6k 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.
  • L Offline
    L Offline
    lzr_mihnea
    wrote on 27 Mar 2013, 10:27 last edited by
    #1

    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
    0
    • M Offline
      M Offline
      mbnoimi
      wrote on 27 Mar 2013, 10:34 last edited by
      #2

      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
      0
      • L Offline
        L Offline
        lzr_mihnea
        wrote on 27 Mar 2013, 10:40 last edited by
        #3

        It worked!
        Thank you very much!

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbnoimi
          wrote on 30 Mar 2013, 22:48 last edited by
          #4

          Could you please tag this thread as [SOLVED]

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lzr_mihnea
            wrote on 1 Apr 2013, 07:37 last edited by
            #5

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

            1 Reply Last reply
            0

            2/5

            27 Mar 2013, 10:34

            • Login

            • Login or register to search.
            2 out of 5
            • First post
              2/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved