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. How to insert data from Qt to PostgreSql(DataBase) ?
Forum Update on Monday, May 27th 2025

How to insert data from Qt to PostgreSql(DataBase) ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 820 Views
  • 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.
  • P Offline
    P Offline
    Pappu Kumar Keshari
    wrote on 25 Jan 2023, 19:10 last edited by
    #1

    I am trying with following code but data is not inserting in postgreSql(Database) . Where is error in my code ? Suggest please.

    QSqlQuery qsql;
    qsql.prepare("INSERT INTO prod.user_details(user_id, first_name, last_name, login_name, login_password)"
    "VALUES (?, ?, ?, ?, ?)");
    qsql.bindValue(0,23);
    qsql.bindValue(1, "ranjeet");
    qsql.bindValue(2, "keshari");
    qsql.bindValue(3,"ranjeet789");
    qsql.bindValue(4,"ranjeetmicro");

    J 1 Reply Last reply 25 Jan 2023, 19:14
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Jan 2023, 19:13 last edited by
      #2

      Hi,

      With the code you show, there's at least nothing executed so that would be the main issue.

      Otherwise, it would be way simpler to check the value returned by exec and if false use QSqlQuery's lastError method to get what went wrong.

      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
      • P Pappu Kumar Keshari
        25 Jan 2023, 19:10

        I am trying with following code but data is not inserting in postgreSql(Database) . Where is error in my code ? Suggest please.

        QSqlQuery qsql;
        qsql.prepare("INSERT INTO prod.user_details(user_id, first_name, last_name, login_name, login_password)"
        "VALUES (?, ?, ?, ?, ?)");
        qsql.bindValue(0,23);
        qsql.bindValue(1, "ranjeet");
        qsql.bindValue(2, "keshari");
        qsql.bindValue(3,"ranjeet789");
        qsql.bindValue(4,"ranjeetmicro");

        J Offline
        J Offline
        JonB
        wrote on 25 Jan 2023, 19:14 last edited by
        #3

        @Pappu-Kumar-Keshari
        We don't know what else you are doing, what you do with the query, how the database is open, what the table schema is, whether you have permission....

        What did QSqlQuery::lastError() tell you?

        P J 2 Replies Last reply 27 Jan 2023, 04:34
        1
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 25 Jan 2023, 19:15 last edited by
          #4

          QSqlQuery::prepare() returns a boolean which you should check and if it fails use QSqlQuery::lastError() to retrieve the actual error string. Also your code does not show how/where you exec() your query.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          P 1 Reply Last reply 27 Jan 2023, 05:04
          1
          • J JonB
            25 Jan 2023, 19:14

            @Pappu-Kumar-Keshari
            We don't know what else you are doing, what you do with the query, how the database is open, what the table schema is, whether you have permission....

            What did QSqlQuery::lastError() tell you?

            P Offline
            P Offline
            Pappu Kumar Keshari
            wrote on 27 Jan 2023, 04:34 last edited by
            #5

            @JonB I am able to fetch data from data base to Qt Successfully. But I am unable to insert data from Qt to data base that's problematic. I am using this code but data is not inserting

            I am trying with following code but data is not inserting in postgreSql(Database) . Where is error in my code ? Suggest please.

            QSqlQuery qsql;
            qsql.prepare("INSERT INTO prod.user_details(user_id, first_name, last_name, login_name, login_password)"
            "VALUES (?, ?, ?, ?, ?)");
            qsql.bindValue(0,23);
            qsql.bindValue(1, "ranjeet");
            qsql.bindValue(2, "keshari");
            qsql.bindValue(3,"ranjeet789");
            qsql.bindValue(4,"ranjeetmicro");

            0

            1 Reply Last reply
            0
            • C Christian Ehrlicher
              25 Jan 2023, 19:15

              QSqlQuery::prepare() returns a boolean which you should check and if it fails use QSqlQuery::lastError() to retrieve the actual error string. Also your code does not show how/where you exec() your query.

              P Offline
              P Offline
              Pappu Kumar Keshari
              wrote on 27 Jan 2023, 05:04 last edited by
              #6

              @Christian-Ehrlicher I am able to fetch data from data base to Qt Successfully. But I am unable to insert data from Qt to data base that's problematic. I am using this code but data is not inserting
              I am trying with following code but data is not inserting in postgreSql(Database) . Where is error in my code ? Suggest please.

              QSqlQuery qsql;
              qsql.prepare("INSERT INTO prod.user_details(user_id, first_name, last_name, login_name, login_password)"
              "VALUES (?, ?, ?, ?, ?)");
              qsql.bindValue(0,23);
              qsql.bindValue(1, "ranjeet");
              qsql.bindValue(2, "keshari");
              qsql.bindValue(3,"ranjeet789");
              qsql.bindValue(4,"ranjeetmicro");

              J 1 Reply Last reply 27 Jan 2023, 05:12
              0
              • P Pappu Kumar Keshari
                27 Jan 2023, 05:04

                @Christian-Ehrlicher I am able to fetch data from data base to Qt Successfully. But I am unable to insert data from Qt to data base that's problematic. I am using this code but data is not inserting
                I am trying with following code but data is not inserting in postgreSql(Database) . Where is error in my code ? Suggest please.

                QSqlQuery qsql;
                qsql.prepare("INSERT INTO prod.user_details(user_id, first_name, last_name, login_name, login_password)"
                "VALUES (?, ?, ?, ?, ?)");
                qsql.bindValue(0,23);
                qsql.bindValue(1, "ranjeet");
                qsql.bindValue(2, "keshari");
                qsql.bindValue(3,"ranjeet789");
                qsql.bindValue(4,"ranjeetmicro");

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 27 Jan 2023, 05:12 last edited by
                #7

                @Pappu-Kumar-Keshari Please read again what @Christian-Ehrlicher suggested and follow that suggestion - you have zero error handling, please add. And again: you do NOT show where and how you actually insert the data (exec()). Please don't ignore questions and suggestions if you ask for help...

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • J JonB
                  25 Jan 2023, 19:14

                  @Pappu-Kumar-Keshari
                  We don't know what else you are doing, what you do with the query, how the database is open, what the table schema is, whether you have permission....

                  What did QSqlQuery::lastError() tell you?

                  J Offline
                  J Offline
                  JonB
                  wrote on 27 Jan 2023, 08:37 last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0

                  1/8

                  25 Jan 2023, 19:10

                  • Login

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