def list(count): conn = connect() cur = conn.cursor() query = "SELECT id,title FROM %s ORDER BY id DESC LIMIT %d" query = query % (Config.DB_TABLE, count) cur.execute(query) result = Result() result.posts = [] for row in cur.fetchall(): post = Post() post.id = row[0] post.title = row[1] result.posts += [post] return result
def get(id): conn = connect() cur = conn.cursor() cur.execute("SELECT * FROM %s WHERE id='%d'" % (Config.DB_TABLE, id)) row = cur.fetchall() if len(row) == 0: return Result() row = row[0] result = Result() post = Post() post.id = row[0] post.title = row[1] post.content = row[2] post.date = str(row[3]) result.posts = [post] return result
conn = connect() cur = conn.cursor() query = "INSERT INTO %s (title, date, content) VALUES ('%s', '%s', '%s')" query = query % (Config.DB_TABLE, title, date, content) cur.execute(query) last_id = cur.lastrowid conn.commit() except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) conn.rollback() finally: conn.close() result = Result() new_post = Post() new_post.id = last_id result.posts = [new_post] return result def get(id): conn = connect() cur = conn.cursor() cur.execute("SELECT * FROM %s WHERE id='%d'" % (Config.DB_TABLE, id)) row = cur.fetchall() if len(row) == 0: return Result() row = row[0] result = Result()