import sqlite3
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS teachers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
subject TEXT,
salary REAL
)
''')
conn.commit()
conn.close()
这段代码会创建一个名为`teachers`的表。
def add_teacher(name, subject, salary):
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
cursor.execute("INSERT INTO teachers (name, subject, salary) VALUES (?, ?, ?)", (name, subject, salary))
conn.commit()
conn.close()
def list_teachers():
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM teachers")
rows = cursor.fetchall()
for row in rows:
print(row)
conn.close()