import sqlite3def create_table(): conn = sqlite3.connect('cybercafe.db') c = conn.cursor() c.execute('''CREATE TABLE IF NOT EXISTS customers (id INTEGER PRIMARY KEY, name TEXT, age INTEGER, gender TEXT, balance INTEGER)''') c.execute('''CREATE TABLE IF NOT EXISTS computers (id INTEGER PRIMARY KEY, brand TEXT, model TEXT, status TEXT)''') conn.commit() conn.close()def add_customer(name, age, gender, balance): conn = sqlite3.connect('cybercafe.db') c = conn.cursor() c.execute('''INSERT INTO customers(name, age, gender, balance) VALUES (?, ?, ?, ?)''', (name, age, gender, balance)) conn.commit() conn.close()def get_customers(): conn = sqlite3.connect('cybercafe.db') c = conn.cursor() c.execute('''SELECT * FROM customers''') customers = c.fetchall() conn.close() return customersdef delete_customer(customer_id): conn = sqlite3.connect('cybercafe.db') c = conn.cursor() c.execute('''DELETE FROM customers WHERE id=?''', (customer_id,)) conn.commit() conn.close()def add_computer(brand, model, status): conn = sqlite3.connect('cybercafe.db') c = conn.cursor() c.execute('''INSERT INTO computers(brand, model, status) VALUES (?, ?, ?)''', (brand, model, status)) conn.commit() conn.close()def get_computers(): conn = sqlite3.connect('cybercafe.db') c = conn.cursor() c.execute('''SELECT * FROM computers''') computers = c.fetchall() conn.close() return computersdef delete_computer(computer_id): conn = sqlite3.connect('cybercafe.db') c = conn.cursor() c.execute('''DELETE FROM computers WHERE id=?''', (computer_id,)) conn.commit() conn.close()if __name__ == '__main__': create_table()
时间: 2024-01-07 11:03:56 浏览: 100
这是一个Python程序,用于创建和管理一个名为cybercafe.db的SQLite数据库,其中包含两个表:customers和computers。下面是各个函数的功能:
- create_table(): 创建两个表,如果它们不存在。
- add_customer(name, age, gender, balance): 向customers表中添加一行,包含一个姓名、年龄、性别和余额字段。
- get_customers(): 获取customers表中的所有行。
- delete_customer(customer_id): 从customers表中删除指定id的行。
- add_computer(brand, model, status): 向computers表中添加一行,包含品牌、型号和状态字段。
- get_computers(): 获取computers表中的所有行。
- delete_computer(computer_id): 从computers表中删除指定id的行。
如果这个程序被当做主程序运行,它会调用create_table()函数来创建表。
阅读全文