Solved QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite)
-
I am using Sqlite on my app where I need to delete from a table where the primary key is a obscured CHAR that created with
QByteArray::toBase64
void insertIntoTable(QString key) { QSqlQuery query; query.prepare("INSERT INTO download (key) VALUES(:key)"); QByteArray obscuredStr = key.toUtf8(); obscuredStr = obscuredStr.toBase64(); query.bindValue(":key", obscuredStr); }
void DatabaseManager::deleteFromTable(QString table, QString columnName, QString valueToCompareAgainst) { QSqlQuery query; QByteArray obscuredStr = valueToCompareAgainst.toUtf8(); obscuredStr = obscuredStr.toBase64(); if( ! query.exec("DELETE FROM " + table + " WHERE " + columnName + "= '" + obscuredStr+ "'" )) { LOG_ERROR("SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text()); qDebug() << "SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text(); } }
The code runs fine at the insertion but it can't seem to delete data from the table. It doesn't return any error. It just won't delete. If I I don't use the pseudo-encryption with
toBase64
, which turns the stament a lot less long, it deletes that data.
I have not idea what I am might be missing. -
@hbatalha Please first take a look at documentation to see how to properly construct SQL queries in Qt: https://doc.qt.io/qt-5/qsqlquery.html
Hint: use prepare(). -
-
Change your
DELETE
statement to be same with key but as aSELECT
instead. That eliminates whether the issue has anything to do with deleting. -
Use the returned column from this
SELECT
statement to compare against thevalueToCompareAgainst
and/or the originalkey
. Try to see if you can see a difference there. -
Instead of
'" + obscuredStr+ "'"
try passing that as a bound variable to theDELETE
statement, as you pass it into theINSERT
statement.
In your
INSERT
statement you do not actuallyexec()
anything. So if that is your real code you never put the item into the table. Which might explain why you can't delete it..... -
-
@jsulm said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):
Hint: use prepare().
I have tried this :
query.prepare("DELETE FROM " + table + " WRERE " + columnName + "= '(:key)'"); query.bindValue(":key", valueToCompareAgainst); // query.exec(); if (! query.exec()) { LOG_ERROR("SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text()); qDebug() << "SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text(); }
Gives me the error : No query Unable to fetch row
-
Use the returned column from this SELECT statement to compare against the valueToCompareAgainst and/or the original key
like this?
QSqlQuery query; query.prepare("DELETE FROM " + table + " WRERE " + columnName + "= ?"); query.addBindValue(valueToCompareAgainst); query.exec(); qDebug() << (valueToCompareAgainst == query.boundValue(0).toString()); // returns true
It returns true when comaparing them. But I can't pass it to DELETE, can't figure out how.
In your INSERT statement you do not actually exec() anything. So if that is your real code you never put the item into the table. Which might explain why you can't delete it.....
No it is not my real code. I forgot about it when type that minimal example.
-
@hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):
WRERE
I think this should be
WHERE
;) -
@KroMignon oops, I mistyped. But still it doesn't delete from the table.
If I don't obscure the string it deletes fine. It does look if the statement lenght is less long it works. -
Hi,
Print the query that was generated to see if there's something wrong there.
-
QSqlQuery query; query.prepare("DELETE FROM " + table + " WHERE " + columnName + "= ?"); query.addBindValue(valueToCompareAgainst); query.exec(); qDebug() << query.lastQuery(); // returns "DELETE FROM tableName WHERE columnName= ?" qDebug() << (valueToCompareAgainst == query.boundValue(0).toString()); // returns true
The query generated seems to be ok.
-
Can you show it anyway ? Looking OK and being OK can be subtly different.
-
@hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):
Use the returned column from this SELECT statement to compare against the valueToCompareAgainst and/or the original key
like this?
No,
SELECT [columnname] FROM [table] WHERE [columnname] = '<value>`
If that doesn't return the row it's not surprising it can't be deleted.
SELECT [columnname] FROM [table]
Compare the column value you get back for the row you want to delete against what you are passing for its value to
DELETE FROM [table] WHERE [columnname] = '<value>'
.All just trying to help you to debug.
-
@SGaist I thought that is the way to do it. So how do I print the query that was generated ?
-
@JonB What about now?
QSqlQuery query; if(! query.exec("SELECT * FROM " + table + " WHERE " + columnName + "= '" + valueToCompareAgainst + "'")) { qDebug() << "SQL QUERY ERROR: " + table + " : " + query.lastError().text(); } while(query.next()) qDebug() << "RETURNED: " << query.value(columnName).toString();
This does not return anything. Might the error be here? What should I do?
All just trying to help you to debug.
I really appreciate it
-
@hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):
@SGaist I thought that is the way to do it. So how do I print the query that was generated ?
My bad ! I misread the code part.
One thing that might be wrong is the missing space between the column name and the question mark.
By the way, which version of Qt are you using ?
-
One thing that might be wrong is the missing space between the column name and the question mark.
Where? You mean the space between columnName and the '=' sign.
By the way, which version of Qt are you using ?
Qt Creator IDE version? If so, it is Qt 4.15.1
-
@hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):
Qt Creator IDE version? If so, it is Qt 4.15.1
No, Qt version.
QtCreator is an IDE, Qt is a C++ framework.
What you posted is the QtCreator version, question was which Qt version you're using. -
-
What qsql driver do you use? Can you reproduce it with a in-memory sqlite database so we can try to check it on our systems?
-
@Christian-Ehrlicher said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):
an you reproduce it with a in-memory sqlite database so we can try to check it on our systems?
Sorry, I don't know how to do that yet. I am a SQL newbie, just started learning it few weeks ago in college. I believe you mean something like this.
What qsql driver do you use?
QSQLITE
-
@hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):
Sorry, I don't know how to do that yet. I am a SQL newbie, just started learning it few weeks ago in college
Don't see why this hinders you from creating a minimal, compilable reproducer for us.