sql = "CREATE TABLE person(id varchar(20) PRIMARY KEY, name varchar(20));"
# 创建表 cs.execute(sql)
# 提交当前事务 conn.commit()
1 2 3 4 5 6 7 8 9
# 新增 cs.execute("INSERT INTO person (id, name) VALUES ('1', '张三')") cs.execute("INSERT INTO person (id, name) VALUES ('2', '李四')") cs.execute("INSERT INTO person (id, name) VALUES ('3', '王五')") cs.execute("INSERT INTO person (id, name) VALUES ('4', '赵六')") cs.execute("INSERT INTO person (id, name) VALUES ('5', '朱七')")
# 提交当前事务 conn.commit()
查询
1 2 3 4 5 6 7 8 9 10
cs.execute("SELECT id, name FROM person")
# 获取查询结果集中的下一行 print(cs.fetchone())
# 获取查询结果集中的下几行 print(cs.fetchmany(2))
# 获取查询结果集中剩下的所有行 print(cs.fetchall())
修改
1 2 3
# 修改 cs.execute("UPDATE person set name = '张四' WHERE id = '1'") conn.commit()
删除
1 2 3
# 删除 cs.execute("DELETE FROM person WHERE id = '3'") conn.commit()