Table of Contents

Sql

This object allows you to establish a connection to a SQL database like MySQL, PostgreSQL, SQLite and others.

Functions

Sql

Constructor function.

Syntax

var myObject = new Sql(driver);

Arguments

  1. driver - (Driver) the database driver to use

Exceptions

Example

Create a Sql object operating on a MySQL database.

var myObject = new Sql(Sql.MySQL);

Notes

On Windows only the MySQL, PostgreSQL, SQLite 3, Firebird and ODBC drivers are installed by default. To see all available drivers call the drivers function.

drivers

Returns a list of available Drivers.

Syntax

Sql.drivers();

Methods

connect

Opens a connection with a database.

Syntax

myObject.connect(parameters);

Arguments

  1. parameters - (object) the connection parameters
    • hostName - (string) the host name
    • port - (integer) the port to use (use the default port if not specified)
    • databaseName - (string) the database name (when using SQLite this is the filename of the database)
    • userName - (string) the user name
    • password - (string) the password
    • options - (string) connection options (see http://doc.qt.nokia.com/latest/qsqldatabase.html#setConnectOptions)

Returns

Exceptions

prepare

Prepare a request on the current database.

Syntax

myObject.prepare(queryString, parameters);

Arguments

  1. queryString - (string) the query string to prepare
  2. parameters - (object) the prepare parameters

Returns

Exceptions

Example

myObject.prepare("INSERT INTO myTable (id, data) VALUES(:id, :data)", {
	id: 1,
	data: "My data"
});

execute

Execute a prepared request or executes a request on the current database.

Syntax

myObject.execute(queryString);
myObject.execute();

Arguments

  1. queryString - (string) the query string to execute

Returns

Exceptions

fetchResult

Retrieves the result of an executed request.

Syntax

myObject.fetchResult(indexStyle);
myObject.fetchResult();

Arguments

  1. indexStyle - (IndexStyle) the fetch index style

Returns

Exceptions

disconnect

Disconnects the current database connection.

Syntax

myObject.disconnect();

Returns

Enumerations

Driver

SQL drivers. On Windows only the MySQL, PostgreSQL, SQLite 3, Firebird and ODBC drivers are installed by default.

Values

  1. SQLite2: SQLite 2
  2. SQLite: SQLite 3
  3. PostgreSQL: PostgreSQL (versions 7.3 and above)
  4. MySQL: MySQL
  5. ODBC: Open Database Connectivity (ODBC) - Microsoft SQL Server and other ODBC-compliant databases
  6. TDS: Sybase Adaptive Server (obsolete)
  7. DB2: IBM DB2 (version 7.1 and above)

IndexStyle

The fetch index style.

Values

  1. IndexNumber: indexes the results by number
  2. IndexName: indexes the results by column name