sqlite python测试
import sqlite3
# 连接到 SQLite 数据库
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
# 创建表,如果表不存在则创建
cursor.execute('''CREATE TABLE IF NOT EXISTS stocks
(date text, trans text, symbol text, qty real, price real)''')
# 插入数据
cursor.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
stocks = [
('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
]
cursor.executemany("INSERT INTO stocks VALUES (?,?,?,?,?)", stocks)
# 查询数据
cursor.execute("SELECT * FROM stocks WHERE symbol='RHAT'")
rows = cursor.fetchall()
for row in rows:
print(row)
# 更新数据
cursor.execute("UPDATE stocks SET qty=200 WHERE symbol='RHAT'")
# 删除数据
cursor.execute("DELETE FROM stocks WHERE symbol='MSOFT'")
# 提交事务
conn.commit()
# 关闭连接
conn.close()