使用python与sql sever连接实现学生信息管理系统
时间: 2024-04-23 10:24:36 浏览: 112
基于python实现学生信息管理系统
5星 · 资源好评率100%
好的,以下是一个简单的Python与SQL Server连接实现学生信息管理系统的示例:
1. 首先,需要安装pyodbc模块,用于连接SQL Server数据库。
```
pip install pyodbc
```
2. 导入pyodbc模块,并且连接SQL Server数据库。
```
import pyodbc
# 定义连接参数
server = 'localhost'
database = 'StudentDB'
username = 'sa'
password = 'password'
# 连接数据库
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
```
3. 创建一个学生信息表。
```
# 创建学生信息表
cursor = cnxn.cursor()
cursor.execute('''
CREATE TABLE Students
(ID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Gender VARCHAR(10),
Address VARCHAR(100));
''')
```
4. 定义一些函数用于实现学生信息管理系统的功能。
```
# 添加学生信息
def add_student(name, age, gender, address):
cursor.execute('INSERT INTO Students (Name, Age, Gender, Address) VALUES (?, ?, ?, ?)', (name, age, gender, address))
cnxn.commit()
# 查找学生信息
def search_student(name):
cursor.execute('SELECT * FROM Students WHERE Name = ?', name)
row = cursor.fetchone()
if row:
print(row)
else:
print('No student found with that name.')
# 更新学生信息
def update_student(name, age, gender, address):
cursor.execute('UPDATE Students SET Age = ?, Gender = ?, Address = ? WHERE Name = ?', (age, gender, address, name))
cnxn.commit()
# 删除学生信息
def delete_student(name):
cursor.execute('DELETE FROM Students WHERE Name = ?', name)
cnxn.commit()
```
5. 使用函数实现学生信息管理系统的功能。
```
# 添加学生信息
add_student('Tom', 20, 'Male', '123 Main St.')
add_student('Jane', 19, 'Female', '456 Elm St.')
add_student('John', 21, 'Male', '789 Oak St.')
# 查找学生信息
search_student('Tom')
# 更新学生信息
update_student('Jane', 22, 'Female', '456 Elm St.')
search_student('Jane')
# 删除学生信息
delete_student('John')
search_student('John')
```
完整代码如下:
```
import pyodbc
# 连接SQL Server数据库
server = 'localhost'
database = 'StudentDB'
username = 'sa'
password = 'password'
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
# 创建学生信息表
cursor = cnxn.cursor()
cursor.execute('''
CREATE TABLE Students
(ID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Gender VARCHAR(10),
Address VARCHAR(100));
''')
# 添加学生信息
def add_student(name, age, gender, address):
cursor.execute('INSERT INTO Students (Name, Age, Gender, Address) VALUES (?, ?, ?, ?)', (name, age, gender, address))
cnxn.commit()
# 查找学生信息
def search_student(name):
cursor.execute('SELECT * FROM Students WHERE Name = ?', name)
row = cursor.fetchone()
if row:
print(row)
else:
print('No student found with that name.')
# 更新学生信息
def update_student(name, age, gender, address):
cursor.execute('UPDATE Students SET Age = ?, Gender = ?, Address = ? WHERE Name = ?', (age, gender, address, name))
cnxn.commit()
# 删除学生信息
def delete_student(name):
cursor.execute('DELETE FROM Students WHERE Name = ?', name)
cnxn.commit()
# 添加学生信息
add_student('Tom', 20, 'Male', '123 Main St.')
add_student('Jane', 19, 'Female', '456 Elm St.')
add_student('John', 21, 'Male', '789 Oak St.')
# 查找学生信息
search_student('Tom')
# 更新学生信息
update_student('Jane', 22, 'Female', '456 Elm St.')
search_student('Jane')
# 删除学生信息
delete_student('John')
search_student('John')
```
希望能够帮助到你!如果有任何问题,请随时提出。
阅读全文