From 00ae461dff2662adebf5408b43bcdd4b82bd1a15 Mon Sep 17 00:00:00 2001 From: Peinthor Rene Date: Thu, 10 Jul 2014 00:31:45 +0200 Subject: [PATCH] add a getRow function to retrieve rowdata by rowid --- src/sqlitedb.cpp | 21 +++++++++++++++++++++ src/sqlitedb.h | 11 +++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 88bc385b..6b40fe6b 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -428,6 +428,27 @@ bool DBBrowserDB::executeMultiSQL(const QString& statement, bool dirty, bool log return true; } +bool DBBrowserDB::getRow(const QString& sTableName, int rowid, QList& rowdata) +{ + QString sQuery = QString("SELECT * from %1 WHERE rowid=%2;").arg(sTableName).arg(rowid); + QByteArray utf8Query = sQuery.toUtf8(); + sqlite3_stmt *stmt; + + int status = sqlite3_prepare_v2(_db, utf8Query, utf8Query.size(), &stmt, NULL); + if(SQLITE_OK == status) + { + // even this is a while loop, the statement should always only return 1 row + while(sqlite3_step(stmt) == SQLITE_ROW) + { + for (int i = 0; i < sqlite3_column_count(stmt); ++i) + rowdata.append(QByteArray(static_cast(sqlite3_column_blob(stmt, i)), sqlite3_column_bytes(stmt, i))); + } + } + sqlite3_finalize(stmt); + + return status == SQLITE_OK; +} + int DBBrowserDB::addRecord(const QString& sTableName) { char *errmsg; diff --git a/src/sqlitedb.h b/src/sqlitedb.h index 27cf92ba..814c85e1 100644 --- a/src/sqlitedb.h +++ b/src/sqlitedb.h @@ -5,6 +5,7 @@ #include #include +#include class MainWindow; class sqlite3; @@ -64,6 +65,16 @@ public: bool executeSQL ( const QString & statement, bool dirtyDB=true, bool logsql=true); bool executeMultiSQL(const QString& statement, bool dirty = true, bool log = false); + /** + * @brief getRow Executes a sqlite statement to get the rowdata(columns) + * for the given rowid. + * @param sTableName Table to query. + * @param rowid The rowid to fetch. + * @param rowdata A list of QByteArray containing the row data. + * @return true if statement execution was ok, else false. + */ + bool getRow(const QString& sTableName, int rowid, QList& rowdata); + void updateSchema(); int addRecord(const QString& sTableName); bool deleteRecord(const QString& table, int rowid);