| Home | Trees | Index | Help |
|
|---|
| Package picalo :: Module Database :: Class _Connection |
|
AbstractDecorator --+
|
_Connection
_MySQLdbConnection,
_OdbcConnection,
_Psycopg2Connection| Method Summary | |
|---|---|
__init__(self,
dbconnection,
connect_func,
connect_args)
| |
Returns the given attribute of the class. | |
Returns a string representation of this connection | |
Returns a string representation of this connection | |
Commits any changes to the database. | |
Copies a table from a database into this connection. | |
| _Cursor |
Returns a cursor to the database. |
| object |
Runs executable queries (INSERT, UPDATE, DELETE) that don't return tables. |
| InsertQueryBuilder |
This method returns a helper class for creating INSERT queries. |
Returns whether the connection needs saving | |
Returns the table names in this database as a list | |
Creates a new table in the database and posts the records in the given table. | |
| Cursor |
Runs a SELECT query that returns a result set. |
| Record |
Runs a SELECT query and returns only the first record from the result set. |
Refreshes the given table name, if it has already been pulled from the database. | |
Saves the database connection | |
| SelectQueryBuilder |
This method returns a helper class for creating simple SELECT queries. |
| Table |
This method is the primary access to databases within Picalo. |
| UpdateQueryBuilder |
This method returns a helper class for creating simple UPDATE queries. |
Creates a databaes relation matching the column names and types of the given table. | |
Returns a list of the columns in the given table | |
Helper method for _Connection.table(). | |
| Inherited from AbstractDecorator | |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Class Variable Summary | |
|---|---|
str |
QUERY_PARAMETER = '%s'
|
| Method Details |
|---|
__getattr__(self,
key)
Returns the given attribute of the class. In addition, you can type
connection.table to access any table in the database. The connection
will automatically "SELECT * FROM table" and pull the records
in an efficient way as you need them.
|
__repr__(self)
Returns a string representation of this connection
|
__str__(self)
Returns a string representation of this connection
|
commit(self)Commits any changes to the database. If you make modifications of any kind to your database tables (INSERT, UPDATE, CREATE, etc.), you *must* commit those changes by calling commit(). If you close a database connection without committing, you'll lose all changes. Analyses that only run SELECT calls on a database do not need to commit(). See the execute() method for an example of committing. |
copy_table(self, source_connection, source_tablename, dest_tablename, replace=False)Copies a table from a database into this connection. This method pulls records one by one from the source connection and posts them to this connection. It automatically creates the new destination table, optionally replacing any existing tables with the name. This is a convenience method provided to make importing of data from other databases easy. This method is different from post_table because it is more memory-efficient. The post_table method requires that you have an existing Picalo Table, which means all of the data needs to be in memory at once. This method copies record by record, meaning it only needs to hold one record at a time. This method works only with database connections, not with CSV or other types of sources. The method always tries to create a new table in the database -- it will not append records into an existing table. If the table cannot be created (if it exists, for example), an error is thrown. If the "replace" option is True, any existing tables by this name are dropped before the new table is created. The method automatically commits the data to the database. You cannot use rollback after this method has finished. This is required by some databases after creation of a table, so it has to be done. |
cursor(self)Returns a cursor to the database. Since Picalo manages cursors automatically, most users don't need to access them directly. The method is provided for advanced users who want to explicitly manage cursors.
|
execute(self, sql, parameters=None, close_cursor=True)Runs executable queries (INSERT, UPDATE, DELETE) that don't return tables. This is the primary methods to use when posting information to a database. IMPORTANT: Since most databases don't commit changes automatically, you *must* call commit() to make the changes permanent. If you close the connection without committing, you'll lose all of your changes. Alternatively, you can call rollback() to undo all changes since the last commit() call. Example:
>>> myconn.execute("CREATE TABLE mytable (id integer, name varchar(20))")
>>> myconn.execute("INSERT INTO mytable (id, name) VALUES (1, 'Dennis')")
>>> myconn.commit()
|
insert_query_builder(self, tablename)This method returns a helper class for creating INSERT queries. It allows you to create INSERT queries piece by piece and is available for advanced users. It is useful when creating an SQL query programatically in a script. Example: >>> db = Database.PostgreSQLConnection('mydb') >>> q = db.insert_query_builder('test') >>> q.add('id', 5) >>> q.add('name', 'Sally') >>> print q QueryBuilder: <cursor>.execute("INSERT INTO test (id, name) VALUES (%s, %s)", [5, 'Sally']) >>> q.execute() >>> db.commit()
|
is_changed(self)Returns whether the connection needs saving |
list_tables(self, refresh=True)Returns the table names in this database as a list |
post_table(self, table, name, replace=False, add_if_empty=False)Creates a new table in the database and posts the records in the given table. If a table with the given name already exists in the table, the method throws an error unless the "replace" option is True. This is a convenience method provided to make importing of data from text files (CSV, TSV, etc.) and other sources into databases easy. The example shows how a CSV file can be posted to database in just two lines of code. The method always tries to create a new table in the database -- it will not append records into an existing table. If the table cannot be created (if it exists, for example), an error is thrown. If the "replace" option is True, any existing tables by this name are dropped before the new table is created. The method automatically commits the data to the database. You cannot use rollback after this method has finished. This is required by some databases after creation of a table, so it has to be done. Example:>>> myconn = Database.PostgreSQLConnection('mydb') >>> data = Table([('id', int), ('name', unicode)],[ ... [ 1, 'Benny' ], ... [ 2, 'Vijay' ], ... ]) >>> myconn.post_table(data, 'mytable', True) >>> myconn.table('SELECT * FROM mytable').view() +----+-------+ | id | name | +----+-------+ | 1 | Benny | | 2 | Vijay | +----+-------+
|
query(self, sql, parameters=None)Runs a SELECT query that returns a result set. This method provides efficient access to extremely large data sets, but it is not as easy to use as table(). Most users should use table() unless you explicitly need to access only one record at a time. In other words, check out Database.Connection.table(sql) and use it unless you really need the efficiency offered by this method. Stated differently, query() and table() accomplish the same task: query a database. The query() method is more efficient and the table() method is more powerful and easier to use. If you need to efficiently use memory and must use this method, you should use an iterator in a for loop as is done in the example. This allows you to access one record at a time. Note that you can only go in forward direction one at a time. If you need to go backwards in your analysis, either store the records in variables or use table(). Example:>>> import psycopg >>> myconn = Database.Connection(psycopg.connect(database='mydb')) >>> for record in myconn.query("SELECT * FROM mytable"): ... print record['id'] ... print record['name'] ... 1 Dennis
|
query1(self, sql, parameters=None)Runs a SELECT query and returns only the first record from the result set. There are many times when you expect only a single record in the result set, such a key value query. In these circumstances, query1() provides easy access directly to the first record. The method returns a single Record instance or None if no results are found. Example:>>> import psycopg >>> myconn = Database.Connection(psycopg.connect(database='mydb')) >>> rec = myconn.query1("SELECT min(id) FROM mytable") >>> print rec 1
|
refresh(self, tablename=None)Refreshes the given table name, if it has already been pulled from the database. This has relation to the db.tablename syntax. If the table has been updated on the database, we won't pull the new records until this method is called. When tablename is None, all tables are refreshed.
|
save(self, filename)Saves the database connection |
select_query_builder(self, tablename, select_fields=['*'])This method returns a helper class for creating simple SELECT queries. It allows you to create SELECT queries piece by piece and is available for advanced users. It is useful when creating an SQL query programatically in a script. Example: >>> db = Database.PostgreSQLConnection('mydb') >>> q = db.select_query_builder('test', ['id', 'name']) >>> q.add_where('id', 1) >>> print q QueryBuilder: <cursor>.execute("SELECT id, name FROM test WHERE id=%s", [1]) >>> results = q.table() >>> results.prettyprint() +----+--------+ | id | name | +----+--------+ | 1 | Joseph | +----+--------+
|
table(self, sql, parameters=None)This method is the primary access to databases within Picalo. Most users will create a connection and then call only this method to retrieve data from tables. Retrieves a Table to the results of a new query or the most recently-run query. Table objects have additional methods not found in this module, such as pretty printing, saving to text files, and mutability. If your database driver is one of the enhanced support drivers (such as PythonWin's odbc, psycopg, and MySQLdb drivers), the method will automatically set column types. The primary reason you would not want to use this method is that it loads all records into memory at query time, so it might use up your memory with super large tables. Example:>>> import psycopg >>> myconn = Database.Connection(psycopg.connect(database='conan')) >>> data = myconn.table("SELECT * FROM mytable") >>> data.view() +----+--------+ | id | name | +----+--------+ | 1 | Dennis | +----+--------+
|
update_query_builder(self, tablename)This method returns a helper class for creating simple UPDATE queries. It allows you to create UPDATE queries piece by piece and is available for advanced users. It is useful when creating an SQL query programatically in a script. Example: >>> db = Database.PostgreSQLConnection('mydb') >>> q = db.update_query_builder('test') >>> q.add_where('id', 3) >>> q.add('name', 'newname') >>> print q QueryBuilder: <cursor>.execute("UPDATE test SET name=%s WHERE id=%s", ['newname', 3]) >>> q.execute() >>> db.commit()
|
_create_database_table(self, table, name, replace=False)Creates a databaes relation matching the column names and types of the given table. |
_get_columns(self, tablename)Returns a list of the columns in the given table |
_set_table_types(self, cursor, table)Helper method for _Connection.table(). After the query is run, this method uses the cursor to set the column types appropriately. |
| Class Variable Details |
|---|
QUERY_PARAMETER
|
| Home | Trees | Index | Help |
|
|---|
| Generated by Epydoc 2.1 on Mon Aug 20 05:38:17 2007 | http://epydoc.sf.net |