Results 1 to 9 of 9

Thread: network application

  1. #1
    Join Date
    Mar 2012
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default network application

    what I have to do is to put the database on one machine and access to it from different machines on which the application is installed
    what we must do is
    QSqlDatabase db = QSqlDatabase :: addDatabase (......)
    we must provide the parameters to the function addDatabase such as machine name, port, and the driver
    can you explain me these parameters and provide me with the arguments of the function
    thanks

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: network application

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase( "QMYSQL", "MyUniqueConnectionName" ); // first argument is driver name, second is unique connection name
    2. db.setDatabaseName( "MyDatabaseName" ); // name of your database on the server
    3. db.setHostName( "my_host_name.co.uk" ); // hostname of your database server
    4. db.setPort( 3306 ); // port of the sql server
    5. db.setUserName( "db_user_name" ); // user name in the database
    6. db.setPassword( "your_super_secret_password" ); // password for that user
    7.  
    8. if( db.open() )
    9. {
    10. // connected
    11. }
    12. else
    13. {
    14. // not connected
    15. }
    To copy to clipboard, switch view to plain text mode 

    In fact everythign is explained in the docummentation, did you read it?

  3. #3
    Join Date
    Mar 2012
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: network application

    thanks for your reply,
    i already did all of this, i had a problem just in the first instruction addDatabase
    that's why i asked

  4. #4
    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: network application

    So you already did what Spitfire has suggested, i.e. read the documentation and written code like the example above. Clearly, the QSqlDatabase::addDatabase() function does not take machine name, port, user name or password as an argument, yet that is what you asked. What exactly is the problem?
    Last edited by ChrisW67; 10th April 2012 at 00:35.

  5. #5
    Join Date
    Mar 2012
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: network application

    my professor told me that if i want to make the application on network i have to add other parameters as name of the machine and the bridhe
    so i can put the database in another machine and access to it in any other machine where the application is already installed
    thanks again

    that's my program

    #include <QtGui/QApplication>
    #include<QtSql>
    #include<QtDebug>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QSqlDatabase db =QSqlDatabase::addDatabase("QODBC");


    db.setHostName("localhost");
    db.setDatabaseName("dsi64");
    db.setPassword("khadija");
    db.setUserName("postgres");
    db.setPort(5432);
    if(db.open())
    {
    qDebug() <<"opened" ;
    QSqlQuery query;
    query.exec("SELECT id_niveau,intitule from niveau;");

    while (query.next())
    {
    QString intitule = query.value(1).toString();
    int id_niveau = query.value(0).toInt();
    qDebug() << qPrintable(intitule) << ": " << id_niveau << endl;
    }
    db.close();
    }
    else
    {
    qDebug() << db.lastError().text();
    }
    MainWindow w;
    w.show();

    return a.exec();
    }

  6. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: network application

    So, what's the problem?

    You can't connect to remote host?
    No wonder there as you're using 'localhost' as hostname.
    use setHostName() to set target machine host and that's it.

    Did you read the doc (or at least the comments in my example)?

    btw machine name and hostname are two different things, don't confuse them.

  7. #7
    Join Date
    Mar 2012
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: network application

    when i put the database in the other machine , can i put in datasource odbc another server and not localhost :just another name so that if i put that name in the fonction setHostName it would work
    thanks for your attention

  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: network application

    Why are You trying to connect to PostgreSQL with ODBC driver not with PGSQL ?

  9. #9
    Join Date
    Mar 2012
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: network application

    i'm used to work in odbc and it works well
    i dont know what i'm going to put in server in odbc it accepts just localhost

Similar Threads

  1. Replies: 0
    Last Post: 6th April 2012, 10:06
  2. Replies: 1
    Last Post: 11th February 2011, 04:32
  3. Specialised Network Chat Application
    By TropicalPenguin in forum Jobs
    Replies: 0
    Last Post: 28th September 2010, 22:40
  4. QT Network
    By csvivek in forum Newbie
    Replies: 0
    Last Post: 1st April 2008, 17:19
  5. help in QT network!
    By bbc58206 in forum Qt Programming
    Replies: 3
    Last Post: 10th September 2007, 05:00

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.