Open jzztf opened 6 years ago
import sqlite3 # 建立连接 conn = sqlite3.connect('grade_1.db') # 创建游标对象 c = conn.cursor() # 为数据库添加表 c.execute("CREATE TABLE IF NOT EXITSTS class_1 VALUES(id INTEGER,name TEXT,math REAL,Chinese REAL,English REAL) ") # INTEGER ==> int; TEXT ==> str; REAL ==> float; BLOB ==> bytes; NULL ==> None # 为表添加数据 c.execute("INSERT INTO class_1 VALUES(1,'张三',80,90,88)") # 提交 conn.commit() # 为表添加多行数据(引用Python变量,使用"?"而不是"%s") c.execute("INSERT INTO class_1 VALUES(?,?,?,?,?)",(id,name,math,Chinese,English)) # 提交 conn.commit() # 查看数据 c.execute("SELECT * FROM class_1 WHERE math> 90 ORDER BY id) # c,fetchone() 只取一行 for row in c.fetchall(): print(row) # 更新数据库 c.execute("UPDATE class_1 SET math=88 WHERE name='zhangsan') conn.commit() # 删除行 c.execute("DELETE class_1 WHERE id=0) conn.commit() # 关闭数据库 c.close() conn.close()
sqlite3 数据库