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

(singke) #1
queries you for a password for the MySQL root user account. The MySQL server
maintains its own set of user accounts and passwords, separate from the Linux system
user accounts. The root user account in the MySQL server has total control over the
entire MySQL server. Be sure to remember the password you assign to the MySQL
root user account!

When the installation process finishes, the MySQL database server program automatically starts
running in background mode. You’re all ready to start setting up your MySQL database for your
Python application.


Setting Up the MySQL Environment


Before you can start writing your Python scripts to interact with a database, you need a few database
objects to work with. At a minimum, you’ll want to have these:


A unique database to store your application data
A unique user account to access the database from your scripts
One or more data tables to organize your data

You build all these objects by using the mysql command-line client program. The mysql program
interfaces directly with the MySQL server, using SQL commands to create and modify each of the
objects.


Most of the interactions you make with the database are performed using SQL statements. The SQL
language is an industry standard for communicating with different types of databases. You can send
any type of SQL statement to the MySQL server by using the mysql program. The following sections
walk through the different SQL statements you’ll need to build the basic database objects for your
shell scripts.


Creating a Database


The MySQL server organizes data into databases. A database usually holds the data for a single
application, separating it from other applications that use the database server. Creating a separate
database for each Python application helps eliminate confusion and data mix-ups.


You need to use this SQL statement to create a new database:


CREATE DATABASE name;

This is pretty simple. Of course, you must have the proper privileges to create new databases on the
MySQL server. The easiest way to ensure that you do is to log in as the root user account:


Click here to view code image


pi@raspberrypi ~ $ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 51
Server version: 5.5.31-0+wheezy1 (Debian)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Free download pdf