QmlSqlDatabase QML Type

The QmlSqlDatabase class represents a connection to a database. The QmlSqlDatabase provides an interface for accessing a database through a connection. An instance of QmlSqlDatabase represents the connection. The connection provides access to the database via one of the supported databaseDriver , which are derived from QSqlDriver (c++). Alternatively, one can subclass your own database driver from QSqlDriver (c++). See How to Write Your Own Database Driver for more information. Create a connection (i.e., an instance of QSqlDatabase) by calling one of the method addDatabase() , where you specify the databaseDriver (i.e., what kind of database will you access) and a connectionName. A connection is known by its own name, Not by the name of the database it connects to. You can have multiple connections to one database. QSqlDatabase also supports the concept of a default connection (not recommended ), which is the unnamed connection. To create the default connection, don't pass the connection name argument before you call addDatabase(). Subsequently, when you call any method function that takes the connection name argument, if you don't pass the connectionName argument, the default connection is assumed. The following snippet shows how to create and open a default connection to a PostgreSQL database: More...

Import Statement: import QmlSql 1.0

Properties

Methods

Detailed Description


  QmlSqlDatabase{
    id: db
    hostName:"acidalia";
    databaseName: "customdb";
    userName: "mojito";
    password("J0a1m8"

    databaseDriver: QmlSqlDatabase.QPSQL
    onOpen{
        //do something
    }
    Componet.onCompleted:{
        db.addDatabase()
    }
  }

Once the QmlSqlDatabase object has been created, set the connection parameters with databaseName, userName, password, hostName and port. Then call addDatabase() to activate the physical connection to the database. The connection is not usable until you open it. The connection defined above will be the default connection, because we didn't give a connection name to the object with the id of db.

To Add a connectionName just add


  connectionName: "customdb-connection"

It is a good idea to add a connectionName to the QmlSqlDatabase as that way one can use the other Objects that are in the QmlSql plugin to call over and over again from any page at any time.

Example:


  QmlSqlQuery{
    id: db
    connectionName: "customdb-connection"
  }

The code example above will connect to the database with the id of "customdb-connection" and one does not need to create Objects over and over again.

If an error occurs, errorString will return information about it. Get the names of the available SQL drivers with databaseDriverList.

Warning : There are still some wrapping that needs to be done to this class and it is subject to change in the near future. Some of the things are close open check validation of Database types ect.

Property Documentation

connectionName : string

Sets the connectionName to connectionName


connectionNames : string

returns a list of connection Names that are set on this db

for more info on this please see the source code.


databaseDriver : enum

Returns the database driver used to access the database connection.

See also addDataBase().


databaseDriverList : string

Returns a list of all the available database drivers that one can use


databaseName : string

Sets the connection's databaseName. To have effect, the databaseName must be set before the connection is opened.

Note: The databaseName is not the connectionName. The connectionName must be added before addDatabase() method is called.

For the QOCI (Oracle) driver, the databaseName is the TNS Service Name.

There is no default value.


errorString : string

Returns information about the last error that occurred on the database.


password : string

Sets the connection's password to password. To have effect, The password must be set before the connection is opened.

There is no default value.

Warning: This stores the password in plain text within Qt. Use the open() call that takes a password as parameter to avoid this behavior.


port : string

Sets the connection's port number to port. To have effect, the port number must be set before the connection is opened.

There is no default value.


running : bool

Returns true if the database connection is currently open; otherwise returns false.


source : string

Sets the connection's host name to source. To have effect, the source must be set before the connection is opened.

There is no default value.


user : string

Sets the connection's user name to user. To have effect, the user name must be set before the connection is opened.

There is no default value


Method Documentation

addDataBase()

Adds a database to the list of database connections using the driver type and the connection name connectionName. If there already exists a database connection called connectionName, that connection is removed. The database connection is referred to by connectionName. The newly added database connection is returned.

If connectionName is not specified, the new connection becomes the default connection for the application, and subsequent calls to databaseName without the connection name argument will return the default connection. If a connectionName is provided here, use database(connectionName) to retrieve the connection.

Warning: If you add a connection with the same name as an existing connection, the new connection replaces the old one. If you call this function more than once without specifying connectionName, the default connection will be the one replaced.

Note: This function is thread-safe


removeDatabase()

Removes the database connection connectionName from the list of database connections.

Warning: There should be no open queries on the database connection when this method is called, otherwise a resource leak will occur.

To remove the default connection, which may have been created with a call to addDatabase() not specifying a connection name, you can retrieve the default connection name by calling connectionName() on the database returned by database(). Note that if a default database hasn't been created an invalid database will be returned.

Note: This function is thread-safe


variant tables(string connectionName)

This returns a list of all the table names on a connection point by there connecitonName

Note: This is in alpha and subject to change.