Document common.getnumposts().
[bloggy.git] / common.py
blobb9c0bde7c125b10c6246dc7ad825ac90dbbb5624
1 import sqlite3
2 import sys
4 import config
6 def connect():
7 try:
8 conn = sqlite3.connect(config.DBPATH)
9 conn.execute("CREATE TABLE IF NOT EXISTS entries (id INTEGER PRIMARY KEY, date , title TEXT, text TEXT)")
10 except sqlite3.OperationalError:
11 print 'Failed to connect to database. Check file and directory permissions.'
12 sys.exit(1)
13 return conn
15 def getnumposts(conn, id=None):
16 """Enumerate the number of posts in the database. if an ID is specified
17 then enumerate the number of posts with that ID. The latter should be 0 or
18 1 so essentially this is a check for whether the specified post exists.
19 """
20 if id:
21 numposts = conn.execute("SELECT count(id) FROM entries WHERE id = ?", (id,)).fetchone()
22 else:
23 numposts = conn.execute("SELECT count(id) FROM entries").fetchone()
24 return int(numposts[0])