MeVisLab Scripting Reference
MLABSqlDatabase Class Reference

Inherits QObject.

Public Slots

SQL connection parameters


void setHostName (const QString &hostname)
 
QString hostName ()
 
void setPort (int port)
 
int port ()
 
void setUserName (const QString &name)
 
QString userName ()
 
void setPassword (const QString &pwd)
 
void setDatabaseName (const QString &name)
 
QString databaseName ()
 
SQL connection


bool open ()
 
bool open (const QString &username, const QString &password)
 
void close ()
 
QMap< QString, QVariant > lastError ()
 
bool isOpen ()
 
Database information


QStringList tables ()
 
bool supportsTransactions ()
 
bool supportsQuerySize ()
 
QSqlDatabase * getQSqlDatabase ()
 
Database transactions


bool transaction ()
 
bool commit ()
 
bool rollback ()
 
SQL query


MLABScriptSqlQuery * createQuery ()
 
void removeQuery (MLABScriptSqlQuery *query)
 
QVariant query (const QString &sqlStatement)
 

Detailed Description

MLABSqlDatabase.

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

Member Function Documentation

◆ close

void MLABSqlDatabase::close ( )
slot

Closes an open connection.

◆ commit

bool MLABSqlDatabase::commit ( )
slot

Commits the transaction data (returns true on success).

◆ createQuery

MLABScriptSqlQuery* MLABSqlDatabase::createQuery ( )
slot

Creates a new query object.

◆ databaseName

QString MLABSqlDatabase::databaseName ( )
slot

Returns the name of the database.

◆ getQSqlDatabase

QSqlDatabase* MLABSqlDatabase::getQSqlDatabase ( )
inlineslot

Returns the underlying QSqlDatabase instance.

◆ hostName

QString MLABSqlDatabase::hostName ( )
slot

Returns the host name.

◆ isOpen

bool MLABSqlDatabase::isOpen ( )
slot

Returns whether the database is open.

◆ lastError

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)

◆ open [1/2]

bool MLABSqlDatabase::open ( )
slot

Opens the connection, all connections parameters must be set before calling open.

Returns true on success.

◆ open [2/2]

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

Opens the connection with given username and password, all connections parameters must be set before calling open, except for username and password.

Returns true on success.

◆ port

int MLABSqlDatabase::port ( )
slot

Returns the port.

◆ query

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

Performs a query and returns the complete result as a list of records (maps) if the statement is a SELECT, otherwise is returns the number of affected rows.

  • returns a list of records (dict) 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:

results = db.query("SELECT username,password from be_users;")
if not results == -1:
# iterate over all rows
for result in results:
# print the username and password of each row
MLAB.log("username " + result["username"])
MLAB.log("password " + result["password"])
Access to all global functions and objects of MLAB.
Definition: mlabGlobalScriptAccess.h:81
void log(const QString &text)
Logs the given text to the console.

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

def (db, sqlStatement):
result = db.query(sqlStatement)
if not result == -1:
# is a select result?
for row in result:
for key in row:
MLAB.log(key)
else:
MLAB.log("error in statement")
QVariant query(const QString &sqlStatement)
Performs a query and returns the complete result as a list of records (maps) if the statement is a SE...

◆ removeQuery

void MLABSqlDatabase::removeQuery ( MLABScriptSqlQuery *  query)
slot

Removes the query object.

◆ rollback

bool MLABSqlDatabase::rollback ( )
slot

Rolls back to the beginning of the transaction (returns true on success).

◆ setDatabaseName

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

Sets the name of the database.

◆ setHostName

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

Sets the host name.

◆ setPassword

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

Sets the database connection password.

◆ setPort

void MLABSqlDatabase::setPort ( int  port)
slot

Sets the port.

◆ setUserName

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

Sets the database user name.

◆ supportsQuerySize

bool MLABSqlDatabase::supportsQuerySize ( )
slot

Returns whether the database supports returning the size of a SELECT statement.

◆ supportsTransactions

bool MLABSqlDatabase::supportsTransactions ( )
slot

Returns whether the database supports transactions.

◆ tables

QStringList MLABSqlDatabase::tables ( )
slot

Returns a list of tables of the database (call open() first!).

◆ transaction

bool MLABSqlDatabase::transaction ( )
slot

Starts a transaction (returns true on success).

◆ userName

QString MLABSqlDatabase::userName ( )
slot

Returns the database username.