In [10]:
import sqlite3
In [11]:
db = "./mydb.db"
conn = sqlite3.connect(db)
c = conn.cursor()
In [ ]:
cmd = "CREATE TABLE my_table (Name TEXT NOT NULL,\
Age INTEGER NOT NULL)"
c.execute(cmd)
In [13]:
conn.commit()
In [15]:
cmd = "ALTER TABLE my_table ADD COLUMN Salary INTEGER NOT NULL DEFAULT 0"
c.execute(cmd)
conn.commit()
In [20]:
cmd = "INSERT INTO my_table VALUES ('Joe', 32, 5000)"
c.execute(cmd)
conn.commit()
In [23]:
cmd = "INSERT INTO my_table VALUES ('{}', {}, {})".format('Kat', 50, 7000)
print cmd
c.execute(cmd)
conn.commit()
In [28]:
names = [ \
('James', 26, 2000),
('Lot', 66, 9000),
('Ian', 36, 4000),
]
c.executemany("INSERT INTO my_table VALUES (?,?,?)",names)
conn.commit()
In [29]:
conn.close()
In [30]:
db = "./mydb.db"
conn = sqlite3.connect(db)
c = conn.cursor()
In [31]:
c.execute("SELECT * from my_table where Name='Joe'")
result = c.fetchall()
print result
In [33]:
c.execute("SELECT * from my_table where Age > 30")
result = c.fetchall()
print result
print result[2]
In [34]:
conn.close()
In [ ]: