BaseDB¶
-
class
Data_Reduction.GAVRT.mysql.BaseDB(host=None, user=None, pw=None, name=None, port=3306)¶ Bases:
objectThis is a database superclass.
- Public attributes::
db - the database connection object c - the database cursor host - database host port - port used to connect to the database pw - user’s password user - authorized db user
- Methods::
connect - returns a connection to a database. check_db - reconnects to the database if a connection has been lost cursor -
Methods Summary
checkDB()Reconnects to the database if the connection has been lost.
close()Close a connection
commit()Commits the most recent database transaction
connect()Make a connection to the database.
cursor()Creates a database cursor object; same as BaseDB.c but this is better because it handles disconnected a database
get(*args)Executes a query of the database
getLastId(table)ID of the last record
getLastRecord(table)Returns the last record as a dictionary
getRecordById(table, rec_id)Get the record with the given ID
get_as_dict(*args, **kwargs)Executes a query of the database and returns the result as a dict
get_columns(table)Returns information about the columns of a table
List the table names in the database.
get_rows_by_date(table, columns, year, doy)Gets data from a table
get_rows_by_time(table, columns, year, doy, utcs)Queries a table for quantities in columns at designated times
insertRecord(table, rec)Inserts a record into the database; handles a disconnected database
report_table(table, columns)Reports on the columns in a table
send_query(query)Send a MySQL query
updateValues(vald, table)Add row with updated values
Methods Documentation
-
checkDB()¶ Reconnects to the database if the connection has been lost.
-
close()¶ Close a connection
-
commit()¶ Commits the most recent database transaction
-
connect()¶ Make a connection to the database. Creates a cursor object.
Automatically invoked when an instance is created; can be called again if the connection is closed but the database object persists.
-
cursor()¶ Creates a database cursor object; same as BaseDB.c but this is better because it handles disconnected a database
-
get(*args)¶ Executes a query of the database
:param args : query to be executed
- Returns
record (dict)
-
getLastId(table)¶ ID of the last record
:param table : the name of the table :type table : str
- Returns
ID (int)
-
getLastRecord(table)¶ Returns the last record as a dictionary
:param table : name of the table (string)
- Returns
dict
-
getRecordById(table, rec_id)¶ Get the record with the given ID
:param table : table name :type table : str
:param id : row ID :type id : int
- Returns
dict
-
get_as_dict(*args, **kwargs)¶ Executes a query of the database and returns the result as a dict
At present, only keyword {‘asfloat’: True} is recognized and is the default if not given. It will convert to float any values for which it is possible.
If the query returns multiple rows, each value associated with a keyword will be a list. If nothing was found, an empty dictionary is returned.
:param db : database connection object
- Parameters
args – query to be executed
:param kwargs : a dictionary with keyword arguments.
@eturn: the record as a dictionary.
-
get_columns(table)¶ Returns information about the columns of a table
-
get_data_index()¶
-
get_public_tables()¶ List the table names in the database.
- Returns
tuple of tuples of str
-
get_rows_by_date(table, columns, year, doy)¶ Gets data from a table
:param table : table name :type table : str
:param columns : list of columns to be selected :type columns : list of str
:type year : int
:param doy : day of year :type doy : int
- Returns
dict of numpy arrays keyed on column name
-
get_rows_by_time(table, columns, year, doy, utcs)¶ Queries a table for quantities in columns at designated times
This loops over a list of UTs. For each, it takes the first row it finds matching the date and time. So it has an effective resolution of one second.
:param table : table name :type table : str
:param columns : list of columns to be selected :type columns : list of str
:type year : int
:param doy : day of year :type doy : int
:param utcs : times to be selected; first occurrence is used :type utcs : list of unixtimes (seconds since the epoch)
- Returns
dict of numpy arrays keyed on column name
-
insertRecord(table, rec)¶ Inserts a record into the database; handles a disconnected database
:param table : table name :type table : str
:param rec : a dictionary with column names as keys.
- Returns
record ID (int)
-
report_table(table, columns)¶ Reports on the columns in a table
Response has keys: Extra, Default, Field, Key, Null, Type
:param table : table name :type table : str
:param columns : list of column names :type columns : list of str
- Returns
result of query
-
send_query(query)¶ Send a MySQL query
:param crsr : cursor object for a database
:param query : MySQL query :type query : str
- Returns
str with query response
-
updateValues(vald, table)¶ Add row with updated values
Add a new row to table with same values as previous row, except for keys in vald, which are updated with provided values. This is useful for keeping logs.
:param vald : updated values :type vald : dict
:param table : table name :type table : str