|
def | Initialize (self, host, user, password, db=None, dbType="mysql", socket="/tmp/mysql.sock", charset="utf8", port=3306, openCursor=False) |
| Initialize the DBConnector object. More...
|
|
def | OpenConnection (self) |
| Establish a connection to the database and store it in a class variable. More...
|
|
def | CloseConnection (self) |
| Close the connection to the database. More...
|
|
def | Execute (self, sql, parameters=None, multi=False, insert=False) |
| Execute a given SQL statement on the server and return the results. More...
|
|
Base connector class for accessing a database.
In theory this class can be used to access various database types, such as MySQL or Postgres. However, currently we are only supporting MySQL.
TODO: allow choosing betwen dict and normal cursor, standard is currently dict TODO: think about doing connection pooling or something similar. Currently we open a connection ones the class is initialized and give it back at the end.
def common.DBConnector.CDBConnector.Execute |
( |
|
self, |
|
|
|
sql, |
|
|
|
parameters = None , |
|
|
|
multi = False , |
|
|
|
insert = False |
|
) |
| |
Execute a given SQL statement on the server and return the results.
Will make sure that the connection is valid and open before executing
the statement.
@param sql The SQL statement to be executed. Can either be a complete
statement like 'SELECT * FROM XXX WHERE ID = 1' or can use placeholders
for later formatting, like 'SELECT * FROM XXX WHERE ID = %s' (see
http://www.python.org/dev/peps/pep-0249/ for more details). In the
latter case, the values have to be passed via the parameters parameter.
@param parameters If parameters are handed over, then the SQL statement
will be formatted accordingly using those parameters. Can be either a
single value or a list of values, depending on the number of parameters
that are to be replaced.
@param multi If @c True, multiple SQL statements can be executed in
a single run (e.g. creating multiple databasee tables). If left empty
the default value @c False will be used and only a single SQL statement
can be executed. This parameter is only intended for INSERT or UPDATE
statements that require no return value. For anything that should
return data, it is strongly advised not to use @c True for this
parameter.
@param insert If @c True, we will assume an INSERT, UPDATE or CREATE
statement and issue a commit to the database. The id of the
inserted value will returned if available. If default value @c False is
used or if the parameter is left empty, we will assume it is a SELECT
query and the retrieved data will be returned.
@return The result of the executed query. Depends on the type of query,
an INSERT statement (see insert parameter) will normally return the
identifier of the inserted data, a SELECT willreturn a list of a
certain size. If @c False is returned, an error
occurred, which will have been written by the @ref ErrorHandler.
def common.DBConnector.CDBConnector.Initialize |
( |
|
self, |
|
|
|
host, |
|
|
|
user, |
|
|
|
password, |
|
|
|
db = None , |
|
|
|
dbType = "mysql" , |
|
|
|
socket = "/tmp/mysql.sock" , |
|
|
|
charset = "utf8" , |
|
|
|
port = 3306 , |
|
|
|
openCursor = False |
|
) |
| |
Initialize the DBConnector object.
This initialization method has to be called before the object can be
used, as this will set up the proper access credentials and open the
database connection.
@param host The host name of the database, required.
@param user The name of the database user, required.
@param password The password of the database user, required but can be
an empty string (e.g. for user anonymous)
@param db The name of the database that will be used for querying. If it
is omitted, the database name has to be included in latter queries
(e.g. select * from schema.table instead of select * from table).
@param dbType The type of the database server. If it is ommited, the
standard mysql connector will be used.
@param charset The character set used for returning results. If it is
omitted, the default setting utf8 will be used.
@param port The port where the database server is available. If omitted,
the default MySQL port 3306 will be used.
@param openCursor If @c True, a cursor will be created together with
the database connection, which will be kept open (potentially speeding
up multiple (i.e. hundreds of thousands) successive queries). If
@c False, the cursor will be opened before each query and closed after.
Default setting is @c False.
@return @c True, if successfully connected to database, @c False if
there was an error (a message has been issued via @ref ErrorHandler).