1import sqlite3
2
3# Create database
4conn = sqlite3.connect('tablename.db')
5c = conn.cursor()
6c.execute('''CREATE TABLE tablename(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, number REAL)''')
7conn.commit()
8conn.close()
9
10# Insert Values
11conn = sqlite3.connect('tablename.db')
12c = conn.cursor()
13c.execute("INSERT INTO tablename VALUES (?, ?)", (name, number))
14conn.commit()
15conn.close()
16
17# Read Values
18conn = sqlite3.connect('tablename.db')
19c = conn.cursor()
20
21for row in c.execute('SELECT * FROM tablename'):
22 print(row)
23
24number = [row[2] for row in c.execute('SELECT * FROM tablename')]
25conn.close()
1import sqlite3 as lite
2import sys
3
4try:
5 con = lite.connect('products.db')
6 cur = con.cursor()
7 cur.execute("CREATE TABLE drinks(Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Price REAL)")
8 cur.execute("CREATE TABLE fruits(Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Price REAL)")
9 con.commit()
10
11except e:
12 if con:
13 con.rollback()
14
15 print("Unexpected error %s:" % e.args[0])
16 sys.exit(1)
17finally:
18 if con:
19 con.close()