Python Quick Notes :: Part - 4
Bhaskar S | 03/09/2013 |
Hands-on With Python - IV
To access relational databases in Python, one must use the standard DB-API interfaces. For MySQL, download the MySQL Connector for Python from the following website:
Follow the setup procedure oulined in the following website:
MySQL Connector/Python Installation
To work with MySQL, one must import the module mysql.connector. The following are some of the frequently used functions from the Python DB-API:
con = mysql.connector.connect(host=H, port=P, user=U, password=S, database=D) - open a database connection with the specified database D running on the specified host H and port P using the specified credentials for the user U and password S. Returns a connection object con on success
con.close() - close the connection to the database using the connection object con
con.commit() - issue a database commit using the connection object con
cur = con.cursor() - return a new cursor object cur using the connection object con. One must have a cursor object to perform any database operation
con.rollback() - issue a database rollback using the connection object con
cur.close() - close the database cusror using the cursor object cur
cur.execute(sql) - execute the specified sql statement using the cursor object cur
cur.execute(sql, {args}) - execute the specified prepared sql statement with the specified dictionary of arguments args using the cursor object cur
cur.fetchone() - fetch the next row from the result set of the SQL query using the cursor object cur
cur.fetchall() - fetch all the rows from the result set of the SQL query using the cursor object cur
The following is the python program named sample-20.py:
Execute the following command:
python sample-20.py
The following is the output:
Ready to connect to mytestdb ..... Ready to create a cursor ..... Ready to create table product_tbl ..... Ready to insert into table product_tbl ..... Ready to select no of records in table product_tbl ..... Record count in table product_tbl (3,) Ready to select all records from table product_tbl ..... 1000, Python Quick Start Part 1, 10, 2.99 1001, Python Quick Start Part 2, 10, 2.99 1002, Python Quick Start Part 3, 10, 2.99 Ready to update item 1001 in table product_tbl ..... Ready to select item 1001 from table product_tbl ..... 1001, Python Quick Start Part 2, 5, 2.99 Ready to delete item 1002 from table product_tbl ..... Ready to select all records from table product_tbl ..... 1000, Python Quick Start Part 1, 10, 2.99 1001, Python Quick Start Part 2, 5, 2.99 Ready to insert into table product_tbl (prepared) ..... Ready to select all records from table product_tbl ..... 1000, Python Quick Start Part 1, 10, 2.99 1001, Python Quick Start Part 2, 5, 2.99 1003, C Manual Vol 1, 3, 1.99 1004, C Manual Vol 2, 6, 1.99 Ready to update item in table product_tbl (prepared) ..... Ready to select item from table product_tbl (prepared) ..... 1004, C Manual Vol 2, 1, 1.99 Ready to drop the table product_tbl ..... Done !!!
NOTE :: The parameter(s) in a prepared statement must be specified as %(name)s and the value corresponding for name must be provided in a dictionary as {'name': 'value'}
Python's standard library modules xml.sax and xml.dom provide the necessary interfaces to work with XML. The module xml.sax provides the Simple API for XML (SAX) parser interface while the module xml.dom provides the Document Object Model (DOM) parser interface.
The Simple API for XML (SAX) parser is an event based interface for parsing XML in which one must register a custom content handler that defines callback functions for the desired events. To register a custom content handler, one must subclass the class xml.sax.ContentHandler and override the desired callback methods. The following are the commonly used callback methods from the class xml.sax.ContentHandler:
startDocument() - is called at the start of the XML document
endDocument() - is called at the end of the XML document
startElement(name, attrs) - is called at the start of the XML element name. attrs is the list of attribute name-value pairs. To extract the value of an attribute with name price one has to invoke the method attrs.getValue('price')
startElementNS(name, qname, attrs) - is called when the parser is namespace aware and at the start of the XML element name in the namespace qname
endElement(name) - is called at the end of the XML element name
endElementNS(name, qname) - is called when the parser is namespace aware and at the end of the XML element name in the namespace qname
characters(data) - is called when a chunk of data is identified by the parser. The parser may provide data in chunks
For our test, we will use the following XML named Product.xml:
The following is the python program named sample-21.py:
Execute the following command:
python sample-21.py
The following is the output:
---> Start of the Product XML ---> Start of the XML element Products ---> Attributes: ---> Start of the XML element Item ---> Attributes: Name: no, Value: 1000 ---> Start of the XML element name ---> Attributes: ---> Data = Python Quick Start - 1 ---> End of the XML element name ---> Start of the XML element quantity ---> Attributes: ---> Data = 5 ---> End of the XML element quantity ---> Start of the XML element price ---> Attributes: ---> Data = 0.99 ---> End of the XML element price ---> End of the XML element Item ---> Start of the XML element Item ---> Attributes: Name: no, Value: 1001 ---> Start of the XML element name ---> Attributes: ---> Data = Python Quick Start - 2 ---> End of the XML element name ---> Start of the XML element quantity ---> Attributes: ---> Data = 10 ---> End of the XML element quantity ---> Start of the XML element price ---> Attributes: ---> Data = 1.99 ---> End of the XML element price ---> End of the XML element Item ---> Start of the XML element Item ---> Attributes: Name: no, Value: 1002 ---> Start of the XML element name ---> Attributes: ---> Data = Python Quick Start - 3 ---> End of the XML element name ---> Start of the XML element quantity ---> Attributes: ---> Data = 15 ---> End of the XML element quantity ---> Start of the XML element price ---> Attributes: ---> Data = 2.99 ---> End of the XML element price ---> End of the XML element Item ---> End of the XML element Products ---> End of the Product XML
The Document Object Model (DOM) parser represents the XML document as a tree structure in memory and allows for easy traversal of nodes of the tree. To use the DOM parser, one needs to use the Python module xml.dom.minidom. The following are the commonly used data members and methods from the interface xml.dom.minidom:
xml.dom.minidom.DOM - represents an in-memory tree structure of the XML document
xml.dom.minidom.parse(xml) - returns a xml.dom.minidom.DOM object after parsing the specified xml file
xml.dom.minidom.DOM.documentElement - represents the root node of the xml.dom.minidom.DOM object
xml.dom.minidom.Node - represents a node in the XML document
xml.dom.minidom.Node.nodeType - data member that indicates the type of node in the XML document. An XML element is represented by the type xml.dom.minidom.Node.ELEMENT_NODE. The characters between an XML element is represented by the type xml.dom.minidom.Node.TEXT_NODE
node.hasAttributes() - returns True if the node has attributes
node.hasChildNodes() - returns True if the node has child nodes
node.attributes.items() - returns a list of attributes of the node. Each item in the list is a tuple where the first member is the name and the second is the value
node.getElementsByTagName(element) - search the descendants of the node for the element
The following is the python program named sample-22.py:
Execute the following command:
python sample-22.py
The following is the output:
---> Element Name: Item ---> Attribute Name: no, Value: 1000 ---> Element Name: name ---> Text: Python Quick Start - 1 ---> Element Name: quantity ---> Text: 5 ---> Element Name: price ---> Text: 0.99 ---> Element Name: Item ---> Attribute Name: no, Value: 1001 ---> Element Name: name ---> Text: Python Quick Start - 2 ---> Element Name: quantity ---> Text: 10 ---> Element Name: price ---> Text: 1.99 ---> Element Name: Item ---> Attribute Name: no, Value: 1002 ---> Element Name: name ---> Text: Python Quick Start - 3 ---> Element Name: quantity ---> Text: 15 ---> Element Name: price ---> Text: 2.99 ==================================================== ---> Item No: 1000 name: Python Quick Start - 1 ---> Item No: 1001 name: Python Quick Start - 2 ---> Item No: 1002 name: Python Quick Start - 3
References