diff --git a/cli/worblehat.py b/cli/worblehat.py index 9aab9f3..e3d3f42 100644 --- a/cli/worblehat.py +++ b/cli/worblehat.py @@ -23,9 +23,16 @@ q_list_books = \ 'LEFT JOIN bookperson ON book.isbn=bookperson.book ' \ 'LEFT JOIN person ON bookperson.person=person.id' q_list_persons = \ - 'SELECT person.id, person.firstname, person.lastname, COUNT(bookperson.id) AS num_books ' \ + 'SELECT person.id, person.firstname, person.lastname, ' \ + ' COUNT(bookperson.id) AS num_books ' \ 'FROM person LEFT JOIN bookperson ON person.id=bookperson.person ' \ 'GROUP BY person.id, person.firstname, person.lastname' +q_list_categories = \ + 'SELECT category.id, category.name, ' \ + ' COUNT(book.isbn) AS num_books ' \ + 'FROM category ' \ + 'LEFT JOIN book ON category.id=book.category ' \ + 'GROUP BY category.id, category.name' q_persons_for_book = \ 'SELECT person.id, lastname, firstname, relation ' \ 'FROM person ' \ @@ -186,7 +193,11 @@ def list_persons(connection): person['num_books']) def list_categories(connection): - pass + c = connection.cursor() + c.execute(q_list_categories) + for i in xrange(c.rowcount): + cat = fetchone_dict(c) + print '%-15s %-30s %d books' % (cat['id'], cat['name'], cat['num_books']) def list_cmd(connection, what): funs = { 'book': list_books,