[Tutor] sqlite3 format data from tables

Alan Gauld alan.gauld at btinternet.com
Sun May 24 00:20:58 CEST 2009


"David" <david at abbottdavid.com> wrote

> I can not figure out how to get the transaction details to return so that 
> it looks nice. It returns like this now.
>
> Your transaction History is: [(1, u'Food', -100), (2, u'Deposit', -200), 
> (3, u'Deposit', 500), (4, u'Python Book', -50)]

You are just printing raw data as retuirned from the cursor.
Databases are good at storing and retrieving data.
They are terrible at formatting it. (Even when you use
the formatting features of some SQLs)

You need to get the raw data and format it how you want it
yourself. You could do that in your displaydata function
(since display implies some fancy formatting) or even
in your report data (although here I'd more likely expect
you to structure the data in a dictionary or somesuch)

> Here is how it gets to that point;
>
> def get_transactions(self):
>     ct=self.connection.cursor()
>     ct.execute("select * from transact;")
>     return ct.fetchall()
>
> def report_transactions(self):
>     return self.database.get_transactions()

Unless you intend doing something useful this function
is just an extra later of processing!

> def display_transactions(self):
>     print "Your transaction History is:",self.bl.report_transactions()

This is where you need todeciode how the data should look and
format it accordingly. The best way to dio that is probably using
a format string which will let you set the field length, justification etc.
(Or if using Python v3 the even more powerful new formatting
features there)


> self.menu[5]=("Transaction History",self.display_transactions)
>
> Can I use the numbers 1,2,3,4 as a way to return the history?

I don;t really understand what you mean by this bit?


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list