Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

The empid data field definition also specifies a data constraint. A data constraint restricts what type
of data you can enter to create a valid record. The not null data constraint indicates that every
record must have an empid value specified.


Finally, the primary key line defines a data field that uniquely identifies each individual record.
This means that each data record must have a unique empid value in the table.


After you create the new table, you can use the SHOW TABLE command to ensure that it’s created.


Installing the Python MySQL Module


To get your Python scripts to communicate with the MySQL server, you need to use a Python
MySQL/Connector module. This is where things get a little interesting.


There are quite a few different Python modules for communicating with MySQL servers, but
unfortunately, not very many of them have been ported to the Python v3 world yet. The
MySQL/Connector module, created by the developers of MySQL, has been ported to the Python
v3 world, so you can use that in your Python 3 scripts.


The downside is that at the time of this writing, the MySQL/Connector module for Python v3 isn’t
available in the standard Debian Linux software repository yet, so it’s also not available in the
Raspbian software repository. However, you can download the package from the Debian
Experimental software repository and install it on your Raspberry Pi.


Try It Yourself: Install the Python v3 MySQL/Connector Module
In the following steps, you’ll install the MySQL/Connector module for Python v3
on your Raspberry Pi system. Here’s what you do:


  1. Open a browser window and navigate to this URL:
    http://packages.debian.org/experimental/python3-mysql.connector

  2. In the “Download python3-mysql.connector” section, click the All link. That should
    take you to the following URL:
    ://packages.debian.org/experimental/all/python3-mysql.connector/download

  3. Click a link to download the package from a repository close to your location. At the
    time of this writing, this is the download file name:
    Click here to view code image
    python3-mysql.connector_1.0.9-1_all.deb

  4. Use the dpkg command to install the Debian package, like this:
    Click here to view code image
    pi@raspberrypi ~ $ sudo dpkg –i python3-mysql.connector_1.0.9-1_all.deb

  5. Open a Python v3 command-line session and try to import the mysql.connector
    module, as follows:
    Click here to view code image
    pi@raspberrypi ~ $ python3
    Python 3.2.3 (default, Mar 1 2013, 11:53:50)
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.



    import mysql.connector






Free download pdf