sql request that does not work
Solved
General and Desktop
-
Hello, everyone,
I want to make a sql query that can create a table without first knowing the name that this table should have; here is my code:int main(int argc, char** argv) { QCoreApplication app(argc,argv); QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("mydatabase"); if(db.open()){ qDebug("The connection is established with the database."); QSqlQuery q1; q1.prepare("CREATE TABLE :anonyme (id INT PRIMARY KEY NOT NULL, name VARCHAR(100) )"); q1.bindValue(":anonyme","MyTable"); if(q1.exec()) qDebug("Successful request."); else qDebug() << "ERREUR: " << q1.lastError().text(); }else{ qDebug() << " ERREUR: "; } return app.exec(); }
I don't know why my code doesn't work.
and here is the error message: ERREUR: " Parameter count mismatch"
Thank you for your help. -
I don't see a problem - you can create your statement with the correct table name...
http://doc.qt.io/qt-5/qstring.html#arg -
You can only bind parameters, not table names or anything else. Just put 'MyTable' directly in to query.
CREATE TABLE MyTable (id INT PRIMARY KEY NOT NULL, name VARCHAR(100) )
-
I don't see a problem - you can create your statement with the correct table name...
http://doc.qt.io/qt-5/qstring.html#arg