Database (SQL)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import sqlite3

conn = sqlite3.connect('single')

c = conn.cursor()

c.execute("""drop table if exists towns""")


conn.commit()

c.execute("""create table towns (
        tid     int     primary key not NULL ,
        name    text,
        population int,
        postcode        text)"""
)


c.execute("""insert into towns values (1, "Melksham", 5000, "SN12")""")
c.execute("""insert into towns values (2, "Cambridge", 6400, "CB1")""")
c.execute("""insert into towns values (3, "Foxkilo",4444,"CB22")""")
c.execute("""insert into towns values (4, "Sampsted",1800, "SMT££")""")

conn.commit()

#c.execute("""select * from towns where towns.population >'2000'""")
#c.execute("""select * from towns where towns.name ='Melksham'""")
c.execute("""select * from towns""")


for row in c:
        #print ("The Town: ",row[1], row[2])
        print(row)

c.close()
Lesson Topics