Pine Marten wrote: > 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
This whole function can be replaced with querylist = [ int(row[0]) for rowin cur.fetchall() ] PS Don't use tuple as a variable name, you will shadow the built-in tuple. Kent > > 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 > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor