MLABSqlDatabase Class Reference
[Scripting]

List of all members.

Public Slots

SQL connection
void close ()
bool isOpen ()
QMap< QString, QVariant > lastError ()
bool open (const QString &username, const QString &password)
bool open ()
Database transactions
bool commit ()
bool rollback ()
bool transaction ()
SQL query
MLABScriptSqlQuery * createQuery ()
QVariant query (const QString &sqlStatement)
void removeQuery (MLABScriptSqlQuery *query)
SQL connection parameters
QString databaseName ()
QString hostName ()
int port ()
void setDatabaseName (const QString &name)
void setHostName (const QString &hostname)
void setPassword (const QString &pwd)
void setPort (int port)
void setUserName (const QString &name)
QString userName ()
Database information
bool supportsQuerySize ()
bool supportsTransactions ()
QStringList tables ()


Detailed Description

The MLABSqlDatabase object can be used to connect to a SQL database and do querys on it.

Member Function Documentation

void MLABSqlDatabase::close (  )  [slot]

close an open connection

bool MLABSqlDatabase::commit (  )  [slot]

commit the transaction data (returns true on success)

MLABScriptSqlQuery* MLABSqlDatabase::createQuery (  )  [slot]

create a new query object

QString MLABSqlDatabase::databaseName (  )  [slot]

get the name of the database

QString MLABSqlDatabase::hostName (  )  [slot]

get the host name

bool MLABSqlDatabase::isOpen (  )  [slot]

returns if database is open

QMap<QString, QVariant> MLABSqlDatabase::lastError (  )  [slot]

returns the last error message as a map

the map contains:

  • message - complete message (type,driverText,databaseText,errorNumber)
  • type - type of error
  • databaseText
  • driverText
  • number - error number (depends on driver/database type)

bool MLABSqlDatabase::open ( const QString &  username,
const QString &  password 
) [slot]

open connection with given username and password, all connections parameters must be set before calling open, except for username and password returns true on success

bool MLABSqlDatabase::open (  )  [slot]

open connection, all connections parameters must be set before calling open returns true on success

int MLABSqlDatabase::port (  )  [slot]

get port

QVariant MLABSqlDatabase::query ( const QString &  sqlStatement  )  [slot]

do a query and get the complete result as a list of records (maps) when the statement is a SELECT, otherwise is returns the number of affected rows

  • returns an Array of records (maps) on a successful SELECT
  • returns number of affectedRows on any other statement
  • returns -1 on an error

Example that prints the results of a specific select statement:

  var result = db.query("SELECT username,password from be_users;");
  if (result!=-1) {
    // iterate over all rows
    for (var i in result) {
      // print the username and password of each row
      MLAB.log("username " + result[i]["username"]);
      MLAB.log("password " + result[i]["password"]);
    }
  }

Example that prints all result rows of a select statement or the number of affected rows on other statements, assuming that "db" is an opened database:

  function (db, sqlStatement) {
   var result = db.query(sqlStatement);
   if (result != -1) {
    // is a select result?
    if (result instanceof Array) {
      for (var a in result) {
        var row = result[a];
        for (var b in row) {
          MLAB.log(b + ":" + row[b]);
        }
      }
    } else {
      MLAB.log("affectedRows: " + tmp);
    }
   } else {
     MLAB.log("error in statement");
   }
  }

void MLABSqlDatabase::removeQuery ( MLABScriptSqlQuery *  query  )  [slot]

remove the query object

bool MLABSqlDatabase::rollback (  )  [slot]

rollback to the beginning of the transaction (returns true on success)

void MLABSqlDatabase::setDatabaseName ( const QString &  name  )  [slot]

set the name of the database

void MLABSqlDatabase::setHostName ( const QString &  hostname  )  [slot]

set the host name

void MLABSqlDatabase::setPassword ( const QString &  pwd  )  [slot]

set the db connection password

void MLABSqlDatabase::setPort ( int  port  )  [slot]

set port

void MLABSqlDatabase::setUserName ( const QString &  name  )  [slot]

set db user name

bool MLABSqlDatabase::supportsQuerySize (  )  [slot]

returns if the database supports returning the size of a SELECT statement

bool MLABSqlDatabase::supportsTransactions (  )  [slot]

returns if the database supports transactions

QStringList MLABSqlDatabase::tables (  )  [slot]

get tables of the database (call open() first!)

bool MLABSqlDatabase::transaction (  )  [slot]

start a transaction (returns true on success)

QString MLABSqlDatabase::userName (  )  [slot]

get db username


Generated on Sat Sep 3 18:38:03 2011 for MeVisLab Scripting Reference by  doxygen 1.5.8