Database access
Talking to databases is a very common task in Python, especially in the web development world. Unfortunately, not many libraries for database access have been ported to Python 3 in a mature state. We'll be looking at a few of the available database solutions.
Python comes with built-in support for SQLite 3. We looked at some examples of it in earlier chapters. SQLite is not suitable for multi-user, multi-threaded access, but it's perfect for storing configuration or local data. It simply stores all the data in a single file and allows us to access that data using SQL syntax. All we need to do to use it is import sqlite3
and read the help file. Here's a short example to get you started:
import sqlite3 connection = sqlite3.connect("mydb.db") connection.execute( "CREATE TABLE IF NOT EXISTS " "pet (type, breed, gender, name)") connection.execute("INSERT INTO pet VALUES(" "'dog', 'spaniel', 'female', 'Esme')") connection.execute("INSERT INTO pet VALUES("...