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. [SOLVED] Can't insert values to database with prepare and bindValue functions
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Can't insert values to database with prepare and bindValue functions

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.0k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello. I've been trying to insert some values to my database. There are three columns in the database and first column is integer primary key autoincrement and the others are integer unique. When I run this code:
    @QSqlQuery query;
    query.exec("insert into tableName values(101, 49, 50)");@

    It adds the values 101,49,50 to the new row. However the following code just increments the first column's value and doesn't add any value to second and third columns:
    @QSqlQuery query;
    int x = 99;
    int y = 51;
    int z = 52;

    query.prepare("INSERT INTO definition(columnName1, columnName2, columnName3)"
    "VALUES (:x, :y, :z)");

    query.bindValue("columnName1", x);
    query.bindValue(":columnName2", y);
    query.bindValue(":columnName3", z);@

    I am using SQLite3 Database and Qt 5.4.0. Also I am wondering if I call the code without adding any value to the first column does the program automatically increments the first column's value and add other values to the second and third columns? Since I couldn't run the code above I couldn't try this. What I am trying to say is a code like this:
    @int y = 51;
    int z = 52;

    query.prepare("INSERT INTO definition(columnName2, columnName3)"
    "VALUES (:y, :z)");

    query.bindValue(":columnName2", y);
    query.bindValue(":columnName3", z);@

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      Try with following

      @query.bindValue(":x", 10);
      query.bindValue(":y", 20);
      query.bindValue(":z", 30);@

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      1 Reply Last reply
      2
      • O Offline
        O Offline
        ObiWan
        wrote on last edited by
        #3

        There is missing colon in the line

        @query.bindValue("columnName1", x);@

        It should be

        @query.bindValue(":columnName1", x);@

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          This solved my problem. Thanks!
          [quote author="Dheerendra" date="1420995958"]Try with following

          @query.bindValue(":x", 10);
          query.bindValue(":y", 20);
          query.bindValue(":z", 30);@
          [/quote]

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
            wrote on last edited by
            #5

            You can close this request chaining the subject line to SOLVED.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            https://www.pthinks.com

            1 Reply Last reply
            2

            • Login

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