Wednesday, February 24, 2010

sqlite in Python

Sqlite is a very useful server-less database system that can be accessed from Python as well.

By the way you can combine multiple insert statements in a single transaction to improve performace
as described here.

import sqlite3

def createDB():
    db = sqlite3.connect('people.db')
    db.execute('create table boys(name, age)')
    db.commit()
    db.close()
    pass

def writeDB():
    db = sqlite3.connect('people.db')
    db.execute('insert into boys(name, age) values ("danushka",30)')
    db.commit()
    db.close()
    pass

def readDB():
    db = sqlite3.connect('people.db')
    L = db.execute('select name, age from boys').fetchall()
    print L
    db.close()
    pass

if __name__ == "__main__":
    #createDB()
    #writeDB()
    readDB()

No comments:

Post a Comment

Continuously monitor GPU usage

 For nvidia GPUs do the follwing: nvidia-smi -l 1