[Tutor] using SQLite with matplotlib - queries vs. lists

Pine Marten pine508 at hotmail.com
Tue Feb 20 21:33:46 CET 2007


I'm a novice hoping to use data stored in an SQLite database to make simple 
graphs using matplotlib embedded in a wxPython GUI.  I noticed that 
matplotlib uses lists of integers to make graphs, such as the simple example 
(from the tutorial) of:

from pylab import *
plot([1,2,3,4])
show()

But SQLite queries return as a list of tuples in rows, not a list of 
numbers.  For some reason I haven't found a way any ready-made converter for 
this purpose.  As an exercise, I've wrote this function that converts them:

#takes the result of a fetchall() in sql and returns it as a list of numbers
#assumes you've done the query first, i.e. cur.execute('SELECT * FROM 
table')
import sqlite3
import string
def query_to_list():
    queryresult = cur.fetchall()
    print "Queryresult is ", queryresult
    queryrows = []
    for tuple in queryresult:
        num = tuple[0]
        queryrows.append(num)
    querylist = []
    for row in queryrows:
        num = int(row)
        querylist.append(num)
    print "Query list is: ", querylist

My question is, is there a better way to do this, or is there a ready-made 
way to go from an sql database to matplotlib?  I tried searching for it in 
various ways but haven't found anything.  I feel as though there should be a 
more direct way to go from a db to a plot.

Thanks in advance,
Chae

_________________________________________________________________
Mortgage rates as low as 4.625% - Refinance $150,000 loan for $579 a month. 
Intro*Terms  http://www.NexTag.com



More information about the Tutor mailing list