3.21 学数据库 python操作数据库 权限系统设计

  1. pip3 下载pymysql Python 操作Mysql
#pypi里面
pip3 install pymysql

2.使用pymysql

def yanga11ang():
#------------------1.执行SQL---------------------------
import pymysql
# 创建连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='654321', db='t1',charset='utf8')
# 创建游标
cursor = conn.cursor()
# 执行SQL,并返回收影响行数
effect_row = cursor.execute("update hosts set host = '1.1.1.2'")
# 执行SQL,并返回受影响行数     #!用参数,不用字符串拼接,防止sql注入,(数据库安全,)
effect_row = cursor.execute("update hosts set host = '1.1.1.2' where nid > %s", (1,))
# 执行SQL,并返回受影响行数 插入多个
effect_row = cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])
# 提交,不然无法保存新建或者修改的数据
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
#----------2.获取新创建数据自增ID-----------------
# -*- coding:utf-8 -*-
import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
cursor = conn.cursor()
cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])
conn.commit()
cursor.close()
conn.close()

# 获取最新自增ID
new_id = cursor.lastrowid
def yanga11ang():
#-------------3.获取查询数据-------------
# -*- coding:utf-8 -*-
import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
cursor = conn.cursor()
cursor.execute("select * from hosts")

# 获取第一行数据 相当于文件里面的readline
row_1 = cursor.fetchone()
# 获取前n行数据
row_2 = cursor.fetchmany(3)
# 获取所有数据 返回一个放元组(记录)的元祖
row_3 = cursor.fetchall()
#注:在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如:
cursor.scroll(1,mode='relative')  # 相对当前位置移动
cursor.scroll(2,mode='absolute') # 相对绝对位置移动

conn.commit()
cursor.close()
conn.close()
def yanga11ang():
#-------------4.fetch数据类型-----------
# -*- coding:utf-8 -*-
import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
# 游标设置为字典类型,每一行是一个字典类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
r = cursor.execute("call p1()")
result = cursor.fetchone()
conn.commit()
cursor.close()
conn.close()

3.sql注入

def yanga111ang():
import pymysql
conn=pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='654321', db='t1')
cursor=conn.cursor()
#传递参数
cursor.execute('select username,password from userinfo where username=%s and password=%s',('yanga11ang',654321))
#字符串拼接 密码不对也可以额拿到数据
sql='select username,password from userinfo where username=%s and password=%s'
sql=sql %('yanga11ang" or 1=1 -- ',123456)
sql=sql %('yanga11ang" -- ',123456) #--在sql是注释的意思,会把密码注释了,这样就可以查到信息了

4.权限系统设计

1.登录
2.获取菜单
3.打印菜单
4.选择菜单的功能
-------初步设计-------
建立用户表
建立权限表
建立 用户权限关系表
弊端: 如果是有许多员工他们的权限相似,管理不变
查看用户权限的方式sql语句:

-------初步改进-----------
建立用户表+角色
建立权限表
建立角色表
建立 角色权限关系表
查看用户权限的方式sql语句:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值