用python语言写一个复杂的客户信息管理系统
时间: 2023-09-02 21:07:35 浏览: 35
好的,下面是一个比较复杂的Python客户信息管理系统示例代码,它使用SQLite数据库来存储客户信息,并包含了更多的功能:
```python
import sqlite3
conn = sqlite3.connect('customers.db')
c = conn.cursor()
# 创建表格
c.execute('''CREATE TABLE customers
(id INTEGER PRIMARY KEY, name TEXT, email TEXT, address TEXT, phone TEXT)''')
# 插入数据
def insert_customer(name, email, address, phone):
c.execute("INSERT INTO customers (name, email, address, phone) VALUES (?, ?, ?, ?)", (name, email, address, phone))
conn.commit()
# 删除数据
def delete_customer(id):
c.execute("DELETE FROM customers WHERE id=?", (id,))
conn.commit()
# 更新数据
def update_customer(id, name, email, address, phone):
c.execute("UPDATE customers SET name=?, email=?, address=?, phone=? WHERE id=?", (name, email, address, phone, id))
conn.commit()
# 查询数据
def get_customers():
c.execute("SELECT * FROM customers")
return c.fetchall()
# 根据ID查询数据
def get_customer_by_id(id):
c.execute("SELECT * FROM customers WHERE id=?", (id,))
return c.fetchone()
# 根据姓名查询数据
def get_customers_by_name(name):
c.execute("SELECT * FROM customers WHERE name LIKE ?", ('%'+name+'%',))
return c.fetchall()
# 根据邮箱查询数据
def get_customer_by_email(email):
c.execute("SELECT * FROM customers WHERE email=?", (email,))
return c.fetchone()
# 根据地址查询数据
def get_customers_by_address(address):
c.execute("SELECT * FROM customers WHERE address LIKE ?", ('%'+address+'%',))
return c.fetchall()
# 根据电话查询数据
def get_customer_by_phone(phone):
c.execute("SELECT * FROM customers WHERE phone=?", (phone,))
return c.fetchone()
# 菜单界面
while True:
print("1. 插入客户信息")
print("2. 删除客户信息")
print("3. 更新客户信息")
print("4. 查询客户信息")
print("5. 退出")
choice = int(input("请选择要执行的操作:"))
if choice == 1:
name = input("请输入客户姓名:")
email = input("请输入客户邮箱:")
address = input("请输入客户地址:")
phone = input("请输入客户电话:")
insert_customer(name, email, address, phone)
elif choice == 2:
id = int(input("请输入要删除的客户ID:"))
delete_customer(id)
elif choice == 3:
id = int(input("请输入要更新的客户ID:"))
customer = get_customer_by_id(id)
if customer:
name = input("请输入新的客户姓名(原值为{}):".format(customer[1]))
email = input("请输入新的客户邮箱(原值为{}):".format(customer[2]))
address = input("请输入新的客户地址(原值为{}):".format(customer[3]))
phone = input("请输入新的客户电话(原值为{}):".format(customer[4]))
update_customer(id, name, email, address, phone)
else:
print("无效的客户ID")
elif choice == 4:
print("1. 根据ID查询")
print("2. 根据姓名查询")
print("3. 根据邮箱查询")
print("4. 根据地址查询")
print("5. 根据电话查询")
sub_choice = int(input("请选择查询方式:"))
if sub_choice == 1:
id = int(input("请输入客户ID:"))
customer = get_customer_by_id(id)
if customer:
print(customer)
else:
print("无效的客户ID")
elif sub_choice == 2:
name = input("请输入客户姓名:")
customers = get_customers_by_name(name)
for customer in customers:
print(customer)
elif sub_choice == 3:
email = input("请输入客户邮箱:")
customer = get_customer_by_email(email)
if customer:
print(customer)
else:
print("无效的客户邮箱")
elif sub_choice == 4:
address = input("请输入客户地址:")
customers = get_customers_by_address(address)
for customer in customers:
print(customer)
elif sub_choice == 5:
phone = input("请输入客户电话:")
customer = get_customer_by_phone(phone)
if customer:
print(customer)
else:
print("无效的客户电话")
else:
print("无效的选择")
elif choice == 5:
break
else:
print("无效的选择")
conn.close()
```
这个示例代码包含了更多的功能,如根据不同的条件查询客户信息,以及根据客户ID进行更新操作。您可以根据自己的需求进行修改和扩展。同时,您需要确保输入的数据是安全的,避免SQL注入攻击。