PDA

View Full Version : Variable table name problem



waynew
12th March 2010, 00:10
Has anyone successfully created a Sqlite table using a variable for a table name?

Like this:



void createTable(QString tableName) {
query.prepare("Create table ? (col1, col2)");
query.addBindValue(tableName);
query.exec();
}


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

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



void createTable(QString name) {
query.prepare("CREATE table tablename (col1, col2)");
query.exec();
query.prepare("ALTER table tablename rename to ?");
query.addBindValue(name);
query.exec();
}


Still the parameter count mismatch error.

Any ideas how to do this?

norobro
12th March 2010, 01:42
I usually do the following for variables in a query:
void createTable(QString tableName) {
query.prepare("Create table "+tableName+" (col1, col2)");
query.exec();
}

waynew
12th March 2010, 01:51
Thanks Norobro, I would never have thought of that approach, but it works great.

norobro
12th March 2010, 01:54
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.

waynew
12th March 2010, 02:41
Glad to hear it. They are great companions. You can check all three of ours at www.k4elo.net
Thanks again for your help.