Results 1 to 16 of 16

Thread: Qsql and connection

  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,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 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 10:27.

  7. #5
    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

    It seems that you have not built QODBC driver. You have to build it first (find its source in Src\qtbase\src\plugins\sqldrivers)


    Added after 6 minutes:


    See this :

    http://qt-project.org/doc/qt-4.8/sql...gin-on-windows

    for how to build odbc plugin on windows
    Last edited by alizadeh91; 5th April 2013 at 11:12.

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

    smemamian (5th April 2013)

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

    Default Re: Qsql and connection

    i open cmd and i write :

    Qt Code:
    1. cd C:\Qt\Qt5.0.0\5.0.0\Src\qtbase\src\plugins\sqldrivers\odbc
    2. qmake odbc.pro
    To copy to clipboard, switch view to plain text mode 
    I get the error message :
    'qmake' is not recognized as a internal or external command ,
    operable program of batch file

  10. #7
    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

    Did u add qmake to your environment variables???

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

    smemamian (5th April 2013)

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

    Default Re: Qsql and connection

    no, how ?

  13. #9
    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

    1.RightClick on mycomputer
    2. click properties
    3. go to "change settings" in the link at bottom right corner of window
    4. go to "advance" tab
    5. Press environment variables
    6. from system variables list, double click on "path"
    7. First of all put a ";" at end of "variable value" then paste address of your qmake (e.g C:\Qt\Qt5.0.0\5.0.0\msvc2010\bin) out there after the simicolon (";")
    8. reopen the cmd then type "qmake -v" to see if you made it right or not!
    Last edited by alizadeh91; 5th April 2013 at 12:42.

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

    Default Re: Qsql and connection

    this is correct ?
    Qt Code:
    1. variable value : C:\TCWIN45\BIN;C:\Qt\Qt5.0.0\5.0.0\msvc2010\bin\qmake
    To copy to clipboard, switch view to plain text mode 

  15. #11
    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

    just don't remove any character in "variable value" field. just put a simicolon at end and paste qmake address (C:\Qt\Qt5.0.0\5.0.0\msvc2010\bin don't put qmake at end of address)

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

    smemamian (5th April 2013)

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

    Default Re: Qsql and connection

    Tnx , Hooray\:d/

    variable value : C:\TCWIN45\BIN;C:\Qt\Qt5.0.0\5.0.0\msvc2010\bin;

    Added after 12 minutes:


    Qt Code:
    1. nmake
    To copy to clipboard, switch view to plain text mode 
    'nmake' is not recognized as a internal or external command ,
    operable program of batch file
    i hasn't nmake in C:\Qt\Qt5.0.0\5.0.0\msvc2010\bin address for add to environment variables (
    Last edited by smemamian; 5th April 2013 at 13:05.

  18. #13
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qsql and connection

    hi,
    i hasn't nmake in C:\Qt\Qt5.0.0\5.0.0\msvc2010\bin address for add to environment variables
    just find nmake using a search explorer and set the path

    hope it helps,
    Bala

  19. #14
    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

    Instead of cmd you can use "visual studio command prompt" (just search it in start menu and open it). It recognize nmake by itself

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

    Default Re: Qsql and connection

    Quote Originally Posted by BalaQT View Post
    hi,


    just find nmake using a search explorer and set the path

    hope it helps,
    Bala
    No items match your search


    Added after 4 minutes:


    Quote Originally Posted by alizadeh91 View Post
    Instead of cmd you can use "visual studio command prompt" (just search it in start menu and open it). It recognize nmake by itself
    Very thanks .
    Last edited by smemamian; 5th April 2013 at 18:05.

  21. #16
    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

    use visual studio command prompt

Similar Threads

  1. Replies: 0
    Last Post: 11th November 2011, 20:18
  2. Using auto_increment in QSql
    By SykeS in forum Newbie
    Replies: 3
    Last Post: 21st May 2010, 08:13
  3. Replies: 1
    Last Post: 2nd April 2010, 07:42
  4. connection with qsql module
    By adamatic in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2009, 11:38
  5. QSql error
    By renjithmamman in forum Qt Programming
    Replies: 4
    Last Post: 20th December 2006, 11: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.