Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

An hello world example.

  • Loading branch information...
commit 0f87d3454815106e19db92b2c38b299367ad4788 1 parent 95234ca
@kgaughan authored
Showing with 40 additions and 0 deletions.
  1. +10 −0 README
  2. +30 −0 docs/overview.rst
View
10 README
@@ -20,6 +20,16 @@ involved in dealing with `DB-API 2`_ compatible database drivers.
:height: 13px
:target: http://travis-ci.org/kgaughan/dbkit
+::
+
+ from dbkit import connect, query
+ from contextlib import closing
+ import sqlite3
+
+ with connect(sqlite3, 'counters.db') as ctx, closing(ctx):
+ for counter, value in query('SELECT counter, value FROM counters'):
+ print "%s: %d" % (counter, value)
+
Overview
========
View
30 docs/overview.rst
@@ -45,6 +45,36 @@ Non-aims:
.. _context: http://docs.python.org/library/contextlib.html
+Comparison with straight DB-API 2 code
+======================================
+
+Need a "Hello, World!" example? Here's how you'd set up a connection context,
+query a database table, and print out its contents with `dbkit`::
+
+ from dbkit import connect, query
+ from contextlib import closing
+ import sqlite3
+
+ with connect(sqlite3, 'counters.db') as ctx, closing(ctx):
+ for counter, value in query('SELECT counter, value FROM counters'):
+ print "%s: %d" % (counter, value)
+
+And here's how you'd so it with a DB-API 2 (using *just* :pep:`249`, no
+driver-specific extensions)::
+
+ import sqlite3
+ from contextlib import closing
+
+ with closing(sqlite3.connect('counters.db')) as conn:
+ with closing(conn.cursor()) as cur:
+ cur.execute('SELECT counter, value FROM counters')
+ while True:
+ row = cur.fetchone()
+ if row is None:
+ break
+ print "%s: %d" % row
+
+
Download
========

0 comments on commit 0f87d34

Please sign in to comment.
Something went wrong with that request. Please try again.