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. SQLite create table does not work when inserting variable into query
Forum Update on Monday, May 27th 2025

SQLite create table does not work when inserting variable into query

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 337 Views
  • 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.
  • iwoitheI Offline
    iwoitheI Offline
    iwoithe
    wrote on last edited by
    #1

    I am attempting to insert a variable into a query using QSqlQuery::prepare(). The code is the following.

    QSqlQuery query;
    // value.at(2).toString() returns the ID of the profile as a QString
    QString profilePropTable = "profile_" + value.at(2).toString() + "_properties";
    query.prepare("create table if not exists ? (name TEXT, type TEXT, display_item TEXT");
    query.bindValue(0, profilePropTable);
    query.exec();
    

    The error I get (using query.lastError().text()) is "Parameter count mismatch". I have also used named placeholders with the same error.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      You cannot use a placeholder and bind variable in place of the table name, only for binding values to columns.

      Something like this:

      QString strQuery = QString(
        "create table if not exists profile_%1_properties name TEXT, type TEXT, display_item TEXT"
       ).arg(value.at(2).toString());
      query.exec(strQuery);
      

      but only if you are 100% certain the value in value.at(2) cannot be subverted by the user (security risk).

      iwoitheI 1 Reply Last reply
      2
      • C ChrisW67

        You cannot use a placeholder and bind variable in place of the table name, only for binding values to columns.

        Something like this:

        QString strQuery = QString(
          "create table if not exists profile_%1_properties name TEXT, type TEXT, display_item TEXT"
         ).arg(value.at(2).toString());
        query.exec(strQuery);
        

        but only if you are 100% certain the value in value.at(2) cannot be subverted by the user (security risk).

        iwoitheI Offline
        iwoitheI Offline
        iwoithe
        wrote on last edited by
        #3

        @ChrisW67 It works. Thank you. :)

        but only if you are 100% certain the value in value.at(2) cannot be subverted by the user (security risk).

        The id (value.at(2)) is set in C++ and there is no way for the user to adjust this value.

        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