2. DB Connector (module)

class kClusterLib.db_connector.mysql_connection(*args, **kwargs)[source]

Gives access to database.

Examples

Create a database connection object and use it to fetch data.

>>> with mysql_connection( "localhost", "root", "pass", "test") as con:
>>>     status = con.execute(" select count(*) from employee")
>>>     print status,con.fetchall()

Note

when using with` statement, database connection is closed automatically

Methods
__init__ : (internal) initializer __enter__ : (internal) opens db connection and creates cursor __exit__ : (internal) closes connection and cursor
kClusterLib.db_connector.fetchRAWData(**kwargs)[source]

Returns data from database based on keyword arguments given.

Conveniently get data from a database table. No need to fiddle with database cursors or anything.

Keyword Arguments:
 
  • debug (bool) – flag to print debug messages. If db connection is failing then setting this variable to True might help locate problem.
  • idx_start (int) – start index for samples selection
  • idx_end (int) – end index for samples selection
  • db_host (str) – database host (whether mysql database is local or remote)
  • user (str) – username for database connection authentication
  • u_pass (str) – password for selected username
  • dbase (str) – database name
  • table (str) – table name

Example

Fetching data from sensor_co table in EXPeriment database

>>> from db_connector import fetchRAWData
>>> myMat, myLabels = fetchRAWData(debug=False,
                                    idx_start=0,
                                    idx_end=1000,
                                    db_host='localhost',
                                    dbase='EXPeriment',
                                    user = 'analyst',
                                    table='sensor_co',
                                    u_pass = '*******',
                                    )
>>> print myMat.shape

produces following output .. testoutput:

(1000, 42)