QT / SQLite query placeholder does not work with WHERE clause - c++

I have a QT application that connects to a sqlite database. To build the queries I use the prepare and bindValue methods. I am able to get this working in an INSERT query. However these methods no longer work when the placeholder is in a WHERE clause.
QSqlQuery query(this->database);
query.prepare("DELETE FROM `Shops` WHERE `id`=:id;");
query.bindValue(":id", this->id);
query.exec();
qDebug() << query.executedQuery();
When I execute this code, the following line will be printed into the debug console:
"DELETE FROM `Shops` WHERE `id`=:id;"
I tried the same with an UPDATE ... WHERE ...=:id query and it didn't worked. In an INSERT ... INTO ... VALUES (:name) it worked however.
I already found out, that it is not possible to use placeholders for columns or table names in qt. But I could not found anything about restrictions with WHERE clauses. Does anyone has an idea?
Ps: I know, that I could just concatenate the string. But I am curious, why it does not work.

Related

QtSql Not Showing Query Results Anywhere

My SQL results were showing on Label but then I decided to drop that specific and made a new one.
Then I changed the query to be executed but nothing shows up anymore!
I have been debugging for the past 2 hours to no avail.
I used boolean to check if the query was executed and it showed '1' however, when I print the query.value, it shows nothing.
QSqlQuery query;
query.exec("SELECT * FROM artists;");
QString name = query.value(0).toString();
ui->label_2->setText(name);
I can also confirm that my database is connected since it shows '1' on if (db.open()).
I have imported sql in the .pro file, obviously.
I use mariadb => QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
Could it be a case of permission denied?
As drescherjm pointed out, query.next() must be used to move the pointer of query forward.

Error with Sqlite Insert operation: near"s": syntax error unable to execute statement [duplicate]

I want to generate SQL queries from user input for inserting some data into a database.
The user may input anything. Is there a way in Qt to convert such user inputs in string-type values fields?
"The user may input anything."
That doesn't give us much to go by, but I can give you an example of how I would set up a basic insert query.
// I assume you already have a QSqlDatabase object called 'db'
QSqlQuery query(db);
QString s = "INSERT INTO table (colA, colB) VALUES (:valA, :valB);"
query.prepare(s);
// You only need to prepare the query once
// To actually insert values into colA & colB, do this:
query.bindValue(":valA", QString("stuff to put in colA"));
query.bindValue(":valB", QString("other stuff for colB"));
query.exec();
query.finish(); // you probably don't even need this
The bindValue method takes a QVariant as its second argument (I used strings in my example, but you could use anything supported by the variant type). You just have to make sure the type of the values makes sense for the relevant columns in your database.
Also, I'm using the syntax from PostgreSQL for my example. I think it's standard, but you may need to change the parameter binding (the :valA :valB stuff) to match what your db engine expects.

Qt SQL Pattern Matching

I am working on a C++ Qt program where I include a mysql database.
I want to do some sql queries and show the output on a tableview. Because I also want to filter the results I wrote this kind of code snippet:
query->prepare("SELECT * FROM contacts WHERE contactsName LIKE '%:name%';");
query->bindValue(":name", mUi->searchContactsLine->text());
query->exec();
mModel->setQuery(*query);
mUi->tableContacts->setModel(mModel);
... where
searchContactsLine
is the inputlinebox to search for a string,
mModel
is my QSqlQueryModel and
tableContacts
is my TableView in qt.
As you can see I want to use pattern matching with the dollar sign. But if I run the program I got no row back.. no matter what I am searching for.
In the past I had problems with the inverted commas ('...') while using the bindValue function, but if I delete it from the sql command I get an sql syntax error.
So what do I wrong? Maybe you have an idea of that.

Qt QSqlQuery bindValue works with ? but not with :placeholders

I'm working with SQLite, doing insert into table. Folowwing
QSqlQuery testQuery(QString("INSERT INTO test(testcol) VALUES(?)"));
testQuery.bindValue(0, someQStringObg);
testQuery.exec();
works, but
QSqlQuery testQuery(QString("INSERT INTO test(testcol) VALUES(:val)"));
testQuery.bindValue(":val", someQStringObg);
testQuery.exec();
don't. testQuery.lastError().text() returns No query Unable to fetch row
Have no clue why things are that way, but really want to find out.
Please use prepare as the official example:
QSqlQuery testQuery;
testQuery.prepare("INSERT INTO test(testcol) VALUES(:val)");
testQuery.bindValue(":val", someQStringObj);
testQuery.exec();
The reason for the error is that the query was executed before binding to the corresponding placeholder. You can see the relevant part of the constructor documentation:
If query is not an empty string, it will be executed.

QSqlQuery UPDATE/INSERT DateTime with server's time (eg CURRENT_TIMESTAMP)

I am using QSqlQuery to insert data into a MySQL database. Currently all I care about is getting this to work with MySQL, but ideally I'd like to keep this as platform-independent as possible.
What I'm after, in the context of MySQL, is to end up with code that effectively executes something like the following query:
UPDATE table SET time_field=CURRENT_TIMESTAMP() WHERE id='5'
The following code is what I have attempted, but it fails:
QSqlQuery query;
query.prepare("INSERT INTO table SET time_field=? WHERE id=?");
query.addBindValue("CURRENT_TIMESTAMP()");
query.addBindValue(5);
query.exec();
The error I get is: Incorrect datetime value: 'CURRENT_TIMESTAMP()' for column 'time_field' at row 1 QMYSQL3: Unable to execute statement. I am not surprised as I assume Qt is doing some type checking when it binds values.
I have dug through the Qt documentation as well as I know how, but I can't find anything in the API designed specifically for supporting MySQL's CURRENT_TIMESTAMP() function, or that of any other DBMS.
Any suggestions?
I have no SQL server to test here, but addBindValue() binds Qt's data types. You should put the timestamp function direct into the query
QSqlQuery query;
query.prepare("INSERT INTO table SET time_field=CURRENT_TIMESTAMP() WHERE id=?");
query.addBindValue(5);
query.exec();
should do the trick.
I don't see a way to get the database time command. I think it would be in QSqlDriver as it is specific to the database.
The only way I can think of :
QString timeCommand("CURRENT_TIMESTAMP()");
query.prepare(QString("INSERT INTO table SET time_field=%1 WHERE id=?").arg(timeCommand));
Edit : In fact, I'm not sure CURRENT_TIMESTAMP() is DB specific ...