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. Role of .setHostName() while Connecting to DataBase in Qt
Qt 6.11 is out! See what's new in the release blog

Role of .setHostName() while Connecting to DataBase in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 1.9k Views 2 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.
  • Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by
    #1

    Hi All ,
    Below are the code snippet given in Qt documentation of Qt; Connecting to Database.
    I want to know what to write in the HostName. I would also like to know if I've to give the absolute path address of my .db file for .setDatabaseName (). I have a .db file in SQLite.

    Any help will be appreciated!
    Thanks in advance!

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("bigblue");
    db.setDatabaseName("flightdb");
    db.setUserName("acarlson");
    db.setPassword("1uTbSbAs");
    bool ok = db.open();
    

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    JonBJ 1 Reply Last reply
    0
    • Swati777999S Swati777999

      Hi All ,
      Below are the code snippet given in Qt documentation of Qt; Connecting to Database.
      I want to know what to write in the HostName. I would also like to know if I've to give the absolute path address of my .db file for .setDatabaseName (). I have a .db file in SQLite.

      Any help will be appreciated!
      Thanks in advance!

      QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
      db.setHostName("bigblue");
      db.setDatabaseName("flightdb");
      db.setUserName("acarlson");
      db.setPassword("1uTbSbAs");
      bool ok = db.open();
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Swati777999
      This is an example for MySQL, but you are using SQLite. For that just use db.setDatabaseName("/full/path/to/flightdb");. You can ignore setHostName() (or set it to "localhost").

      1 Reply Last reply
      1
      • Swati777999S Offline
        Swati777999S Offline
        Swati777999
        wrote on last edited by
        #3

        I wrote the following code for SQLite but how would I know if the database got connected to Qt correctly? Where can I find the notification about the status of the connection to the database?

             QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
            db.setDatabaseName("C:/Users/ss/Desktop/TestData.db");
            db.setHostName("localhost");
            db.setUserName("swati");
            db.setPassword("qtsqlite");
        

        “ In order to be irreplaceable, one must always be different” – Coco Chanel

        artwawA KroMignonK 2 Replies Last reply
        0
        • Swati777999S Swati777999

          I wrote the following code for SQLite but how would I know if the database got connected to Qt correctly? Where can I find the notification about the status of the connection to the database?

               QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
              db.setDatabaseName("C:/Users/ss/Desktop/TestData.db");
              db.setHostName("localhost");
              db.setUserName("swati");
              db.setPassword("qtsqlite");
          
          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @Swati777999 Have you tried to read the documentation? It is all described on the very first page, you know.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          2
          • Swati777999S Swati777999

            I wrote the following code for SQLite but how would I know if the database got connected to Qt correctly? Where can I find the notification about the status of the connection to the database?

                 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                db.setDatabaseName("C:/Users/ss/Desktop/TestData.db");
                db.setHostName("localhost");
                db.setUserName("swati");
                db.setPassword("qtsqlite");
            
            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #5

            @Swati777999 said in Role of .setHostName() while Connecting to DataBase in Qt:

            I wrote the following code for SQLite but how would I know if the database got connected to Qt correctly? Where can I find the notification about the status of the connection to the database?

            I think you have first to understand how Qt is managing databases.
            QSqlDatabase is used to define all database connection you want to use in your application.
            In you case you have defined the default database connection, because you don't have give a name to the connection.

            Before be able to use the connection, you have to open it and then you use QSqlQuery to send SQL statements to the DB

            You can take a look at this http://katecpp.github.io/sqlite-with-qt/ which gives you step by step explanation about SQLite usage with Qt.

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            Swati777999S 1 Reply Last reply
            3
            • KroMignonK KroMignon

              @Swati777999 said in Role of .setHostName() while Connecting to DataBase in Qt:

              I wrote the following code for SQLite but how would I know if the database got connected to Qt correctly? Where can I find the notification about the status of the connection to the database?

              I think you have first to understand how Qt is managing databases.
              QSqlDatabase is used to define all database connection you want to use in your application.
              In you case you have defined the default database connection, because you don't have give a name to the connection.

              Before be able to use the connection, you have to open it and then you use QSqlQuery to send SQL statements to the DB

              You can take a look at this http://katecpp.github.io/sqlite-with-qt/ which gives you step by step explanation about SQLite usage with Qt.

              Swati777999S Offline
              Swati777999S Offline
              Swati777999
              wrote on last edited by
              #6

              @KroMignon Thanks for your answer!

              Just like Kate (Reference: the link given by you), I've put the following conditions for getting the status of the connection in the Application Output of QtCreator but I only get the output whenever there's an error. I don't get the output when there's no error. Why is it so? Can you please explain?

                 bool ok = db.open();
                  if (!ok)
                  {
                     qDebug() << "Error: connection with database failed";
                  }
                  else
                  {
                     qDebug() << "Database: connection ok";
                  }
              

              “ In order to be irreplaceable, one must always be different” – Coco Chanel

              KroMignonK JonBJ 2 Replies Last reply
              0
              • Swati777999S Swati777999

                @KroMignon Thanks for your answer!

                Just like Kate (Reference: the link given by you), I've put the following conditions for getting the status of the connection in the Application Output of QtCreator but I only get the output whenever there's an error. I don't get the output when there's no error. Why is it so? Can you please explain?

                   bool ok = db.open();
                    if (!ok)
                    {
                       qDebug() << "Error: connection with database failed";
                    }
                    else
                    {
                       qDebug() << "Database: connection ok";
                    }
                
                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #7

                @Swati777999 said in Role of .setHostName() while Connecting to DataBase in Qt:

                I don't get the output when there's no error. Why is it so? Can you please explain?

                No can't explain, this don't make sense to me.
                You could change to code to:

                bool ok = db.open();
                qDebug() << "Database: connection " << (ok ? "OK" : "FAILURE");
                

                You should always got a message.
                Are you sure you have build the project before starting it?

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                0
                • Swati777999S Swati777999

                  @KroMignon Thanks for your answer!

                  Just like Kate (Reference: the link given by you), I've put the following conditions for getting the status of the connection in the Application Output of QtCreator but I only get the output whenever there's an error. I don't get the output when there's no error. Why is it so? Can you please explain?

                     bool ok = db.open();
                      if (!ok)
                      {
                         qDebug() << "Error: connection with database failed";
                      }
                      else
                      {
                         qDebug() << "Database: connection ok";
                      }
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Swati777999 said in Role of .setHostName() while Connecting to DataBase in Qt:

                  Why is it so? Can you please explain?

                  It is not so. You get a message either way. If you execute that code. If you don't execute the code you don't get any message.

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

                    Hi,

                    One side note: unless you try to open your database on a read-only filesystem where the file does not exist, you won't have any error. This comes from SQLite, if the database file does not exist, it's created.

                    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
                    1

                    • Login

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