As pointed out by Piotr, use of the with statement makes for much more efficient code, though it does not explicitly close the connection to the database, if this is what is desired by the user. This was found by a similar question here.
What using the with statement does do is run either the con.commit() method if the code block inside the with statement is executed without error; or the con.rollback() method if an exception is encountered.
Example from http://docs.python.org/2/library/sqlite3.html
import sqlite3
con = sqlite3.connect(":memory:")
con.execute("create table person (id integer primary key, firstname varchar unique)")
with con:
con.execute("insert into person(firstname) values (?)", ("Joe",))
# If Successful, con.commit() is called automatically afterwards
# else con.rollback() is called after the with block finishes with an exception,
# the exception is still raised and must be caught
try:
with con:
con.execute("insert into person(firstname) values (?)", ("Joe",))
except sqlite3.IntegrityError:
print "couldn't add Joe twice"
Note the use of the shortcut method con.execute() which is a method of the database connection object. This implicitly creates the cursor object for you and returns the result, and so requires even less code to be written.