From 94955c1db98d66451c7d63305e7abea8b4edef35 Mon Sep 17 00:00:00 2001 From: Peinthor Rene Date: Wed, 13 Mar 2013 18:09:40 +0100 Subject: [PATCH] add a function to return the create table statement for a given table --- src/sqlitedb.cpp | 26 ++++++++++++++++++++++++++ src/sqlitedb.h | 8 ++++++++ 2 files changed, 34 insertions(+) diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 81e42522..9bbda46a 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -1,6 +1,7 @@ #include "sqlitedb.h" #include "sqlbrowser_util.h" #include "MainWindow.h" + #include #include #include @@ -860,6 +861,31 @@ void DBBrowserDB::logSQL(const QString& statement, int msgtype) } } +QString DBBrowserDB::getTableSQL(const QString& sTable) +{ + sqlite3_stmt *vm; + const char *tail; + int err; + QString sTableSQL; + + QString statement = QString("SELECT sql FROM sqlite_master WHERE name='%1';").arg(sTable); + + QByteArray utf8Statement = statement.toUtf8(); + err=sqlite3_prepare(_db, utf8Statement, utf8Statement.length(), + &vm, &tail); + if (err == SQLITE_OK){ + logSQL(statement, kLogMsg_App); + if( sqlite3_step(vm) == SQLITE_ROW ) + { + return QString::fromUtf8((const char*)sqlite3_column_text(vm, 0)); + } + sqlite3_finalize(vm); + }else{ + qCritical() << "Unable to get sql for table: " << sTable; + } + + return sTableSQL; +} void DBBrowserDB::updateSchema( ) { diff --git a/src/sqlitedb.h b/src/sqlitedb.h index 21971a0c..a085361f 100644 --- a/src/sqlitedb.h +++ b/src/sqlitedb.h @@ -86,6 +86,14 @@ public: bool dump( const QString & filename); bool reload( const QString & filename, int * lineErr); bool executeSQL ( const QString & statement, bool dirtyDB=true, bool logsql=true); + + /** + * @brief getTableSQL Returns the create table statement for the given table. + * @param sTable Table name + * @return An empty string if the table does not exist + * or the create table statement. + */ + QString getTableSQL(const QString& sTable); void updateSchema() ; bool addRecord(); bool deleteRecord(int wrow);