Tuesday, March 23, 2010

SQLite to CSV in Python

To begin this mission, I know nothing about Python.

In 20 minutes, I have a sqlite database outputting data to a csv file.

WOW!

import sqlite3

conn = sqlite3.connect('C:/SQLite/db.sql')
curse = conn.cursor()

data = curse.execute('select * from Data where DATETIME([DateTime]) >= DATETIME("2010-03-20 05:00:00") AND DATETIME([DateTime]) <= DATETIME("2010-03-20 15:00:00");')

f = open('output.csv', 'w')

for row in data:
print >> f, row


------


7 lines of code. Awesome.

No comments:

Post a Comment