【MK】Python全栈 阶段二:习题汇总 十:MySQL的综合应用:实践二:《新闻管理系统》数据库项目开发实战:Python+MySQL

版权声明:本文为博主原创文章,未经博主允许不得转载。



一、项目介绍

1.1 流程介绍

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.2 操作管理

1、 管理员身份
在这里插入图片描述
在这里插入图片描述

1.3 项目结构

在这里插入图片描述

1.4 程序运行

1、登录界面效果图展示:使用管理员身份登录

在这里插入图片描述

2、登录成功后选择:新闻管理、用户管理

在这里插入图片描述

3、…

1.5 cmd 执行

1、复制路径
在这里插入图片描述
2、cmd 执行
在这里插入图片描述
在这里插入图片描述

二、代码实现

新闻管理系统

  用户名:admin 密码:ABC123456

   数据库结构创建: https://blog.csdn.net/InitialHeart2021/article/details/119893583

mysql_db.py

import mysql.connector.pooling

__config = {
   
    'host': 'localhost',
    'port': 3306,
    'user': 'root',
    'password': '123456',
    'database': 'lsj2021_project_news'
}
try:
    pool = mysql.connector.pooling.MySQLConnectionPool(
        **__config,
        pool_size=10
    )
except Exception as e:
    print(e)

news_dao.py

import os, sys
sys.path.append('..')
from db.mysql_db import pool

class NewsDao:
    # 查询待审批的新闻列表  page:分页页数
    def search_unreview_list(self, page):
        try:
            con = pool.get_connection()
            cursor = con.cursor()
            sql = "SELECT n.id, n.title, t.type, u.username FROM t_news n JOIN t_type t ON n.type_id=t.id JOIN t_user u ON n.editor_id=u.id " \
                  "WHERE n.state=%s ORDER BY n.create_time DESC LIMIT %s, %s"
            cursor.execute(sql, ['待审批', (page-1)*10, 10])
            result = cursor.fetchall()
            return result
        except Exception as e:
            print(e)
        finally:
            if 'con' in dir():
                con.close()

    # 查询待审批新闻记录总页数
    def search_unreview_count_page(self):
        try:
            con = pool.get_connection()
            cursor = con.cursor()
            sql = "SELECT CEIL(COUNT(*)/10) FROM t_news WHERE state=%s"
            cursor.execute(sql, ["待审批"])
            count_page = cursor.fetchone()
            return count_page
        except Exception as e:
            print(e)
        finally:
            if 'con' in dir():
                con.close()

    # 新闻审批
    def update_unreview_news(self, id):
        try:
            con = pool.get_connection()
            # 开启事务
            con.start_transaction()
            cursor = con.cursor()
            sql = "UPDATE t_news set state=%s WHERE id=%s"
            cursor.execute(sql, ['已审批', id])
            # 提交事务
            con.commit()
        except Exception as e:
            if 'con' in dir():
                # 事务回滚
                con.rollback()
            print(e)
        finally:
            if 'con' in dir():
                con.close()

    # 查询新闻列表:分页显示记录
    def search_list(self, page):
        try:
            con = pool.get_connection()
            cursor = con.cursor()
            sql = "SELECT n.id, n.title, t.type, u.username FROM t_news n " \
                  "JOIN t_type t ON n.type_id=t.id " \
                  "JOIN t_user u ON n.editor_id=u.id " \
                  "ORDER BY n.create_time DESC LIMIT %s,%s "
            cursor.execute(sql, [(page-1)*10, 10])
            result = cursor.fetchall()
            return result
        except Exception as e:
            print(e)
        finally:
            if 'con' in dir():
                con.close()
                # print('报错!!!')


    # 查询新闻总页数
    def search_count_page(self):
        try:
            con = pool.get_connection()
            cursor = con.cursor()
            sql = "SELECT CEIL(COUNT(*)/10) FROM t_news"
            cursor.execute(sql)
            count_page = cursor.fetchone()[0]
            return count_page
        except Exception as e:
            print(e)
        finally:
            if 'con' in dir():
                con.close()

    # 删除新闻
    def delete_by_id(self, id):
        try:
            con = pool.get_connection()
            # 开启事务
            con.start_transaction()
            cursor = con.cursor()
            sql = "DELETE FROM t_news WHERE id=%s"
            cursor.execute(sql, [id])
            # 提交事务
            con.commit()
        except Exception as e:
            if 'con' in dir():
                con.rollback()
            print(e)
        finally:
            if 'con' in dir():
                con.close()

# service = NewsDao()
# result = service.search_unreview_list(1)
# print(result)

role_dao.py

import sys, os
sys.path.append('..')
from db.mysql_db import pool

class RoleDao:
    # 查询角色列表
    def search_list(self):
        try:
            con = pool.get_connection()
            cursor = con.cursor()
            sql = 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

InitialHeart2021

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值