目录

 

序言

sqlite使用教程

python sqlite使用教程

数据库构造

    学生信息表结构:

    教师信息表结构:

    课程成绩表结构:

   教师添加课程:

    学生表连接课程成绩表:

数据库语句了解

sqlite数据库语句大全

具体数据库连接方式

学生信息表:student_info_sql.py

教师信息表:teacher_info_sql.py

课程成绩表:student_achievement_sql.py

后记

史上最全面的python学生管理系统教程(一)

史上最全面的python学生管理系统教程(三)


序言

    这篇我们来讲该软件的数据保存方式,由于想到要简便快捷,所以我采用了sqlite3数据库,该数据库操作简单方便,而且易于管理,并且SQLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。它是一个零配置的数据库,这意味着与其他数据库一样,您不需要在系统中配置。就像其他数据库,SQLite 引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SQLite 直接访问其存储文件。我给大家推荐一款小巧的sqlite数据库文件打开的应用:SQLite Spy。百度一下就可以下载。

sqlite使用教程

 

数据库构造

    该软件需求为三位角色:学生,教师,管理员。

    为了从简,我把管理员只设置为一位,帐号密码皆为“ 1 ",所以就不再给他设计管理员表了

if Admin_number=="1" and Admin_pw=="1":
    self.page.destroy()
    self.lab3.pack_forget()
    AdminPage(self.root)

    只设计了三张表,分别为:学生信息表,教师信息表,课程成绩表。

    还是那句话,为了从简,我把学生信息表跟教师信息表两张表结构设计的大致一样,这样就可以代码复用了,但是为了好分辨,我还是分别写了两个py文件。

    学生信息表结构:

create table if not exists student_info
(
        id integer PRIMARY KEY autoincrement,
        student_number varchar(12),
        student_name varchar(10),
        student_passworld varchar(128),
        age varchar(2)
)

    教师信息表结构:

create table if not exists teacher_info
(
        id integer PRIMARY KEY autoincrement,
        teacher_number varchar(12),
        teacher_name varchar(10),
        teacher_passworld varchar(128),
        age varchar(2)
)

    课程成绩表结构:

create table if not exists student_achievement
(
        student_number varchar(12),
        student_name varchar(10)
)

因为教师可以添加课程科目,所以初始表比较简单,并且课程表是跟随学生表联动的,当注册一个学生信息时自动创建该学生的课程成绩表。具体构思如下:

   教师添加课程:

#  往成绩数据库中添加新的一列科目成绩
def achievement_insertData(subject_name):
        hel = achievement_opendb()
        hel[1].execute("alter table student_achievement add column "+subject_name+" int")
        hel[1].commit()
        hel[1].close()

    学生表连接课程成绩表:

#  往学生数据库中添加内容
def user_insertData(number,name,pw,age):
        achievement_infoData(number,name)#同时插入到课程成绩表
        hel = user_opendb()
        hel[1].execute("insert into student_info(student_number,student_name, student_passworld,age)values (?,?,?,?)",(number,name,pw,age))
        hel[1].commit()
        hel[1].close()
数据库语句了解sqlite数据库语句大全

具体数据库连接方式

学生信息表:student_info_sql.py

# -*- coding:utf-8 -*-
import sqlite3
from student_achievement_sql import *
# 打开学生数据库
def user_opendb():
        conn = sqlite3.connect("student.db")
        cur = conn.execute("""create table if not exists student_info(
        id integer PRIMARY KEY autoincrement,
        student_number varchar(12),
        student_name varchar(10),
        student_passworld varchar(128),
        age varchar(2))""")
        return cur, conn
    
#查询所有列名
def user_lie_name():
    hel = user_opendb()
    cur = hel[1].cursor()
    cur.execute("select * from student_info")
    col_name_list = [tuple[0] for tuple in cur.description]  
    return col_name_list
    cur.close()

#查询学生全部信息
def user_slectTable():
        hel = user_opendb()
        cur = hel[1].cursor()
        cur.execute("select * from student_info")
        res = cur.fetchall()
        #for line in res:
                #for h in line:
                        #print(h),
                #print(line)
        return res
        cur.close()
#  往学生数据库中添加内容
def user_insertData(number,name,pw,age):
        achievement_infoData(number,name)
        hel = user_opendb()
        hel[1].execute("insert into student_info(student_number,student_name, student_passworld,age)values (?,?,?,?)",(number,name,pw,age))
        hel[1].commit()
        hel[1].close()
#查询学生个人信息
def user_showdb(number):
        hel = user_opendb()
        cur = hel[1].cursor()
        cur.execute("select * from student_info where student_number="+number)
        res = cur.fetchone()
        cur.close()
        return res
#   删除学生数据库中的全部内容
def user_delalldb():
        achievement_delalldb()
        hel = user_opendb()              # 返回游标conn
        hel[1].execute("delete from student_info")
        print("删库跑路XWC我最帅")
        hel[1].commit()
        hel[1].close()
#   删除学生数据库中的指定内容
def user_deldb(number):
        achievement_deldb(number)
        hel = user_opendb()              # 返回游标conn
        hel[1].execute("delete from student_info where student_number="+number)
        print("已删除学号为 %s 学生" %number)
        hel[1].commit()
        hel[1].close()
        
#  修改学生数据库的内容
def user_alter(number,name,pw,age):
        hel = user_opendb()
        hel[1].execute("update student_info set student_name=?, student_passworld= ?,age=? where student_number="+number,(name,pw,age))
        hel[1].commit()
        hel[1].close()
        
#  修改学生数据库密码的内容
def user_alter_pw(number,pw):
    hel = user_opendb()
    hel[1].execute("update student_info set student_passworld= %s where student_number=%s"%(pw,number))
    hel[1].commit()
    hel[1].close()
        
# 登录查询学生数据
def user_slect_number_pw(number,pw):
        hel = user_opendb()
        cur = hel[1].cursor()
        cur.execute("select * from student_info where student_number="+number+" and student_passworld= "+pw)
        hel[1].commit()
        for row in cur:
            if row:
                return True
            else:
                return False
        cur.close()
        hel[1].close()

教师信息表:teacher_info_sql.py

# -*- coding:utf-8 -*-
import sqlite3
# 打开教师数据库
def teacher_opendb():
        conn = sqlite3.connect("student.db")
        cur = conn.execute("""create table if not exists teacher_info(
        id integer PRIMARY KEY autoincrement,
        teacher_number varchar(12),
        teacher_name varchar(10),
        teacher_passworld varchar(128),
        age varchar(2))""")
        return cur, conn
    
#查询所有列名
def teacher_lie_name():
    hel = teacher_opendb()
    cur = hel[1].cursor()
    cur.execute("select * from teacher_info")
    col_name_list = [tuple[0] for tuple in cur.description]  
    return col_name_list
    cur.close()

#查询教师全部信息
def teacher_slectTable():
        hel = teacher_opendb()
        cur = hel[1].cursor()
        cur.execute("select * from teacher_info")
        res = cur.fetchall()
        #for line in res:
                #for h in line:
                        #print(h),
                #print(line)
        return res
        cur.close()
        
#  往教师数据库中添加内容
def teacher_insertData(number,name,pw,age):
        hel = teacher_opendb()
        hel[1].execute("insert into teacher_info(teacher_number,teacher_name, teacher_passworld,age)values (?,?,?,?)",(number,name,pw,age))
        hel[1].commit()
        hel[1].close()
        
#查询教师个人信息
def teacher_showdb(number):
        hel = teacher_opendb()
        cur = hel[1].cursor()
        cur.execute("select * from teacher_info where teacher_number="+number)
        res = cur.fetchone()
        cur.close()
        return res
    
#   删除教师数据库中的全部内容
def teacher_delalldb():
        hel = teacher_opendb()              # 返回游标conn
        hel[1].execute("delete from teacher_info")
        print("删库跑路XWC我最帅")
        hel[1].commit()
        hel[1].close()
        
#   删除教师数据库中的指定内容
def teacher_deldb(number):
        hel = teacher_opendb()              # 返回游标conn
        hel[1].execute("delete from teacher_info where teacher_number="+number)
        print("已删除教师号为 %s 教师" %number)
        hel[1].commit()
        hel[1].close()
        
#  修改教师数据库的内容
def teacher_alter(number,name,pw,age):
        hel = teacher_opendb()
        hel[1].execute("update teacher_info set teacher_name=?, teacher_passworld= ?,age=? where teacher_number="+number,(name,pw,age))
        hel[1].commit()
        hel[1].close()
        
#  修改教师数据库密码的内容
def teacher_alter_pw(number,pw):
    hel = teacher_opendb()
    hel[1].execute("update teacher_info set teacher_passworld= %s where teacher_number=%s"%(pw,number))
    hel[1].commit()
    hel[1].close()
        
# 登录查询教师数据
def teacher_slect_number_pw(number,pw):
        hel = teacher_opendb()
        cur = hel[1].cursor()
        cur.execute("select * from teacher_info where teacher_number="+number+" and teacher_passworld= "+pw)
        hel[1].commit()
        for row in cur:
            if row:
                return True
            else:
                return False
        cur.close()
        hel[1].close()

课程成绩表:student_achievement_sql.py

# -*- coding:utf-8 -*-
import sqlite3
from student_info_sql import *
# 打开成绩数据库
def achievement_opendb():
        conn = sqlite3.connect("student.db")
        cur = conn.execute("""create table if not exists student_achievement(
        student_number varchar(12),
        student_name varchar(10))""")
        return cur, conn
    
#查询所有列名
def achievement_lie_name():
    hel = achievement_opendb()
    cur = hel[1].cursor()
    cur.execute("select * from student_achievement")
    col_name_list = [tuple[0] for tuple in cur.description]  
    return col_name_list
    cur.close()
    
#查询学生成绩全部信息
def achievement_slectTable():
        hel = achievement_opendb()
        cur = hel[1].cursor()
        cur.execute("select * from student_achievement")
        res = cur.fetchall()
        #for line in res:
                #for h in line:
                        #print(h),
                #print(line)
        return res
        cur.close()
        
#  往成绩数据库中添加新的一列科目成绩
def achievement_insertData(subject_name):
        hel = achievement_opendb()
        hel[1].execute("alter table student_achievement add column "+subject_name+" int")
        hel[1].commit()
        hel[1].close()
        
#  往成绩数据库中添加学生跟学生信息表同步更新
def achievement_infoData(number,name):
        hel = achievement_opendb()
        hel[1].execute("insert into student_achievement(student_number,student_name)values (?,?)",(number,name))
        hel[1].commit()
        hel[1].close()
        
#  按某科排序输出 默认升序  添加desc为降序
def achievement_paixu(subject_name,desc):
        if desc==None:
            desc=""
        else:
            desc=desc
        hel = achievement_opendb()
        cur = hel[1].cursor()
        cur.execute("select * from student_achievement order by "+subject_name+" "+desc)
        res = cur.fetchall()
        cur.close()
        return res
        
#查询个人成绩信息
def achievement_showdb(number):
        hel = achievement_opendb()
        cur = hel[1].cursor()
        cur.execute("select * from student_achievement where student_number="+number)
        res = cur.fetchone()
        cur.close()
        return res
    
#   删除成绩数据库中的全部内容
def achievement_delalldb():
        hel = achievement_opendb()              # 返回游标conn
        hel[1].execute("delete from student_achievement")
        print("删库跑路XWC我最帅")
        hel[1].commit()
        hel[1].close()
        
#   删除成绩数据库中的指定学生内容跟学生信息表同步更新
def achievement_deldb(number):
        hel = achievement_opendb()              # 返回游标conn
        hel[1].execute("delete from student_achievement where student_number="+number)
        print("已删除学号为%s 学生的成绩单" %number)
        hel[1].commit()
        hel[1].close()
        
#  修改成绩数据库的个人某科成绩
def achievement_alter(number,subject_name,ach):
        hel = achievement_opendb()
        hel[1].execute("update student_achievement set %s = %s where student_number=%s"%(subject_name,ach,number))
        hel[1].commit()
        hel[1].close()

后记

    思前想后其实感觉这样搞是有问题的,代码重复率太高,可用性不强,更别提复用性了,奈何本人水平有限,想进行重构但是看到一堆就头疼不已,所以经过一番思想斗争还是放弃了,有能力的同学可以将数据库进行优化一下,软件具体用到的语句都有说明,并且参数比较清楚,可以将多余的语句进行删除,还有不懂的地方可以留言,我会一一解答。