Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Behind the Scenes
  3. Wiki Discussion
  4. Sql connection
Qt 6.11 is out! See what's new in the release blog

Sql connection

Scheduled Pinned Locked Moved Wiki Discussion
5 Posts 2 Posters 4.6k Views 1 Watching
  • 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
    barun
    wrote on last edited by
    #1

    hello! i m using Qt 5.1. i want to connect my sql data with qt..but shows problem
    my is here..please help me..
    @
    #include <QtCore/QCoreApplication>
    #include <QtSql>
    #include <QDebug>
    #include <QString>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    QString servername="BARUN//BARUN";
    QString dbname= "testDB";

    QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
    db.setConnectOptions();
    QString dsn=QString("DRIVER{SQL NATIVE CLIENT};SERVER").arg(servername).arg(dbname);
    
    db.setDatabaseName(dsn);
    if(db.open())
    {
        qDebug()<<"opened";
        db.close();
    }
    else
    {
        qDebug()<<"error"<<db.lastError().text();
    }
    
    return a.exec&#40;&#41;;
    

    }
    @

    after compiling it shows ..enable to connect
    my sql server name is=BARUN\BARUN

    edit: please use @ tags around your code sections; Andre

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Please enclose your code in coding tags, without them it's really hard to read.

      Are you sure that the server name is correct ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        barun
        wrote on last edited by
        #3

        my server name is=BARUN/BARUN

        CODE:
        @
        int main(int argc, char *argv[])
        { QCoreApplication a(argc, argv);
        QString servername=“BARUN//BARUN”;
        QString dbname= “testDB”;

        QSqlDatabase db=QSqlDatabase::addDatabase(“QODBC”); db.setConnectOptions(); QString dsn=QString(“DRIVER{SQL NATIVE CLIENT};SERVER”).arg(servername).arg(dbname); db.setDatabaseName(dsn);
        if(db.open())
        { qDebug()<<“opened”; db.close(); }
        else
        { qDebug()<<“error”<<db.lastError().text();
        }
        return a.exec();
        }
        @

        Again: use @ tags around code sections please; Andre

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Are you sure your dsn variable contains the correct data ? Since your not using coding tags (one @ at the beginning and one at the end) I'm not sure your are not missing a %1 and a %2 to get your servername and dbname in the string.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • B Offline
            B Offline
            barun
            wrote on last edited by
            #5

            my dns :
            @QString dsn=QString("DRIVER{Sql native client};server=%1;database=%2;trusted_connectivity=yes;").arg(servername).arg(dbname);@

            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