Home arrow Python arrow Page 2 - Using SQLite in Python

Connecting to a Database - Python

SQLite is a small C library that implements a self-contained SQL database engine. This article examines pysqlite, one of a number of libraries in Python, that is used as an interface with SQLite.

  1. Using SQLite in Python
  2. Connecting to a Database
  3. Writing Data
  4. Retrieving Data
  5. Adapting and Converting
By: Peyton McCullough
Rating: starstarstarstarstar / 132
April 12, 2006

print this article



Now we're ready to begin working with pysqlite. To start using the library, it, of course, needs to be imported. It can be found under the name of pysqlite2, but we'll need the dbapi2 submodule. This should be imported under the name of sqlite for clarity:

>>> from pysqlite2 import dbapi2 as sqlite

Now we can begin using pysqlite. The first thing we'll do is create a new database to work with. This is done by creating a connection object and passing the name of a database file. If the database file does not exist, which, in our case, it doesn't, then it will be created. If it does exist, then it will be opened:

>>> connection = sqlite.connect('test.db')

It's also possible to create a temporary database that exists in memory:

>>> memoryConnection = sqlite.connect(':memory:')

Once we have a connection object, we must create a cursor object, which does the interaction for us. This is done by calling the cursor method of our connection object:

>>> cursor = connection.cursor()

>>> More Python Articles          >>> More By Peyton McCullough

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: