Python sqlite3 Basic tutorial

#working with SQL Lite 3 using python
import sqlite3
def main():
#This will connect to the Database if it exists or will create a new database
db = sqlite3.connect(‘test.db’)
#Use execute method to run queries on the database
db.execute(‘drop table if exists test’)
#Creating a table with the name tets
db.execute(‘create table test (t1 text, i1 integer)’)
db.execute(‘insert into test values (? , ?)’,(‘one’,1))
#As it is a transactional database you need to commit whenever you change data
db.commit()
#Read from the database
cursor = db.execute(‘select * from test’)
for row in cursor:
print(row)

if __name__ == ‘__main__’: main()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s