CGATcore Database Module¶
database.py - database utility functions¶
This module contains convenience functions to work with a relational database.
Reference¶
apsw_connect(dbname=None, modname='tsv')
¶
attempt to connect to apsw database.
This method will attempt to establish a connection to a database .
Arguments¶
modname: string A module name to register with sqlite dbname: string A database name to connect to
Returns¶
con : object A connection to a database.
Source code in cgatcore/database.py
connect(dbhandle=None, attach=None, url=None)
¶
attempt to connect to database.
If dbhandle
is an existing connection to a database,
it will be returned unchanged. Otherwise, this method
will attempt to establish a connection.
Arguments¶
url: string A database url dbhandle : object or string A database handle or a connection string.
Returns¶
dbhandle : object A DB-API2 conforming database handle
Source code in cgatcore/database.py
db_execute(cc, statements)
¶
excute a statement or statements against a cursor
execute(queries, dbhandle=None, attach=False)
¶
Execute a statement or a list of statements (sequentially)
executewait(dbhandle, statement, regex_error='locked', retries=-1, wait=5)
¶
repeatedly execute an SQL statement until it succeeds.
Arguments¶
dbhandle : object
A DB-API conform database handle.
statement : string
SQL statement to execute.
error : string
Exception to catch and examine for error messages.
regex_error : string
Any error message matching regex_error
will be ignored,
otherwise the procedure exists.
retries : int
Number of retries. If set to negative number, retry indefinitely.
If set to 0, there will be only one attempt.
wait : int
Number of seconds to way between retries.
Returns¶
A cursor object
Source code in cgatcore/database.py
fetch(query, dbhandle=None, attach=False)
¶
Fetch all query results and return
fetch_DataFrame(query, dbhandle=None, attach=False)
¶
Fetch query results and returns them as a pandas dataframe
Source code in cgatcore/database.py
fetch_with_names(query, dbhandle=None, attach=False)
¶
Fetch query results and returns them as an array of row arrays, in which the first entry is an array of the field names
Source code in cgatcore/database.py
getColumnNames(dbhandle, table)
¶
return column names of a table from a database.
getTables(dbhandle)
¶
toTSV(dbhandle, outfile, statement, remove_none=True)
¶
execute statement and save as tsv file to disk.
If remove_none is true, empty/NULL values will be output as empty values.
Source code in cgatcore/database.py
write_DataFrame(dataframe, tablename, dbhandle=None, index=False, if_exists='replace')
¶
write a pandas dataframe to an sqlite db, index on given columns index columns given as a string or list eg. "gene_id" or ["gene_id", "start"]