[Tutor] building database with sqlite3 and matplotlib

Kent Johnson kent37 at tds.net
Sat Aug 8 20:28:15 CEST 2009


On Fri, Aug 7, 2009 at 5:35 PM, David Kim<davidkim05 at gmail.com> wrote:

> I've been learning python in a vacuum for the past few months and I
> was wondering whether anyone would be willing to take a look at some
> code? I've been messing around with sqlite and matplotlib, but I
> couldn't get all the string substitution (using ?s).
> The code can be found at
> http://notestoself.posterous.com/use-python-and-sqlite3-to-build-a-database-co
> A short summary of what I did is at
> http://notestoself.posterous.com/use-python-and-sqlite3-to-build-a-database
>
> (Or should I have pasted the code in this message?)

No, it's a bit long for that. pastebin.com is another option that
doesn't require others to download the code.

> I've been trying to learn from books, but some critique would be very
> appreciated.

Mostly it looks pretty good.

    sql = "select asset_id from assets where ticker='%s'" % ticker #change this!
    c.execute(sql)

Try (I think, I don't have DB-API docs at the moment...)

    sql = "select asset_id from assets where ticker='?'"
    c.execute(sql, [ticker])

        date_raw = datetime.datetime.fromordinal(int(quote[0]))
        year, month, day = date_raw.year, date_raw.month, date_raw.day
        date_string = str(year)+'-'+str(month)+'-'+str(day)

See datetime.strptime() for simpler date formatting

    marks = len(data_dict['headers'])*'?,'
    marks_string = marks[:-1]

or

  marks = ','.join(['?'] * len(data_dict['headers']))

if (__name__ == '__main__'):
    main()

You don't seem to actually have a main(). Are you running this by importing it?

I would make a separate function to create the database and call that
from build_database().

Kent


More information about the Tutor mailing list