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 Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 889 Views 1 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.
  • P Offline
    P Offline
    Pappu Kumar Keshari
    wrote on 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");

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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

        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");

        JonBJ Online
        JonBJ Online
        JonB
        wrote on 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 JonBJ 2 Replies Last reply
        1
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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
          1
          • JonBJ JonB

            @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 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
            • Christian EhrlicherC Christian Ehrlicher

              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 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");

              jsulmJ 1 Reply Last reply
              0
              • P Pappu Kumar Keshari

                @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");

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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
                • JonBJ JonB

                  @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?

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0

                  • Login

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