pymysql_utils Package — pymysql_utils 0.30 documentation

### Navigation

- index
- modules |
- pymysql_utils 0.30 documentation »

# pymysql_utils Package

## `pymysql_utils` Module

Created on Sep 24, 2013

@author: paepcke

Modifications:
  - Dec 30, 2013: Added closing of connection to close() method

class `pymysql_utils.pymysql_utils.` `MySQLDB`(host='127.0.0.1', port=3306, user='root', passwd='', db='mysql')

  Bases: `object`

  Shallow interface to MySQL databases. Some niceties nonetheless. The query() method is an iterator. So:

      for result in mySqlObj.query('SELECT * FROM foo'):
          print result

  `bulkInsert`(tblName, colNameTuple, valueTupleArray)

    Inserts large number of rows into given table. Strategy: write the values to a temp file, then generate a LOAD INFILE LOCAL MySQL command. Execute that command via subprocess.call(). Using a cursor.execute() fails with error ‘LOAD DATA LOCAL is not supported in this MySQL version...’ even though MySQL is set up to allow the op (load-infile=1 for both mysql and mysqld in my.cnf).

    Parameters: | tblName (string) – table into which to insert colNameTuple ((str[,str[...]])) – tuple containing column names in proper order, i.e. corresponding to valueTupleArray orders. valueTupleArray ([(<anyMySQLCompatibleTypes>[<anyMySQLCompatibleTypes,...]])) – array of n-tuples, which hold the values. Order of values must correspond to order of column names in colNameTuple.

  `close`()

    Close all cursors that are currently still open.

  `createTable`(tableName, schema, temporary=False)

    Create new table, given its name, and schema. The schema is a dict mappingt column names to column types. Example: {‘col1’ : ‘INT’, ‘col2’ : ‘TEXT’}

    Parameters: | tableName (String) – name of new table schema (Dict<String,String>) – dictionary mapping column names to column types

  `dropTable`(tableName)

    Delete table safely. No errors

    Parameters: | tableName (String) – name of table

  `ensureSQLTyping`(colVals)

    Given a list of items, return a string that preserves MySQL typing. Example: (10, ‘My Poem’) —> ‘10, “My Poem”’ Note that ‘,’.join(map(str,myList)) won’t work: (10, ‘My Poem’) —> ‘10, My Poem’

    Parameters: | colVals (<any>) – list of column values destined for a MySQL table

  `execute`(query)

    Execute an arbitrary query, including MySQL directives.

    Parameters: | query (String) – query or directive

  `executeParameterized`(query, params)

    Executes arbitrary query that is parameterized as in the Python string format statement. Ex: executeParameterized(‘SELECT %s FROM myTable’, (‘col1’, ‘col3’))

    Parameters: | query (string) – query with parameter placeholder params ((<any>)) – actuals for the parameters

  `insert`(tblName, colnameValueDict)

    Given a dictionary mapping column names to column values, insert the data into a specified table

    Parameters: | tblName (String) – name of table to insert into colnameValueDict (Dict<String,Any>) – mapping of column name to column value

  `query`(queryStr)

    Query iterator. Given a query, return one result for each subsequent call.

    Parameters: | queryStr (String) – query

  `stringifyList`(iterable)

    Goes through the iterable. For each element, tries to turn into a string, part of which attempts encoding with the ‘ascii’ codec. Then encountering a unicode char, that char is UTF-8 encoded.

    Acts as an iterator! Use like: for element in stringifyList(someList):

    > print(element)

    Parameters: | iterable ([<any>]) – mixture of items of any type, including Unicode strings.

  `truncateTable`(tableName)

    Delete all table rows. No errors

    Parameters: | tableName (String) – name of table

  `update`(tblName, colName, newVal, fromCondition=None)

    Update one column with a new value.

    Parameters: | tblName (String) – name of table in which update is to occur colName (String) – column whose value is to be changed newVal (type acceptable to MySQL for the given column) – value acceptable to MySQL for the given column fromCondition (String) – optionally condition that selects which rows to update. if None, the named column in all rows are updated to the given value. Syntax must conform to what may be in a MySQL FROM clause (don’t include the ‘FROM’ keyword)

## `test_pymysql_utils` Module

Created on Sep 24, 2013

@author: paepcke

class `pymysql_utils.test_pymysql_utils.` `TestMySQL`(methodName='runTest')

  Bases: `unittest.case.TestCase`

  To make these unittests work, prepare the local MySQL db as follows:

    o CREATE USER unittest; o CREATE DATABASE unittest;

    > > o GRANT SELECT ON unittest.* TO 'unittest'@'localhost‘; o GRANT INSERT ON unittest.* TO 'unittest'@'localhost‘;

    > o GRANT DROP ON unittest.* TO 'unittest'@'localhost‘; o GRANT CREATE ON unittest.* TO 'unittest'@'localhost‘;

  `buildSmallDb`()
  `setUp`()
  `tearDown`()
  `testExecuteArbitraryQuery`()
  `testExecuteArbitraryQueryParameterized`()
  `testInsert`()
  `testInsertSeveralColums`()
  `testQueryIterator`()
  `testTruncate`()

### Navigation

- index
- modules |
- pymysql_utils 0.30 documentation »