基于模块的简易通讯录管理系统代码
时间: 2024-01-17 14:13:32 浏览: 108
以下是一个基于模块的简易通讯录管理系统的代码示例:
input_module.py:
```python
def input_contact_info():
name = input("请输入联系人姓名:")
phone = input("请输入联系人电话:")
email = input("请输入联系人邮箱:")
return (name, phone, email)
```
output_module.py:
```python
def output_contact_info(contact):
print("联系人姓名:", contact[0])
print("联系人电话:", contact[1])
print("联系人邮箱:", contact[2])
```
data_module.py:
```python
import sqlite3
def create_table():
conn = sqlite3.connect("contacts.db")
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS contacts (name TEXT, phone TEXT, email TEXT)")
conn.commit()
conn.close()
def add_contact(contact):
conn = sqlite3.connect("contacts.db")
cursor = conn.cursor()
cursor.execute("INSERT INTO contacts VALUES (?, ?, ?)", contact)
conn.commit()
conn.close()
def delete_contact(name):
conn = sqlite3.connect("contacts.db")
cursor = conn.cursor()
cursor.execute("DELETE FROM contacts WHERE name=?", (name,))
conn.commit()
conn.close()
def update_contact(contact):
conn = sqlite3.connect("contacts.db")
cursor = conn.cursor()
cursor.execute("UPDATE contacts SET phone=?, email=? WHERE name=?", contact)
conn.commit()
conn.close()
def search_contact(name):
conn = sqlite3.connect("contacts.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM contacts WHERE name=?", (name,))
result = cursor.fetchone()
conn.close()
if result:
return result
else:
return None
def get_all_contacts():
conn = sqlite3.connect("contacts.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM contacts")
results = cursor.fetchall()
conn.close()
return results
```
logic_module.py:
```python
import input_module
import output_module
import data_module
def add_contact():
contact = input_module.input_contact_info()
data_module.add_contact(contact)
def delete_contact():
name = input("请输入要删除联系人的姓名:")
data_module.delete_contact(name)
def update_contact():
name = input("请输入要修改联系人的姓名:")
contact = input_module.input_contact_info()
contact = (contact[1], contact[2], contact[0])
data_module.update_contact(contact)
def search_contact():
name = input("请输入要查找联系人的姓名:")
contact = data_module.search_contact(name)
if contact:
output_module.output_contact_info(contact)
else:
print("未找到联系人信息")
def show_all_contacts():
contacts = data_module.get_all_contacts()
for contact in contacts:
output_module.output_contact_info(contact)
```
通过上述四个模块的组合,我们可以实现一个简单的通讯录管理系统,具有增删改查和查看所有联系人等功能。
阅读全文