Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QtSql>
- #define DEBUG() qDebug() << Q_FUNC_INFO << __LINE__
- #define WARNING() qWarning() << Q_FUNC_INFO << __LINE__
- bool exec(QSqlQuery &query)
- {
- if (!query.exec()) {
- WARNING() << query.executedQuery() << query.lastError();
- return false;
- }
- return true;
- }
- int main(int argc, char **argv)
- {
- QCoreApplication app(argc, argv);
- QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
- db.setDatabaseName(":memory:");
- if (!db.open()) {
- WARNING() << db.lastError();
- return -1;
- }
- {
- QSqlQuery query(db);
- query.prepare("CREATE TABLE IF NOT EXISTS Chat (key INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT)");
- if (!exec(query)) {
- WARNING();
- return -1;
- }
- }
- {
- QSqlQuery query(db);
- query.prepare("INSERT INTO Chat(value) VALUES(?)");
- query.addBindValue("Qt Quick");
- if (!exec(query)) {
- WARNING();
- return -1;
- }
- }
- {
- QSqlQuery query(db);
- query.prepare("SELECT COUNT(key) FROM Chat WHERE value LIKE ?");
- query.addBindValue("%Qt%");
- if (!exec(query)) {
- WARNING();
- return -1;
- }
- while (query.next()) {
- DEBUG() << query.value(0).toInt() << "found";
- }
- }
- db.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement