c.execute('''drop table company''')
c.execute('''create table company
(id int primary key not null,
name text not null,
age int not null,
address char(50),
salary real
);''')
增
c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (1,'Paul',32,'California',20000.00)")
c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (2,'Allen',25,'Texas',15000.00)")
c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (3,'Teddy',23,'Norway',20000.00)")
c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (4,'Mark',25,'Rich-Mond ',65000.00)")
删
c.execute('delete from company where id = 4')
改
c.execute('update company set salary = 99999 where name = "allen"')
查
res = c.execute('select name,salary from company')for i in res:print(i)
范例
import sqlite3
# sql语句中无论 大小写
conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute('''drop table company''')
c.execute('''create table company
(id int primary key not null,
name text not null,
age int not null,
address char(50),
salary real
);''')# 增加
c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (1,'Paul',32,'California',20000.00)")
c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (2,'Allen',25,'Texas',15000.00)")
c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (3,'Teddy',23,'Norway',20000.00)")
c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (4,'Mark',25,'Rich-Mond ',65000.00)")# 删除
c.execute('delete from company where id = 4')# 修改
c.execute('update company set salary = 99999 where name = "allen"')# 查询
res = c.execute('select name,salary from company')for i in res:print(i)
conn.commit()
conn.close()