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] - qsqlquery; bindvalue a select query
Forum Updated to NodeBB v4.3 + New Features

[solved] - qsqlquery; bindvalue a select query

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 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.
  • A Offline
    A Offline
    angelicaP
    wrote on last edited by
    #1

    hi,

    i have the following scenario:

    @ QSqlQuery query;
    query.prepare("INSERT INTO person (id, forename, surname) "
    "VALUES (:id, :forename, :surname)");
    query.bindValue(":id", 1001);
    query.bindValue(":forename", "Bart");
    query.bindValue(":surname", "Simpson");
    query.exec();@

    everything is fine and working.

    now i have two tables, person and actor, the date already exists in actor and I would like to populate person with some data from actor (maybe doesn't make sense what i would like to do, but just on account of this example):
    @ QSqlQuery query;
    query.prepare("INSERT INTO person (id, forename, surname) "
    "VALUES (:id, :forename, :surname)");
    query.bindValue(":id", "select id from actor where actorName= (' " + ui->lineEditActorName->text()+" ')");
    ....
    query.exec();@

    1. is it possible to use bindvalue with queries?
      nothing found in QSqlQuery class ref
    2. is the syntax used here ...... = (' " + ui->lineEditActorName->text()+" ')"); correct?
      i would like to say that actorName is ... whatever is written on the lineEdit
      thanks for your inputs.
    1 Reply Last reply
    0
    • P Offline
      P Offline
      panosk
      wrote on last edited by
      #2

      Hi,

      If that was possible, it would be too messy. Just create your SELECT query in a separate function that will return or assign the value you want (or just execute this query first and assign the result to a variable) and then bind that value in your INSERT query.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        angelicaP
        wrote on last edited by
        #3

        thanks, solved with the solution you gave.

        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