neuroradiology / prettytable

Automatically exported from code.google.com/p/prettytable
Other
0 stars 0 forks source link

Not printing out table in ordered rows #47

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
# Import the SQLite3 module
import sqlite3
db = sqlite3.connect('book.db')
db_cur = db.cursor()
db_cur.execute('''CREATE TABLE IF NOT EXISTS novel(id INTEGER PRIMARY KEY, 
title TEXT, author TEXT, year INT)''')
novel = [
    ('As I Lay Dying', 'William Faulkner', '1930'), 
    ('Lullaby', 'Chuck Palanhuik', '2003'), 
    ('Fight Club', 'Chuck Palanhuik', '2000'), 
    ('Sex','Madonna', '1998'),
    ('Survivor','Chuck Palanhuik', '2002'),
    ('Cats Cradle', 'Kurt Vonnegut', '1967'),
    ('Slaughterhouse Five', 'Kurt Vonnegut', '1956')
]

db_cur.executemany('''INSERT INTO novel("TITLE", "AUTHOR", "YEAR") 
VALUES(?,?,?)''', novel)
db.commit()

print "Operation done successfully";
from prettytable import from_db_cursor
db.execute("SELECT * FROM novel")
pt = from_db_cursor(db_cur)

db.close()     
print pt.get_string(sortby = "TITLE")

On Python 2.7, this brings back as AttributeError: 'NoneType' object has no 
attribute 'get_string'.

If I just print pt,

I get this:

+-----+---------------------+------------------+------+
|  id |        title        |      author      | year |
+-----+---------------------+------------------+------+
|  1  |    As I Lay Dying   | William Faulkner | 1930 |
|  2  |       Lullaby       | Chuck Palanhuik  | 2003 |
|  3  |      Fight Club     | Chuck Palanhuik  | 2000 |
|  4  |         Sex         |     Madonna      | 1998 |
|  5  |    As I Lay Dying   | William Faulkner | 1930 |
|  6  |       Lullaby       | Chuck Palanhuik  | 2003 |
|  7  |      Fight Club     | Chuck Palanhuik  | 2000 |
|  8  |         Sex         |     Madonna      | 1998 |
|  9  |    As I Lay Dying   | William Faulkner | 1930 |
|  10 |       Lullaby       | Chuck Palanhuik  | 2003 |

all the way to like 300. there are only seven entries.

HELP!!

Original issue reported on code.google.com by hwi...@gmail.com on 18 Jan 2014 at 1:58

GoogleCodeExporter commented 8 years ago
Nevermind. I figured it out. Thanks.

Original comment by hwi...@gmail.com on 18 Jan 2014 at 2:57

GoogleCodeExporter commented 8 years ago
What was the problem?  I can see that sortby="TITLE" should have been 
sortby="title", but that doesn't explain why pt would have been equal to None...

By the way, if you're going to do something like this on a large scale, it may 
be faster to get SQLite to do the sorting, rather than PrettyTable.

Original comment by luke@maurits.id.au on 19 Jan 2014 at 7:45

GoogleCodeExporter commented 8 years ago

Original comment by luke@maurits.id.au on 16 May 2014 at 10:29