add a getRow function to retrieve rowdata by rowid

This commit is contained in:
Peinthor Rene
2014-07-10 00:31:45 +02:00
parent a6a735ed24
commit 00ae461dff
2 changed files with 32 additions and 0 deletions

View File

@@ -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<QByteArray>& 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<const char*>(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;

View File

@@ -5,6 +5,7 @@
#include <QStringList>
#include <QMultiMap>
#include <QByteArray>
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<QByteArray>& rowdata);
void updateSchema();
int addRecord(const QString& sTableName);
bool deleteRecord(const QString& table, int rowid);