帮我写一个基于python+MySQL的学生管理系统
时间: 2024-02-06 08:02:44 浏览: 26
好的,根据您的要求我可以为您编写一个基于Python和MySQL的学生管理系统。首先,需要安装Python以及MySQL数据库。
以下是简单的代码示例:
```python
import mysql.connector
# MySQL数据库配置
db = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="student"
)
# 创建游标对象
cursor = db.cursor()
# 创建student表
cursor.execute("CREATE TABLE IF NOT EXISTS student (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)")
# 插入记录
def insert_student(name, age):
sql = "INSERT INTO student (name, age) VALUES (%s, %s)"
val = (name, age)
cursor.execute(sql, val)
db.commit()
print(cursor.rowcount, "记录插入成功")
# 查找记录
def select_student(name):
sql = "SELECT * FROM student WHERE name = %s"
val = (name,)
cursor.execute(sql, val)
result = cursor.fetchall()
for row in result:
print(row)
# 更新记录
def update_student(name, age):
sql = "UPDATE student SET age = %s WHERE name = %s"
val = (age, name)
cursor.execute(sql, val)
db.commit()
print(cursor.rowcount, "记录更新成功")
# 删除记录
def delete_student(name):
sql = "DELETE FROM student WHERE name = %s"
val = (name,)
cursor.execute(sql, val)
db.commit()
print(cursor.rowcount, "记录删除成功")
```
以上代码实现了创建student表、插入记录、查找记录、更新记录以及删除记录的功能。您可以根据需要进行修改和完善。
请提供您的MySQL数据库配置信息以及更详细的功能要求,以便我为您提供更加贴合您要求的代码。