DbConnection

From Fernseher
Jump to navigationJump to search

DbConnection is used to talk to MySQL (for now) and later SQLite databases from a Modjs script.

To connect to a database, create a new object:

connection = new DbConnection(host, database, username, password, type): type is "mysql" or "sqlite" but defaults to "mysql"

Exceptions include:

DbConnectionError
DbStatementError

To use the connection to the database:

connection.close(): closes the database connection explicitly(otherwise garbage collection will do so)
statement = connection.prepare(string): creates a new prepared statement based on the passed in string
numRowsAffected = connection.execute(string): executes the given statement straight up.  Please don't use unless you have to. May not be supported.
array of result objects = connection.query(string): executes the given statement straight up. Please don't use unless you have to. May not be supported.

To use DbStatements, you must first get one from the connection by preparing it. Then:

numRowsAffected = statement.execute(array of bound arguments)
array of result objects = statement.query(array of bound arguments)

Result objects are created by using the field names (or AS marked field names) wherever possible. If not possible (expression or something), the field will be named "_exp_N" where N goes from 1 on up as needed. Try to use reasonably AS clauses. it just makes it so much easier. expressions may not be supported at first.

Statements stick around until they are garbage collected.