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.
Related
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.
I am trying to use Qt to query a table in an MS Access database with a QSqlQuery. I am able to query all tables, except for one. The one table returns the error:
[Microsoft][ODBC Driver Manager] Function sequence error
Here is the code I use to query the table.
QSqlQueryModel *tempModel = new QSqlQueryModel();
QSqlQuery *qry = new QSqlQuery();
qry->prepare("SELECT * FROM table_name;");
qry->exec();
tempModel->setQuery(*qry);
while(tempModel->canFetchMore())
{
tempModel->fetchMore();
}
I've tried the answer from this SO question, but no change.
QSqlQuery causing ODBC Function sequence error
I too encountered issue with DAO connector to MySql backend. My passthrough queries were working, but attempt to read from the table using DAO were receiving ODBC function sequence error. The recordset connector was fine - no problem with move.first, move.last, record count, numerating the field names. Program failed when attempting to read record data -- but again, no problem was happening with my passthrough queries.
Issue was easy to resolve. I forgot to refresh my ODBC link after making a table schema changes. Refreshed the link.. and now everything working normal again.
To simplify my life, I added a program link for end-users to automatically refresh the ODBC links.
The issue seemed to be with the Date/Time datatype of one of the columns.
One of my columns had a data type of "Date/Time" with a property of "IME Sentence Mode" set to "Phrase Predict".
Changing this from "Phrase Predict" to "None" allowed me to query the MS Access table from my Qt application.
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.
I have been searching for a while on how to get the generated auto-increment ID from an "INSERT . INTO ... (...) VALUES (...)". Even on stackoverflow, I only find the answer of using a "SELECT LAST_INSERT_ID()" in a subsequent query. I find this solution unsatisfactory for a number of reasons:
1) This will effectively double the queries sent to the database, especially since it is mostly handling inserts.
2) What will happen if more than one thread access the database at the same time? What if more than one application accesses the database at the same time? It seems to me the values are bound to become erroneous.
It's hard for me to believe that the MySQL C++ Connector wouldn't offer the feature that the Java Connector as well as the PHP Connector offer.
An example taken from http://forums.mysql.com/read.php?167,294960,295250
sql::Statement* stmt = conn->createStatement();
sql::ResultSet* res = stmt->executeQuery("SELECT ##identity AS id");
res->next();
my_ulong retVal = res->getInt64("id");
In nutshell, if your ID column is not an auto_increment column then you can as well use
SELECT ##identity AS id
EDIT:
Not sure what do you mean by second query/round trip. First I thought you are trying to know a different way to get the ID of the last inserted row but it looks like you are more interested in knowing whether you can save the round trip or not?
If that's the case, then I am completely agree with #WhozCraig; you can punch in both your queries in a single statement like inser into tab value ....;select last_inserted_id() which will be a single call
OR
you can have stored procedure like below to do the same and save the round trip
create procedure myproc
as
begin
insert into mytab values ...;
select last_inserted_id();
end
Let me know if this is not what you are trying to achieve.
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 ...