数据库系统课程设计源代码python
时间: 2024-01-03 10:02:13 浏览: 38
数据库系统课程设计主要涉及到数据库的设计与实现,需要使用一门编程语言来完成相应的源代码。下面是一个基于Python的数据库系统课程设计的示例代码:
```python
import sqlite3
def create_database():
conn = sqlite3.connect('student.db')
c = conn.cursor()
# 创建学生表
c.execute('''CREATE TABLE IF NOT EXISTS student (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER,
major TEXT
)''')
# 创建课程表
c.execute('''CREATE TABLE IF NOT EXISTS course (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
credit INTEGER
)''')
# 创建选课表
c.execute('''CREATE TABLE IF NOT EXISTS enrollment (
student_id INTEGER,
course_id INTEGER,
grade INTEGER,
FOREIGN KEY (student_id) REFERENCES student (id),
FOREIGN KEY (course_id) REFERENCES course (id)
)''')
conn.commit()
conn.close()
def add_student(name, age, major):
conn = sqlite3.connect('student.db')
c = conn.cursor()
c.execute('INSERT INTO student (name, age, major) VALUES (?, ?, ?)', (name, age, major))
conn.commit()
conn.close()
def add_course(name, credit):
conn = sqlite3.connect('student.db')
c = conn.cursor()
c.execute('INSERT INTO course (name, credit) VALUES (?, ?)', (name, credit))
conn.commit()
conn.close()
def enroll_student(student_id, course_id, grade):
conn = sqlite3.connect('student.db')
c = conn.cursor()
c.execute('INSERT INTO enrollment (student_id, course_id, grade) VALUES (?, ?, ?)', (student_id, course_id, grade))
conn.commit()
conn.close()
def main():
create_database()
add_student('小明', 20, '计算机科学')
add_student('小红', 19, '软件工程')
add_course('数据库原理', 4)
add_course('数据结构', 3)
enroll_student(1, 1, 85)
enroll_student(1, 2, 78)
enroll_student(2, 2, 90)
print('数据库系统课程设计源代码完成!')
if __name__ == "__main__":
main()
```
以上的代码是一个简单的学生选课系统的示例,通过Python语言实现了学生、课程和选课表的创建,以及新增学生、课程和选课信息的功能。数据库使用SQLite,并根据表之间的关系建立了外键约束。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)