Results 1 to 16 of 16

Thread: Qsql and connection

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2013
    Posts
    45
    Thanks
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qsql and connection

    hi
    I do not understand line 7 and 11,Please explain
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QtSql>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7. QString servername = "LOCALHOST\\SQLEXPRESS" ;
    8. QString dbname = "test";
    9. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    10. db.setConnectOptions();
    11. QString dsn = QString("DRIVER={SQL Native Client);SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg(dbname);
    12. db.setDatabaseName(dsn);
    13.  
    14. return a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    tnx

  2. #2
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Qsql and connection

    Simply you are filling properties of db. servername is address or name of your server. because your computer is your own server so you have to use LOCALHOST or 127.0.0.1 for server name.

  3. The following user says thank you to alizadeh91 for this useful post:

    smemamian (5th April 2013)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qsql and connection

    The string you are constructing at Line 11 is a Data Source Name (DSN) which encapsulates a bunch of settings that you could, alternatively, set up in the Windows ODBC Manager. You should have a driver named "SQL Native Client" in the ODBC Manager.

    Also, the line should read:
    Qt Code:
    1. QString dsn = QString("DRIVER={SQL Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg(dbname);
    To copy to clipboard, switch view to plain text mode 
    Note a ')' became a '}'

  5. The following user says thank you to ChrisW67 for this useful post:

    smemamian (5th April 2013)

  6. #4
    Join Date
    Mar 2013
    Posts
    45
    Thanks
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qsql and connection

    thanks a lot .

    Why do I get the error message when I compile ?
    QsqlDatabase: QODBC driver not loaded
    QSqlDataase : available drivers:QSQLITE
    my code :
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QtSql>
    3. #include <QDebug>
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7. QString servername = "LOCALHOST\\SQLEXPRESS" ;
    8. QString dbname = "test";
    9. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    10. db.setConnectOptions();
    11. QString dsn = QString("DRIVER={SQL Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg(dbname);
    12. db.setDatabaseName(dsn);
    13.  
    14. if(db.open())
    15. {
    16. qDebug() << "Opene.." ;
    17. }
    18.  
    19. else
    20. qDebug() << "Not Opene.." << db.lastError();
    21.  
    22. return a.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    Microsoft SQL Server 2008
    Windows 7
    Last edited by smemamian; 5th April 2013 at 09:27.

Similar Threads

  1. Replies: 0
    Last Post: 11th November 2011, 19:18
  2. Using auto_increment in QSql
    By SykeS in forum Newbie
    Replies: 3
    Last Post: 21st May 2010, 07:13
  3. Replies: 1
    Last Post: 2nd April 2010, 06:42
  4. connection with qsql module
    By adamatic in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2009, 10:38
  5. QSql error
    By renjithmamman in forum Qt Programming
    Replies: 4
    Last Post: 20th December 2006, 10:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.