Skip to content

Commit

Permalink
add a function to return the create table statement for a given table
Browse files Browse the repository at this point in the history
  • Loading branch information
rp- committed Mar 13, 2013
1 parent 0286520 commit 94955c1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/sqlitedb.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "sqlitedb.h"
#include "sqlbrowser_util.h"
#include "MainWindow.h"

#include <QFile>
#include <QMessageBox>
#include <QProgressDialog>
Expand Down Expand Up @@ -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( )
{
Expand Down
8 changes: 8 additions & 0 deletions src/sqlitedb.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ class DBBrowserDB
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);
Expand Down

0 comments on commit 94955c1

Please sign in to comment.