Python Tutorial

Friday, December 9, 2011

Sqlite in python

Using Sqlite in python is very simple. Sqlite installed default with python 2.5+
I use SQLite Manager for sqlite
Lets goto the example:


import sqlite3 as sqlite
#Connect to the file 'pythonSqliteExample.sqlite'
connection = sqlite.connect("pythonSqliteExample.sqlite")
#Get the cursor
cur=connection.cursor()
cur.execute("select * from info")

for row in cur:
    print "name: ",row[0]," address ",row[1]," score",row[2]

cur.execute("insert into info values ('TJ','BD',50)")
#This is for save the chages
connection.commit()
print "After Commit"
cur.execute("select * from info")

for row in cur:
    print "name: ",row[0]," address ",row[1]," score",row[2]

connection.close()
 


Output

 
name:  tom  address  bangladesh  score 10
name:  jerry  address  bangladesh  score 5.5

After Insert
name:  tom  address  bangladesh  score 10
name:  jerry  address  bangladesh  score 5.5
name:  TJ  address  BD  score 50
 
 


I upload full code with database here You can use it.