I am using Qt GUI to save data in MySQL using C++.
I am using QDateTimeEdit widget for this. Whatever the value changed by user in GUI of QDateTimeEdit, it should be inserted in MySQL.
Can anyone tell me how to do that?
How to access value from QDateTimeEdit and converting it in proper format like QString and using MySQL query inserting it into database?
An alternative is not to convert it to a QString but let the driver do that for you. If you expect some precision in the conversion some cases this might be better, other cases it can be worse:
QDateTime date = ui->dateTimeEdit->dateTime();
QSqlQuery query(myDatabase);
query.prepare("INSERT INTO my_table (id, date) "
" VALUES (:id, :date)");
query.bindValue(":id", 1001);
query.bindValue(":date", date);
query.exec();
The QSqlQuery::bindValue() function will take the QDateTime and pass it through as a QVariant and then the driver should know how to convert a QVariant::DateTime to the correct string that the database understands.
About second part "how to access value":
You somehow in code created object of QDateTimeEdit and place it on some layout. Typically it will be pointer with name for example mpDTPicker.
QDateTimeEdit * mpDTPicker = new QDateTimeEdit();
//place mpDTPicker on layout.
For access current time we need use method dateTime:
//User actions with date -> emitted signal -> execute slot with our logic
{
QDateTime momentum = mpDTPicker->dateTime();
// So here we need convert QDateTime to QString and we will use [toString Method](http://doc.qt.io/qt-4.8/qdatetime.html#toString)
QString result_string = momentum.toString("dd:mm:yy");
QDebug() << result_string;
}
So that is all about converting QDateTime to QString.
About first part of Question how to get that user changed value of DateTimeEdit is total another question.
And about third part how to store it in mysql database everything depended on structure of your table. But typicaly it can be solved with simple query:
QSqlQuery query;
QString mQuerry = "INSERT INTO mytable (id, date) VALUES (0, \"" +result_string "\" )";
query.exec(mQuerry);
And please READ DOCS especial when them so cool :)
Related
I've already implemented a QTableView + QStandardItemModel in Qt5. At the beginning, I set the date data just as a string with the date format based on the application setting. For instance, it can be the US format like MM/dd/yyyy or the european format dd.MM.yyyy. The data comes from a json file with the european date format. My first implementation was like this:
shared_ptr<QStandardItemModel> _model;
// detect a date string with regex, get the submatches and create a QDate object from it
QDate date(stoi(submatches[3].str()), stoi(submatches[2].str()), stoi(submatches[1].str()));
QModelIndex index = _model->index(rowPos, colPos, QModelIndex());
// depends on the setting, the date can be shown on the table like this
_model->setData(index, QString(date.toString("dd.MM.yyyy"));
// activate the column sorting in the QTableView
ui->tableView->setSortingEnabled(true);
This implementation, however, cannot sort the date column correctly. The reason is because the QTableView sorts the column just like a string (sorted by day isntead of by year first) instead of a date entry.
I can change the implementation by setting the data directly with the date object:
_model->setData(index, date);
The sorting works perfectly by date. But, the format is now always shown in dd/MM/yyyy format.
How can I keep this sorting function, but change the date view depends on the date format setting?
I've read that it may be implemented using a custom subclass of QAbstractTableModel. How about implementing as a SubClass of QTableView? Or may be with a subclass of QAbstractItemModel like in here? I'm not an expert yet to implement and integrate a Qt5 subclass.
The solution is to pass the QDate as data to the model, and use a delegate to set as shown in the view:
_model->setData(index, date);
class DateDelegate: public QStyledItemDelegate{
public:
using QStyledItemDelegate::QStyledItemDelegate;
QString displayText(const QVariant &value, const QLocale &locale) const{
return locale.toString(value.toDate(), "dd.MM.yyyy");
}
};
ui->tableView->setItemDelegateForColumn(col_date, new DateDelegate);
This is my first post in stack-overflow, so sorry in advance for possible "bad practices".
Context: the goal is to send SPARQL queries through http-requests to a GraphDB data base.
Problem: to construct the queries on code in a safe way. Currently done by means of std::string dummyStr = "Hello"+" World" or dummyStr.append("bla") (I was told this was not safe due to XSS, but thats not the issue here)
Question: do you know any query builder library for doing this string concatenation?
A search for C++ query builder on the web returned this answer.
After implementing the approach with the suggested Qt QSqlQuery class, I'm able to ".prepare" the query, but not to ".bindValue".
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.open();
QSqlQuery startQuery;
/*Prepare query*/
startQuery.prepare("INSERT INTO person (id, forename, surname) "
"VALUES (:id, :forename, :surname)");
startQuery.bindValue(":id", 1001);
startQuery.bindValue(":forename", "Bart");
startQuery.bindValue(":surname", "Simpson");
/*convert query to std::string*/
QString startQueryString = startQuery.lastQuery();
std::string dummyQuery = startQueryString.toUtf8().constData();
Why can't I bind the values to the placeholders?
Is it because I have no "actual" database, but rather a dummy-database just to construct the query?
My actual Query looks something like this:
SELECT ?s WHERE { FILTER(STRSTARTS(STR(?s),":referenceIRI")). ?s rdf:type rdfs:Class.}
And I would like to treat :referenceIRI as a placeholder.
I've searched overall to try to overcome this problem, as I just need the query-builder functionality.
Also: on my actual SPARQL query I have both ? and :myVal elements, which are the 2 types of placeholders in QSqlQuery for binding values. Any idea on how to by-pass the ? placeholder and just consider the :myVal-type?
I am trying to fetch some records from MySQL database by using a prepared statement by using QSqlQuery as:
QString username=ui->textEdit_password->toPlainText();
QString password=ui->textEdit_password->toPlainText();
QSqlQuery query;
query.prepare("SELECT * FROM login_access WHERE username=? AND password=?");
query.addBindValue(username);
query.addBindValue(password);
query.exec();`
When i run :
std::string q_str1=query.executedQuery().toUtf8().constData();
std::cout<<"Query : "<<q_str1<<"\n";
It outputs : Query : SELECT * FROM login_access WHERE username=? AND password=? where the "?" has not been replaced and the query returns nothing since the "?" character is compared to the database records.
On running the query: SELECT * FROM login_access, the query returns all the database records in the login_access table.
I have also tried replacing the "?" with placeholders ":uname",":pass" and changed query.addBindValue(username); to query.bindValue(":uname",username);, and done same with password field.
I am running QtCreator 4.4.1
Thanks.
Use query.bindValue( ...) because this sets the placeholder value.
I tested executedQuery() on one of my SQL statements with placeholders and it returned a string with just the placeholders, not the values. The documentation does say that in most cases it the same string as lastQuery().
http://doc.qt.io/qt-5/qsqlquery.html#executedQuery
You have confirmed that your SQL statement without the where clause works so the next stage is to check you are binding what you think you are binding. To do this use boundValue(const QString placeholder) to find out if the placehold value is being bound.
It might also be useful to check the query has run OK.
So, after your query.exec you should put the following (assuming these are your placeholders) just to check these things:
qDebug() << query.lasterError();
qDebug() << query.boundValue(":uname");
qDebug() << query.boundValue(":pass");
i am using Qt and i have written c++ code,i have already connected with sqlite database.i want to insert name in database
std::string name="Hello";
qry.prepare( "INSERT INTO s_no (Name,Status) VALUES ('name','1' )");
if( !qry.exec() )
qDebug() << qry.lastError();
else
qDebug( "Inserted!" );
but in db i am finding name only , not hello;
please help me..thank you so much in advance
C++ and SQL are two different programming languages, and execute in different environments. This means that C++ objects are not visible in SQL.
In theory, it would be possible to construct the string containing the SQL command so that the value of the name variable is inserted directly into it:
qry.prepare("INSERT INTO s_no (Name,Status) VALUES('" + name + "', '1')"); // don't do this
However, this will blow up if the name contains a quote. Escaping quotes would be possible with additional code, but a better way of getting variable values into an SQL query is to use parameters:
qry.prepare("INSERT INTO s_no (Name,Status) VALUES(?, '1')");
qry.bindValue(0, name);
(This is the only sensible way of using blob values in a query.)
Try making name a QString. You can change std::string to QString by using: QString name2 = QString::fromStdString(name);. Don't forget to include: #include <QString>.
I have a QDateEdit in my GUI from which I convert the QDate to QString and add it to my database. The QString date is saved in the database in this format: 20/12/2015.
In case a user want to edit the date, then I need to show the date on the QDateEdit field on the GUI again. Hence, I need to fetch the database, bring back the date (which is in QString format) and convert it to QDate back again in order to put it on the QDateEdit field on the GUI.
However, I cannot manage to convert that QString format (i.e.: 20/12/2015) to QDate using the following:
QString date_string_on_db = "20/12/2015";
QDate Date;
Date.fromString(date_string_on_db,"dd/MM/YYYY");
The Date is always returning invalid.
what should I do ?
First of all, the format string should be dd/MM/yyyy. Qt documentation for the QDate class says that yyyy is recognized as a four digit year number.
Second of all, fromString is a static function that returns a new QDate. Currently, the return value of that function is discarded : it is not written back into the Date variable, as you might think. The complete correct code should therefore look like this :
QString date_string_on_db = "20/12/2015";
QDate Date = QDate::fromString(date_string_on_db,"dd/MM/yyyy");