Qt Parameter Count Mismatch with QSQL and Bind Values - c++

I'm currently facing an error where I get "Parameter count mismatch" from query.lastError, my bindvalues are correct (i've tested them).
My query is:
QSqlQuery query(DBT);
query.prepare("SELECT Foto, Nombre, Apellido1, Apellido2, Curso, Grupo, FotoHuella FROM usuarios WHERE Nombre=:nombre1 OR Apellido1=:apellido1 OR Apellido2=:apellido2 OR Curso=:curso1 OR Grupo=:grupo1");
query.bindValue(":nombre1", nombre, QSql::Out);
query.bindValue(":apellido1", apellido1, QSql::Out);
query.bindValue(":apellido2", apellido2, QSql::Out);
query.bindValue(":curso1", curso, QSql::Out);
query.bindValue(":grupo1", grupo, QSql::Out);
query.exec();
In case you where wondering here is where I set up the database:
QSqlDatabase DBT=QSqlDatabase::addDatabase("QSQLITE");
DBT.setDatabaseName("/home/pi/FoodCircleDBT.db");
DBT.open();
Thanks in advance.

Solution for me: actually everything relating to the code worked perfectly but somehow my database schema went crazy when I edited it with a database program. So If you are facing this I recommend you to open the command line and checking your schema.

Related

how to pass a uers input from a lineEdit into a SQL Query QT 6.0 c++

Hello I have tried every forum and resource i have and can still not find the answer to my question.
I am making a booking management system. One of the features is the admin can search someones email and get their details from the database. So i put a line edit so i can get the QString from the email and then i tried to parse it into a SQL query. Thought this would be easy but QT said no. so i am at a complete loss.
Any help will be much appreciated.
The code:
QString email = ui->lineEdit_custsearch->text();
QSqlQuery qry;
QString dets = "SELECT firstname FROM customer WHERE email=="+email+"";
if (qry.exec(dets))
{
for (int i = 0;qry.next();i++)
{
ui->lineEdit_first->setText(qry.value(0).toString());
}
}
In SQL you only need to use a single '=', sign, might be worth giving that a try?
SELECT firstname FROM customer WHERE email="+email+"
Just a suggestion, to make it more readable, it might be worth using string interpolation too so something like the below:
string email = '';
QString dets = $"SELECT firstname FROM customer WHERE email='{email}'";

not positioned on a valid record

Im New in Qt
I have problem in my app.
This is my code:
voidDialog::on_tableView_activated(constQModelIndex&index)
{
QString valu=ui->tableView->model()->data(index).toString();
QSqlQuery qryview;
qryview.prepare("SELECT* FROM employ where nomper='"+valu+"' or prenomper='"+valu+"'");
ui->lineEdit_2->setText(qryview.value(0).toString());
ui->lineEdit_3->setText(qryview.value(1).toString());
}
I want to recuperate my data in QLineEdit.
When I use SQLite, there is no problem.
But when I use MySQL no result shows in lineEdit.
qsqlquery::value: not positioned on a valid record.
The problem is that you are trying to retrieve the row without initializing it. The right way to do this is by using query.next(), below is the code example:
QString valu=ui->tableView->model()->data(index).toString();
QSqlQuery qryview;
qryview.prepare("SELECT* FROM employ where nomper='"+valu+"' or prenomper='"+valu+"'");
qryview.exec();
while(qryview.next()) {
ui->lineEdit_2->setText(qryview.value(0).toString());
ui->lineEdit_3->setText(qryview.value(1).toString());
}
This should work, if not try to print the lastQueryError and see if query is executed coreectly.

QSqlQuery causing ODBC Function sequence error

I searched SO and Google but didn't find much help on this question. It seems due to ODBC functions being called out out of order. But since I am using QSql that wraps ODBC, it is hard for me to track down the function. Please help...
I was able to connect to the sql server database
I tested a very simply query and still got the error. I don't think it's due to column binding.
I was able to run the query with sql server, so I think the sql query is ok.
The tools I am using:
VS c++ 2017, CMake, Qt 5.09.2, sql server 2017
Below are the error messages:
QODBCResult::exec: Unable to execute statement: "[Microsoft][ODBC Driver Manager] Function sequence error"
QSqlError("0", "QODBC3: Unable to execute statement", "[Microsoft][ODBC Driver Manager] Function sequence error")
Test coding:
This coding generate the error message above.
int main()
{
QSqlDatabase GUIInpDB = QSqlDatabase::addDatabase("QODBC", "MainSQLDB");
GUIInpDB.setConnectOptions();
QString inpSqlServer = "DESKTOP-085AEA8\\SQLEXPRESS";
QString dbName = "test";
QString connString = QString("Driver={ODBC Driver 13 for SQL Server};Server=%1;DATABASE=%2;Trusted_Connection=Yes;")
.arg(inpSqlServer).arg(dbName); //the argument replace the %1 and %2
GUIInpDB.setDatabaseName(connString);
QSqlDatabase db = QSqlDatabase::database("MainSQLDB");
if (!db.open())
{
qDebug() << "Connection for db not working";
return 1;
}
QSqlQuery query("SELECT * FROM TBL.tbl_test", db);
if (!query.exec())
qDebug() << query.lastError();
int num_of_rows = query.size();
getchar();
return 0;
}
I found this discussion on QtCenter that might help you, even if I am doubtful about why it fixed the issue
You migth try to use the QSqlQuery constructor that does not exec as mentionned
http://www.qtcentre.org/threads/18832-ODBC-function-sequence-error

Can't retrieve identity field after insert in sql database

Using C++ Qt framework and sql server 2008 I've been trying to insert a record into a table with an identity field and retrieve the identity value. Below is a simplified code sample which actually does do the insert but just doesn't retreive the identity. The identity returned from query.value(0) is an invalid QVariant.
QSqlQuery query(*pConn);
query.prepare("insert into [VH_MANUFACTURER] values ('sRRS test man code','RRS type','RRS logo file',1,'RRS SEO para','RRS description','RS');"
"select SCOPE_IDENTITY();");
if(query.exec())
{
if(query.next())
{
QVariant identity = query.value(0);
int id=identity.toInt();
}
}
I've tried using select ##identity instead of scope_identity with no improvement and also QSqlQuery .lastInsertId() which also returns an invalid QVariant, see below.
bool bFeature = pConn->driver()->hasFeature(QSqlDriver::LastInsertId);
QSqlQuery query(*pConn);
query.prepare("insert into [VH_MANUFACTURER] ([MFG_NAME],[MFG_TYPE],[MFG_LOGO],[MFG_ACTIVE],[MFG_SEO_CONTENT],[MFG_DESCRI],[MFG_CAPMANCODE]) values ('sRRS test man code','RRS type','RRS logo file',1,'RRS SEO para','RRS description','RS')");
if(query.exec())
{
QVariant id=query.lastInsertId();
}
hasFeature returns true, so the driver is supposed to support what I'm trying to do.
Just to test the sql , I ran the sql directly through Sql Server Management Studio and it inserts as expected and returns the identity value correctly.
Finally found a work around using OUTPUT clause in sql. I don't exactly know why the other methods I tried don't work. There is a sql server bug associated with this feature but that doesn't explain why it worked in ssms but not in c++ Qt code. The sample below show's the work around. Here's the reference I used to solve this.
bool bFeature = pConn->driver()->hasFeature(QSqlDriver::LastInsertId);
QSqlQuery query(*pConn);
query.prepare("insert into [VH_MANUFACTURER] ([MFG_NAME],[MFG_TYPE],[MFG_LOGO],[MFG_ACTIVE],[MFG_SEO_CONTENT],[MFG_DESCRI],[MFG_CAPMANCODE]) OUTPUT INSERTED.MFG_ID values ('sRRS test man code','RRS type','RRS logo file',1,'RRS SEO para','RRS description','RS')");
if(query.exec())
{
if(query.next())
{
QVariant id=query.value("MFG_ID");
}
}

Qt and SQLite, error when executing query, without error message

I am using Qt 5.4 and SQLite to to database operations, all other requests work, but this one doesn't seem to work and gives no error.
QSqlQuery query(Globals::db);
QString cmd = "Update VideoFile set isVisible = 0 WHERE Id =1;";
if(query.prepare(cmd))
qDebug("Prepare success..."); //<-- prints out
if (!query.exec()) {
qDebug("Error occurred querying.");
qDebug("%s.", qPrintable(Globals::db.lastError().text())); //<<-- prints out blank
}
Tried so far:
o) Database: exists!
o) Query Prepare() yields True
o) When executing the same Statement in SQLite Browser. Works!
i read about a problem here:, that some databases have a certain delay.
"Portability note: Some databases choose to delay preparing a query until it is executed the first time. In this case, preparing a syntactically wrong query succeeds, but every consecutive exec() will fail." But apparently, the query is not syntactically wrong.