结合sqlite3的学生信息成绩管理系统(三):基础文件basics.py

一、系列文章链接

(一):程序介绍和流程图
(二):主要文件main.py
(三):基础文件basics.py
(四):管理学生信息manage.py
(五):查询学生信息query.py
(六):导入数据文件import_data.py
(七):导出数据文件export_data.py

二、basics.py的函数简介:

实现效果: 无,因为这个文件里的函数都是被其他文件调用的。
提示: 这个文件里大多都是一些被多次使用的函数,不过有几个函数和其他文件的函数的作用有重复,也有一些函数我以为会被用很多次结果只被用了一次的。 ORZ

  1. def create_file( filename):
    “”“创建日志文件夹和日志文件”""

  2. def judge_file():
    “”“判断是否是xls文件,是才能输入”""

  3. def common( cn, sql):
    “”“将数据从sql格式转换成列表镶嵌字典的格式并返回”""
    “”" 非常重要,被多次调用 “”"

  4. def one_students( cn, table_name, want, sel, sinfo):
    “”“按输入的参数返回 ‘want’ 的信息的函数”""

  5. def judge_comment( cn, table_name, want, sel, sinfo):
    “”“判断’want’的信息是否存在返回count,不存在count等于0"”"

  6. def judge_creat_cname( cn, cname):
    “”“检测课程,不存在就创建”""

(1)Students表的

  1. def insert_students( cn, sno, sname):
    “”“对学生表进行插入”""

  2. def del_students( cn, sno):
    “”“按学号进行删除信息”""

(2)Courses表的

  1. def insert_courses( cn, cno, cname):
    “”“对课程表进行插入”""

  2. def del_courses( cn, cno):
    “”“按课程号进行删除信息”""

(3)Reports表的

  1. def insert_reports( cn, sno, cno, ryear, rterm):
    “”“对选课表进行插入”""

  2. def update_reports( cn, sno, cno, sel, sinfo):
    “”“按学号,课程号对选课表进行更新”""

  3. def del_reports( cn, sno, cno):
    “”“按学号进行删除信息”""

(4)转换函数,使用前记得检测输入的参数是否存在

  1. def cno_to_cname( cn, cno):
    “”“将cno转换成cname”""

  2. def cname_to_cno( cn, cname):
    “”“将cname转换成cno”""

三、代码

"""一些可以共同使用的函数"""
import os

def create_file( filename):
	"""
	创建日志文件夹和日志文件
	:param filename:
	:return:
	"""
	path = filename[0:filename.rfind("/")]
	if not os.path.isdir(path):  # 无文件夹时创建
		os.makedirs(path)
	if not os.path.isfile(filename):  # 无文件时创建
		fd = open(filename, mode="w", encoding="utf-8")
		fd.close()
	else:
		pass

def judge_file():
    """判断是否是xls文件,是才能输入"""
    while 1:
        file = input("请输入文件名(.xls):")
        if file.endswith('.xls'):
            return file
        else:
            print("文件格式错误,请重新输入")
            continue

#返回搜索信息的函数
def common( cn, sql):
    """将数据从sql格式转换成列表镶嵌字典的格式并返回"""
    cursor = cn.execute(sql)
    information = []
    for row in cursor:
        information.append( row)
    return information

def one_students( cn, table_name, want, sel, sinfo):
    """按输入的参数返回'want'的信息的函数"""
    sql = '''SELECT DISTINCT %s
            FROM %s WHERE %s = '%s'
         ''' % ( want, table_name, sel, sinfo)
    return common( cn, sql)

def judge_comment( cn, table_name, want, sel, sinfo):
    """判断'want'的信息是否存在返回count,不存在count等于0"""
    sql = '''SELECT DISTINCT %s
            FROM %s WHERE %s = '%s'
         ''' % ( want, table_name, sel, sinfo)
    cursor = cn.execute( sql)
    count = 0
    for row in cursor:
        count = count + 1
    return count

def judge_creat_cname( cn, cname):
    """检测课程,不存在就创建"""
    count = judge_comment\
        ( cn, 'Courses', 'Cname', 'Cname', cname)
    if count == 0:
        print("这个课程在课程表中不存在")
        confirm = input("%s不存在,是否"
                        "创建?(Y/N)" % cname)
        if confirm == 'y' or confirm == 'Y':
            while 1:
                cno = input("请输入%s的课程号"
                            "(eg:112p0001):" % cname)
                count = judge_comment\
                    ( cn, 'Courses', 'Cname', 'Cno', cno)
                if count != 0:
                    print("该课程号已经存在,请重新输入\n")
                else:
                    chours = input("请输入%s的学时" % cname)
                    credit = input("请输入%s的学分" % cname)
                    insert_courses( cn, cno, cname, chours, credit)
                    break
            cno = cname_to_cno( cn, cname)
            return cno
        else:
            print("请重新输入")
            return 0

#插入,删除,更新函数
# Students表的
def insert_students( cn, sno, sname):
    """对学生表进行插入"""
    sclass = sno[0:8]
    sql = '''insert into Students (Sno, Sname, Sclass)
            values('%s','%s','%s')
         ''' % ( sno, sname, sclass)
    cn.execute( sql)
    cn.commit()

def del_students( cn, sno):
    """按学号进行删除信息"""
    sql = '''DELETE FROM Reports
            WHERE Sno = "%s"
         ''' % sno
    cn.execute( sql)
    cn.commit()

    sql = '''DELETE FROM Students
            WHERE Sno = "%s" 
         ''' % sno
    cn.execute( sql)
    cn.commit()

# Courses表的
def insert_courses( cn, cno, cname, chours, credit):
    """对课程表进行插入"""
    sql = '''insert into Courses 
            (Cno, Cname, Chours, Ccredit)
            values('%s','%s','%s','%s')
         ''' % ( cno, cname, chours, credit)
    cn.execute( sql)
    cn.commit()

def insert_courses2( cn, cno, cname):
    """对课程表进行插入"""
    sql = '''insert into Courses 
            (Cno, Cname)
            values('%s','%s')
         ''' % ( cno, cname)
    cn.execute( sql)
    cn.commit()

def del_courses( cn, cno):
    """按课程号进行删除信息"""
    sql = '''DELETE FROM Reports
            WHERE Cno = "%s"
         ''' % cno
    cn.execute( sql)
    cn.commit()

    sql = '''DELETE FROM Courses
            WHERE Cno = "%s" 
         ''' % cno
    cn.execute( sql)
    cn.commit()

# Reports表的
def insert_reports( cn, sno, cno, ryear, rterm):
    """对选课表进行插入"""
    sno = str(int(sno))
    sql = '''insert into Reports 
            (Sno, Cno, Racademicyear, Rterm)
            values('%s','%s','%s','%s')
         ''' % ( sno, cno, ryear, rterm)
    cn.execute( sql)
    cn.commit()

def update_reports( cn, sno, cno, sel, sinfo):
    """按学号,课程号对选课表进行更新"""
    sql = '''UPDATE Reports 
            SET %s = '%s'
            WHERE Sno = '%s' and Cno = '%s'
         ''' % ( sel, sinfo, sno, cno)
    cn.execute( sql)
    cn.commit()

def del_reports( cn, sno, cno):
    """按学号进行删除信息"""
    sql = '''DELETE FROM Reports
            WHERE Sno = '%s' and Cno = '%s'
         ''' % (sno, cno)
    cn.execute( sql)
    cn.commit()

#转换函数,使用前记得检测输入的参数是否存在
def cno_to_cname( cn, cno):
    """将cno转换成cname"""
    sql = '''SELECT DISTINCT Cname
            FROM Courses
            WHERE Cno = "%s"
         ''' % cno
    cursor = cn.execute( sql)
    ccname = []
    for row in cursor:
        ccname.append( row[0])
    cname = ccname[0]
    return cname

def cname_to_cno( cn, cname):
    """将cname转换成cno"""
    sql = '''SELECT DISTINCT Cno
            FROM Courses
            WHERE Cname = "%s"
         ''' % cname
    cursor = cn.execute( sql)
    ccno = []
    for row in cursor:
        ccno.append( row[0])
    cno = ccno[0]
    return cno


如果文章对你有帮助,点赞是对我最好的鼓励了!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值