Results 1 to 5 of 5

Thread: Variable table name problem

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Variable table name problem

    Has anyone successfully created a Sqlite table using a variable for a table name?

    Like this:

    Qt Code:
    1. void createTable(QString tableName) {
    2. query.prepare("Create table ? (col1, col2)");
    3. query.addBindValue(tableName);
    4. query.exec();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Also tried :bind notation, same error: parameter count mismatch.

    Also tried creating the table with a dummy name then renaming like this:

    Qt Code:
    1. void createTable(QString name) {
    2. query.prepare("CREATE table tablename (col1, col2)");
    3. query.exec();
    4. query.prepare("ALTER table tablename rename to ?");
    5. query.addBindValue(name);
    6. query.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Still the parameter count mismatch error.

    Any ideas how to do this?

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Variable table name problem

    I usually do the following for variables in a query:
    Qt Code:
    1. void createTable(QString tableName) {
    2. query.prepare("Create table "+tableName+" (col1, col2)");
    3. query.exec();
    4. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to norobro for this useful post:

    waynew (12th March 2010)

  4. #3
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Variable table name problem

    Thanks Norobro, I would never have thought of that approach, but it works great.

  5. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Variable table name problem

    You're welcome.

    Is that your assistant in your avatar? I have an assistant that is in a box by my monitor whenever I am on my computer.

  6. #5
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Variable table name problem

    Glad to hear it. They are great companions. You can check all three of ours at www.k4elo.net
    Thanks again for your help.

Similar Threads

  1. Replies: 1
    Last Post: 30th November 2007, 10:03
  2. I have a problem to modify table
    By Abk in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2007, 20:11
  3. Table Column Problem
    By kenny_isles in forum Newbie
    Replies: 1
    Last Post: 5th March 2007, 23:35
  4. Relational Table Model Problem
    By aekilic in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2007, 14:57
  5. Table Shortcuts Problem
    By ankurjain in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2006, 08:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.