add a function to return the create table statement for a given table

This commit is contained in:
Peinthor Rene
2013-03-13 18:09:40 +01:00
parent 0286520f19
commit 94955c1db9
2 changed files with 34 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
#include "sqlitedb.h"
#include "sqlbrowser_util.h"
#include "MainWindow.h"
#include <QFile>
#include <QMessageBox>
#include <QProgressDialog>
@@ -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( )
{

View File

@@ -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);