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]Error while using 'bindValue' for my QSqlQuery
Forum Updated to NodeBB v4.3 + New Features

[Solved]Error while using 'bindValue' for my QSqlQuery

Scheduled Pinned Locked Moved General and Desktop
qsqlquerybindvalueqsqlquerymodel
3 Posts 2 Posters 1.5k 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.
  • ealioneE Offline
    ealioneE Offline
    ealione
    wrote on last edited by ealione
    #1

    I have a simple query that goes like this:

        QSqlQuery query(QSqlDatabase::database(name));
        query.prepare("SELECT * FROM :table WHERE login=:login");
        query.bindValue(":table", GVars::strDbTabel);
        query.bindValue(":login", GVars::strUserLogin);
        if (!query.exec())
        {
            qWarning() << "Database error: " << query.lastError().text();
        }
    

    Running this said that there is an error on the query at the point where my strDbTabel value is.

    If instead I do not use bind value and I directly enter the name of my table then it seems to be working fine:

        QSqlQuery query(QSqlDatabase::database(name));
        query.prepare("SELECT * FROM userTable WHERE login=:login");
        query.bindValue(":login", GVars::strUserLogin);
        if (!query.exec())
        {
            qWarning() << "Database error: " << query.lastError().text();
        }
    

    I event tried using QSqlQueryModel:

    QSqlQueryModel model;    
    model.setQuery(QString("SELECT * FROM %1 WHERE login=%2").arg(GVars::strDbTabel).arg(GVars::strUserLogin));
    

    with even worse results
    QSqlQuery::exec: database not open

    even though my database is open.

    Why can't I use bindValue to chose my table name?

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Hi, while you can do a lot of stuff with bindvalue you cannot use it for selecting a table name :-(

      It's a limitation in the SQL spec. for more on this, see for example: Stackoverflow question

      1 Reply Last reply
      1
      • ealioneE Offline
        ealioneE Offline
        ealione
        wrote on last edited by ealione
        #3
        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