MySQL for the Internet of Things

(Steven Felgate) #1

Chapter 6 ■ Building low-Cost MysQl data nodes


You can do much more with the connector than shown here. In fact, you can query the database server
for lookup data; discover values of variables; create databases, tables, views; and so on. You can do just
about anything you want, within reason. Querying the database server for massive rows or large data rows is
likely beyond the memory limitations of the Arduino. Fortunately, most of the sketches you will write will be
saving data with simple INSERT statements.


■Tip you can find many more examples of how to use the connector in the Connector/arduino reference


manual located in the extras folder. the file is named MySQL_Connector_Arduino_Reference_Manual.pdf.


Now that you’ve seen an Arduino connector, let’s look at a connector more general purpose and one
you can use on your laptop, desktop, low-cost computer, embedded system, and more, Anything that can
run Python can write to a MySQL database!


Introducing Connector/Python


The connector for Python from Oracle is a full-featured connector that provides connectivity to the MySQL
database server for Python applications and scripts. The latest version is release-2.1.3GA. Unlike the
Connector/Arduino, Connector/Python is fully supported and actively maintained by Oracle.
Connector/Python features support for all current MySQL server releases from version 4.1 and newer.
It is written to provide automatic data type conversion between Python and MySQL, making building
queries and deciphering results easy. It also has support for compression, permits connections via SSL, and
supports all MySQL SQL commands. The current version, 2.1.3, is augmented with C libraries to improve
performance.
Using Connector/Python in your Python scripts consists of importing the base module, initiating
a connection, and executing queries with a cursor, which is similar to Connector/Arduino. That is not
surprising since I wrote Connector/Arduino using Connector/Python as a model.
However, unlike Connector/Arduino, Connector/Python has no such memory limitations, allowing you
to accomplish a great deal of processing. Indeed, I would move most of my string, date, and mathematical
processing to a Python script rather than attempting it on the Arduino.
Before we jump into how we can use Connector/Python to write some MySQL database–enabled
applications, let’s talk about how to get and install Connector/Python.


pYthON? ISN’t that a SNaKe?


the python programming language is a high-level language designed to be as close to like reading
english as possible while being simple, easy to learn, and powerful. pythonistas^17 will tell you the
designers have indeed met these goals.

python does not require a compilation step prior to being used. rather, python applications (whose
file names end in .py) are interpreted on the fly. this is very powerful, but unless you use a python
integrated development environment (ide) that contains an automatic syntax checker, some syntax
errors will not be discovered until the application is executed. Fortunately, python provides a robust
exception-handling mechanism that will communicate what has gone wrong.

(^17) Python experts often refer to themselves using this term. It is reserved for the most avid and experienced Python
programmers.

Free download pdf