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

QSqlQuery not binding

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 420 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.
  • K Offline
    K Offline
    Kevin470
    wrote on 11 Aug 2022, 07:15 last edited by Kevin470 8 Nov 2022, 07:17
    #1

    Hello,
    I am trying to execute a simple select query to an sqlite database with one argument that I want to bind.
    I have a couple of databases that I will be working on.
    This is my code

    QString table_name = "variant_list";
    
        QSqlQuery query(database);
        query.prepare("SELECT * FROM :table");
        query.bindValue(":table", table_name);
    
        qInfo() << "My Query:" << query.lastQuery();
        qInfo() << "My Query:" << query.result();
    
    
        if(!query.exec())
        {
            qDebug() << "Can't Execute Query!!";
            qDebug() << database.lastError();
    
            return false;
        }
        else
        {
            qDebug() << "Query executed Successfully!";
            return true;
        }
    

    qInfo() << "My Query:" << query.lastQuery(); prints "My Query: "SELECT * FROM :table"
    qDebug() << database.lastError(); prints QSqlError("", "", "")

    I tried all methods to bind values from "https://doc.qt.io/qt-5/qsqlquery.html#approaches-to-binding-values".
    It looks like it isn't binding in any of those cases.
    Where can I be doing it wrong?
    Thanks.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Aug 2022, 07:30 last edited by
      #2

      Hi,

      Bind variables are not available for all query types nor for all elements of an SQL statement.

      Typically, the table name cannot be a bind variable.

      One of the base rule is that bind variables cannot change the structure of a query.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply 11 Aug 2022, 07:41
      2
      • S SGaist
        11 Aug 2022, 07:30

        Hi,

        Bind variables are not available for all query types nor for all elements of an SQL statement.

        Typically, the table name cannot be a bind variable.

        One of the base rule is that bind variables cannot change the structure of a query.

        K Offline
        K Offline
        Kevin470
        wrote on 11 Aug 2022, 07:41 last edited by
        #3

        @SGaist Thank you for answering.
        Could you let me know the possibilities that are available to be able to bind variables?
        I tried an INSERT query "INSERT INTO variant_list (variant_name,variant_type) VALUES (:variant_name,:variant_type)" and it worked like a charm.
        Does that mean the Column Names and values are allowed to be bound and any other element in the query like the Statement, the table name etc. won't bind?

        J 1 Reply Last reply 11 Aug 2022, 08:02
        0
        • K Kevin470
          11 Aug 2022, 07:41

          @SGaist Thank you for answering.
          Could you let me know the possibilities that are available to be able to bind variables?
          I tried an INSERT query "INSERT INTO variant_list (variant_name,variant_type) VALUES (:variant_name,:variant_type)" and it worked like a charm.
          Does that mean the Column Names and values are allowed to be bound and any other element in the query like the Statement, the table name etc. won't bind?

          J Offline
          J Offline
          JonB
          wrote on 11 Aug 2022, 08:02 last edited by
          #4

          @Kevin470
          Your query will work because it binds values. Basically, that is all you can do. Not only can you not bind table names, you also could not bind the column names in your query (INSERT INTO variant_list (:variant_column_name, :variant_column_type) VALUES ... not allowed).

          K 1 Reply Last reply 11 Aug 2022, 09:24
          2
          • J JonB
            11 Aug 2022, 08:02

            @Kevin470
            Your query will work because it binds values. Basically, that is all you can do. Not only can you not bind table names, you also could not bind the column names in your query (INSERT INTO variant_list (:variant_column_name, :variant_column_type) VALUES ... not allowed).

            K Offline
            K Offline
            Kevin470
            wrote on 11 Aug 2022, 09:24 last edited by
            #5

            @JonB Thank you for that. That has been super helpful. :)

            1 Reply Last reply
            0

            1/5

            11 Aug 2022, 07:15

            • Login

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