编写一个基于SQLite3的通讯录管理系统。本系统能实现添加记录、修改记录、删除记录、查询记录基本功能。
时间: 2024-05-12 19:15:00 浏览: 112
好的,我可以为您提供一个Python实现的基于SQLite3的通讯录管理系统。在开始之前,你需要先安装sqlite3和prettytable库。
代码如下:
```python
import sqlite3
from prettytable import PrettyTable
# 连接数据库
conn = sqlite3.connect('address_book.db')
c = conn.cursor()
# 创建表
c.execute('''CREATE TABLE IF NOT EXISTS contacts
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
phone TEXT NOT NULL,
email TEXT)''')
# 添加记录
def add_contact(name, phone, email):
c.execute("INSERT INTO contacts (name, phone, email) VALUES (?, ?, ?)", (name, phone, email))
conn.commit()
# 修改记录
def update_contact(id, name, phone, email):
c.execute("UPDATE contacts SET name=?, phone=?, email=? WHERE id=?", (name, phone, email, id))
conn.commit()
# 删除记录
def delete_contact(id):
c.execute("DELETE FROM contacts WHERE id=?", (id,))
conn.commit()
# 查询记录
def search_contacts(keyword):
c.execute("SELECT * FROM contacts WHERE name LIKE ? or phone LIKE ? or email LIKE ?", ('%'+keyword+'%', '%'+keyword+'%', '%'+keyword+'%'))
rows = c.fetchall()
if len(rows) == 0:
print("No contacts found.")
else:
table = PrettyTable(['ID', 'Name', 'Phone', 'Email'])
for row in rows:
table.add_row(row)
print(table)
# 显示所有记录
def show_all_contacts():
c.execute("SELECT * FROM contacts")
rows = c.fetchall()
if len(rows) == 0:
print("No contacts found.")
else:
table = PrettyTable(['ID', 'Name', 'Phone', 'Email'])
for row in rows:
table.add_row(row)
print(table)
# 主程序
while True:
print("1. Add contact")
print("2. Update contact")
print("3. Delete contact")
print("4. Search contacts")
print("5. Show all contacts")
print("0. Exit")
choice = input("Enter your choice: ")
if choice == '1':
name = input("Enter name: ")
phone = input("Enter phone: ")
email = input("Enter email (optional): ")
add_contact(name, phone, email)
print("Contact added successfully.")
elif choice == '2':
id = input("Enter ID of contact to update: ")
name = input("Enter name (press enter to keep the current value): ")
phone = input("Enter phone (press enter to keep the current value): ")
email = input("Enter email (press enter to keep the current value): ")
old_data = c.execute("SELECT * FROM contacts WHERE id=?", (id,)).fetchall()
if len(old_data) == 0:
print("Contact not found.")
else:
if name == '':
name = old_data[0][1]
if phone == '':
phone = old_data[0][2]
if email == '':
email = old_data[0][3]
update_contact(id, name, phone, email)
print("Contact updated successfully.")
elif choice == '3':
id = input("Enter ID of contact to delete: ")
delete_contact(id)
print("Contact deleted successfully.")
elif choice == '4':
keyword = input("Enter keyword to search: ")
search_contacts(keyword)
elif choice == '5':
show_all_contacts()
elif choice == '0':
break
else:
print("Invalid choice.")
# 关闭数据库连接
conn.close()
```
使用方法:
1. 运行程序后,会显示一个菜单,包括添加、修改、删除、查询、显示所有记录和退出等选项。
2. 选择相应的选项,按照提示输入信息。
3. 添加、修改和删除操作完成后,程序会自动更新数据库中的数据。
4. 查询和显示所有记录操作会返回相应的结果。
注意事项:
1. 数据库文件 `address_book.db` 和程序文件在同一目录下。
2. 在使用查询和显示所有记录功能时,如果没有符合条件的记录,则会提示“未找到联系人”。
阅读全文