QSqlQuery - Prepare crash with "counter counter mismatch"

The following code creates a table and populates the database with data. When I run the code below, I get the error message 'Parameter count mismatch' on query.exec() . However, if I use a commented line, everything works fine. How do I abuse the training feature?

 QSqlQuery query; if (query.exec("CREATE TABLE Users ( u_ID INT IDENTITY(1,1) PRIMARY KEY, cName varchar(25) UNIQUE )")) { for (int i=1;i<=100;++i) { QString uName; uName = "user" + QString::number(i); query.prepare("INSERT INTO Users (cName) VALUES(':UNAME')"); query.bindValue(":UNAME",uName); query.exec(); //query.exec("INSERT INTO Users (cName) VALUES('" + uName + "')"); if (query.lastError().isValid()){ QMessageBox::information(0,"ERR!",query.lastError().text()); } } } 
+4
source share
1 answer

Just use:

 query.prepare("INSERT INTO Users (cName) VALUES(:UNAME)"); 

i.e. do not specify bind variables.

+5
source

Source: https://habr.com/ru/post/1389929/


All Articles