python通讯录程序设计-python数据库编程 Mysql实现通讯录的修改联系人、删除联系人
时间: 2024-03-07 16:47:34 浏览: 87
好的,我可以帮你解决这个问题。在这个程序中,我们将使用Python和MySQL数据库来实现通讯录的修改联系人和删除联系人的功能。
首先,我们需要创建一个MySQL数据库来存储我们的通讯录信息。我们可以使用以下命令在MySQL中创建一个名为"contacts"的数据库:
```
CREATE DATABASE contacts;
```
接下来,我们需要创建一个名为"people"的表来存储我们的联系人信息。该表应该包含以下列:id, name, phone和email。我们可以使用以下命令创建该表:
```
CREATE TABLE people (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
phone VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
```
现在我们已经创建了数据库和表,接下来我们将编写Python代码来实现修改联系人和删除联系人的功能。
首先,我们需要连接到MySQL数据库。我们可以使用以下代码来实现:
```python
import mysql.connector
# Connect to the database
db = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="contacts"
)
# Create a cursor
cursor = db.cursor()
```
接下来,我们将编写函数来实现修改联系人和删除联系人的功能。
修改联系人的函数应该接受一个联系人的ID作为参数,并提示用户输入要修改的信息。我们可以使用以下代码实现:
```python
def update_contact(id):
# Get the contact
cursor.execute("SELECT * FROM people WHERE id = %s", (id,))
contact = cursor.fetchone()
# Print the current contact information
print("Current information:")
print(f"Name: {contact[1]}")
print(f"Phone: {contact[2]}")
print(f"Email: {contact[3]}")
# Prompt the user for new information
name = input("Enter new name (press enter to skip): ")
phone = input("Enter new phone number (press enter to skip): ")
email = input("Enter new email address (press enter to skip): ")
# Update the contact
if name:
cursor.execute("UPDATE people SET name = %s WHERE id = %s", (name, id))
if phone:
cursor.execute("UPDATE people SET phone = %s WHERE id = %s", (phone, id))
if email:
cursor.execute("UPDATE people SET email = %s WHERE id = %s", (email, id))
# Commit the changes
db.commit()
print("Contact updated.")
```
删除联系人的函数应该接受一个联系人的ID作为参数,并提示用户确认是否要删除联系人。我们可以使用以下代码实现:
```python
def delete_contact(id):
# Get the contact
cursor.execute("SELECT * FROM people WHERE id = %s", (id,))
contact = cursor.fetchone()
# Print the current contact information
print("Current information:")
print(f"Name: {contact[1]}")
print(f"Phone: {contact[2]}")
print(f"Email: {contact[3]}")
# Confirm deletion
confirm = input("Are you sure you want to delete this contact? (y/n) ")
if confirm.lower() == "y":
# Delete the contact
cursor.execute("DELETE FROM people WHERE id = %s", (id,))
# Commit the changes
db.commit()
print("Contact deleted.")
else:
print("Deletion cancelled.")
```
现在我们已经编写了修改联系人和删除联系人的函数,接下来我们将编写一个主函数来处理用户输入,并调用这些函数。
```python
def main():
while True:
print("1. Add contact")
print("2. Update contact")
print("3. Delete contact")
print("4. Quit")
choice = input("Enter your choice: ")
if choice == "1":
# Add contact
name = input("Enter name: ")
phone = input("Enter phone number: ")
email = input("Enter email address: ")
cursor.execute("INSERT INTO people (name, phone, email) VALUES (%s, %s, %s)", (name, phone, email))
db.commit()
print("Contact added.")
elif choice == "2":
# Update contact
id = input("Enter contact ID: ")
update_contact(id)
elif choice == "3":
# Delete contact
id = input("Enter contact ID: ")
delete_contact(id)
elif choice == "4":
# Quit
break
else:
print("Invalid choice.")
# Close the database connection
db.close()
if __name__ == "__main__":
main()
```
现在我们已经完成了我们的Python通讯录程序。用户可以选择添加、修改或删除联系人。所有联系人信息都存储在MySQL数据库中。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)