1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  | with lite.connect('test.sqlite') as con:
    cur = con.cursor()
    cur.execute("DROP TABLE IF EXISTS PhoneAddress")#会舍弃掉已存在的PhoneAddress表格
    cur.execute(
        "CREATE TABLE PhoneAddress(phone CHAR(10) PRIMARY KEY, address TEXT, name TEXT unique, age INT NOT NULL)")
    cur.execute("INSERT INTO PhoneAddress VALUES('0912173381','United State','Jhon Doe',53)")
    #也可写为:
    #cur.execute("INSERT INTO PhoneAddress(phone,address,name,age) VALUES('0912173381','United State','Jhon Doe',53)")
    cur.execute("INSERT INTO PhoneAddress VALUES('0928375018','Tokyo Japan','MuMu Cat',6)")
    cur.execute("INSERT INTO PhoneAddress VALUES('0957209108','China','Zhang San',29)")
    cur.execute("SELECT phone,address FROM PhoneAddress")
    #取出所有栏位:
    #cur.execute("SELECT * FROM PhoneAddress")
    
    #fetchone一次只取一组数据
    #data = cur.fetchone()#data为:('0912173381', 'United State')
    #fetchall一次取所有数据
    data = cur.fetchall()#data为:[('0912173381', 'United State'), ('0928375018', 'Tokyo Japan'), ('0957209108', 'China')]
    for rec in data:
        print(rec[0], rec[1])
 |