In most cases when you are doing database programming, the database will be up and running. Your website hosting, for example, may have MySQL database access included. Your IT department may have standardized on a particular database, such as Oracle, DB/2, Sybase, or Informix.
Python Database Programming: Sqlite
But if you have no database yet, a good starting database is Sqlite. The main advantages of Sqlite are that it comes installed with Python and it is simple, yet functional. Using Sqlite is as simple as importing a module (sqlite3).
If you are working with another database, such as SQL Server, then the chances are good that a database has already been created. If not, follow the instructions from you database vendor.
import os import sqlite3 conn = sqlite3.connect('my_database') cursor=conn.cursor() # Create tables cursor.execute(""" create table player (idnum integer, lastname varchar, firstname varchar, age integer, team integer, lefthanded varchar, totalwar float(5,1), earliestfreeagent integer) """) cursor.execute(""" create table team (teamid integer, teamname varchar, teamballpark varchar) """) # Create indices cursor.execute("""create index teamid on team (teamid)""") cursor.execute("""create index idnum on player (idnum)""") cursor.execute("""create index teamidfk on player(team)""") conn.commit() cursor.close() conn.close()
This script should produce no output unless it raises an error. It uses the Sqlite API, which is somewhat similar to the DB API. After the import statements, the script creates a database connection with this statement:
conn = sqlite3.connect('my_database')
From there, the script gets a Cursor object. The Cursor object is used to create two tables (the Player and Team tables from the previous two articles) and define indexes on these tables.
The script then calls the commit method on the connection to save all the changes to disk. Sqlite stores all the data in the file my_database, which you should see in your Python directory.
Python’s support for relational databases started with ad hoc solutions, with one solution written to interface with each particular database, such as Oracle. Each database module created its own API, which was highly specific to that database because each database vendor evolved its own API based on its own needs. This can be difficult to support, because moving from one database to another requires that everything be rewritten and retested.
Over the years, Python has come to support a common database known as the DB API. Specific modules enable your Python scripts to communicate with different databases, such as DB/2, PostgreSQL, and so on. All of these modules support the common API, which makes your job a lot easier when you write scripts to access databases.
The DB API provides a minimal standard for working with databases, using Python structures and syntax wherever possible. This API includes the following:
- Connections, which cover guidelines for how to connect to databases
- Executing statements and stored procedures to query, update, insert and delete data with cursors
- Transactions, with support for committing or rolling back a transaction
- Examining metadata on the database module as well as on database and table structure
- Defining the types of errors
In the next article, we will begin our look at the DB API.
External Links:
Database Programming at wiki.python.org
Database Programming at python.about.com
Databases at docs.python-guide.org
The post Python Database Programming: Part Five appeared first on PythonScript HQ.