Python大作业——爬虫+可视化+数据分析+数据库(数据库篇)

2021-12-19 23:16
由于该程序可以进行歌曲得收藏和下载,并导出用户的所收藏和下载的歌曲,所以需要数据库来进行不同用户信息的存储

一、注册登录

首先需要建立一个用户表

为了简单,登录注册只需要账号密码

所以用户表中只设置两个字段,username和password,数据类型为varchar型

以username为主键间接实现用户名不可重复(如果insert语句操作失败,则直接提示用户名已存在)


建立好表后就可以进行数据库操作了

首先先对输入数据进行简单的判断,如是否输入长度过长或者没有输入

确定没有异常后再进行数据库操作

使用pymysql.connect进行数据库的连接并获取数据库交互对象cursor,用try块包裹,如果出错则抛出数据库连接失败的消息提示框并结束该函数

正确连接数据库之后,将输入框获得的username和password作为参数封装SQL语句:sql = 'insert into users values("%s","%s")' % (username, password)

随后执行sql语句,如果成功执行则结束事务conn.commit(),出现异常则进行事务回滚conn.rollback()并提示用户名已存在(因为username设置为主键,重复则会出现异常,至于数据长度方面的异常已经在最开始排除)

最后不要忘记关闭数据库连接
 
def register(self, event):  # 注册
    username = self.nameEd1.text()
    password = self.nameEd2.text()
    if username == "" or password == "":
        msg_box = QMessageBox(QMessageBox.Warning, '温馨提示', '用户名和密码不能为空')
        msg_box.exec_()
        return

    if len(username) > 10 or len(password) > 16:
        msg_box = QMessageBox(QMessageBox.Warning, '温馨提示', '用户名或密码过长')
        msg_box.exec_()
        return
    
    try:
        conn = pymysql.connect(host='localhost', user='root', password='129496', db='pyhomework')
        cur = conn.cursor()
    except:
        msg_box = QMessageBox(QMessageBox.Warning, '错误提示', '数据库连接失败')
        msg_box.exec_()
        return

    sql = 'insert into users values("%s","%s")' % (username, password)
    try:
        cur.execute(sql)
        conn.commit()
        msg_box = QMessageBox(QMessageBox.Warning, '恭喜', '注册成功')
        msg_box.exec_()
        self.nameEd1.setText("")
        self.nameEd2.setText("")
    except:
        conn.rollback()
        msg_box = QMessageBox(QMessageBox.Warning, '温馨提示', '用户名已经存在')
        msg_box.exec_()
        self.nameEd1.setText("")
        self.nameEd2.setText("")
    cur.close()
    conn.close()

登录功能类似,先进行简单的输入判断,无误后连接数据库并通过输入的用户名执行查询语句

使用user = cur.fetchone()语句获取查询的第一个结果集

如果user为null则证明查询不到该用户

否则将输入的密码与user[1]进行比较(密码是表中的第二个字段,所以使用下标1)

相同则证明登录成反之则证明密码错误

def login(self, event):  # 登录
    username = self.nameEd1.text()
    password = self.nameEd2.text()
    if username == "" or password == "":
        msg_box = QMessageBox(QMessageBox.Warning, '温馨提示', '请输入用户名和密码')
        msg_box.exec_()
        return

    try:
        conn = pymysql.connect(host='localhost', user='root', password='129496', db='pyhomework')
        cur = conn.cursor()
    except:
        msg_box = QMessageBox(QMessageBox.Warning, '温馨提示', '数据库连接错误')
        msg_box.exec_()
        return

    try:
        sql = 'select * from users where username="%s"' % (username)
        cur.execute(sql)
        conn.commit()
    except:
        msg_box = QMessageBox(QMessageBox.Warning, '错误提示', '系统错误')
        msg_box.exec_()
        cur.close()
        conn.close()
        return

    user = cur.fetchone()
    cur.close()
    conn.close()
    if user is None:
        msg_box = QMessageBox(QMessageBox.Warning, '错误提示', '该用户不存在')
        msg_box.exec_()
        self.nameEd1.setText("")
        self.nameEd2.setText("")
        return

    if username == user[0] and password == user[1]:
        msg_box = QMessageBox(QMessageBox.Warning, '恭喜', '登陆成功')
        msg_box.exec_()
        self.username = username
        self.accept()

    else:
        msg_box = QMessageBox(QMessageBox.Warning, '错误提示', '用户名或者密码错误')
        msg_box.exec_()
        self.nameEd1.setText("")
        self.nameEd2.setText("")
        return
def my_collects(self):
    try:
        conn = pymysql.connect(host='localhost', user='root', password='129496', db='pyhomework')
        cur = conn.cursor()
    except:
        msg_box = QMessageBox(QMessageBox.Warning, '温馨提示', '数据库连接错误')
        msg_box.exec_()
        return

    try:
        sql = 'select * from collects where user="%s"' % (self.username)
        cur.execute(sql)
        conn.commit()
    except:
        msg_box = QMessageBox(QMessageBox.Warning, '错误提示', '系统错误')
        msg_box.exec_()

    row = 0
    collects = cur.fetchall()
    cur.close()
    conn.close()
    workbook = xlsxwriter.Workbook(self.username+'\'s collects.xlsx')
    worksheet = workbook.add_worksheet('music')
    worksheet.write_row(row, 0, ['歌手', '歌名', '歌曲资源地址'])
    for collect in collects:
        row += 1
        worksheet.write_row(row, 0, [collect[1], collect[2], collect[3]])
    workbook.close()
    os.startfile(self.username+'\'s collects.xlsx')
def collect(self):
    try:
        conn = pymysql.connect(host='localhost', user='root', password='129496', db='pyhomework')
        cur = conn.cursor()
    except:
        msg_box = QMessageBox(QMessageBox.Warning, '温馨提示', '数据库连接错误')
        msg_box.exec_()
        return
    
    sql = 'insert into collects values("%s","%s", "%s", "%s")' % (self.username, self.keyword, self.names[self.play_index_now], self.musics[self.play_index_now])
    try:
        cur.execute(sql)
        conn.commit()
        msg_box 
        = QMessageBox(QMessageBox.Warning, '提示', '收藏成功')
        msg_box.exec_()
    except:
        conn.rollback()
        msg_box = QMessageBox(QMessageBox.Warning, '温馨提示', '收藏出错')
        msg_box.exec_()
    cur.close()
    conn.close()
filename = QFileDialog.getSaveFileName(self, '下载文件', '.', '音乐文件(*.MP3)')  # 保存歌曲

该语句执行后返回保存的文件名,如果不为空则证明成功保存而不是点击取消

随后即可将内容写入该文件,完成下载到本地的操作

if filename[0] != '':
    with open(filename[0], 'wb') as m:
        m.write(response.content)

总的数据表展示



分享到:
文章评论 · 所有评论
评论请遵守当地法律法规
点击加载更多
本月点击排行
精彩图片
京ICP备14056871号
Copyright ©2022 USN, All Rights Reserved