一.Pymysql

import pymysql
#python2.X 中是 mysqldb 和 pythonmysql 用法是一模一样的
#pymysql可以伪装成上面这两个模块 user = input('username: ')
pwd = input('password: ') #连接数据库
conn = pymysql.connect(host='localhost',user='root',password='gkx321',database='db222')
cursor = conn.cursor() #类似文件句柄。 比如conn是打开了一个柜子,cursor就是我们拿东西的手
# cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) #设置为字典格式,fetch的时候可以显示字段名,以字典形式 #注意以后绝对不要这么拼接
# sql = "select * from userinfo where username='%s' and password = '%s' "%(user,pwd)
# username: uu' or 1=1 --
# password: gkx
# (1, 'gkx', 'gkx123')
# 登陆成功 #正确方式
sql = "select * from userinfo where username = %s and password = %s " #三种方式 元组,列表,字典
# cursor.execute(sql)
cursor.execute(sql,(user,pwd)) #执行语句 ,末尾加不加逗号都行
#r = cursor.execute(sql,(user,pwd)) #有个返回值,表示受影响的行数 # cursor.execute(sql,[user,pwd]) # sql = "select * from userinfo where username = %(u)s and password = %(p)s "
# cursor.execute(sql,{'u':user,'p':pwd}) ret = cursor.fetchone() #fetchone类似 readline 运行一次,只读取一行。
print(ret) cursor.close()
conn.close()
if ret:
print('登陆成功')
else:
print('登陆失败')

pymysql初识

#总结
#查询需要 fetchone
#更新和insert 需要 conn.commit()
#create drop alter 只要 cursor.execute()
import pymysql conn = pymysql.connect(host='localhost',user='root',password='gkx321',database='db222')
# cursor = conn.cursor() #类似文件句柄。 比如conn是打开了一个柜子,cursor就是我们拿东西的手
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) #设置为字典类型的 cursor
'''
#增加 删除 改
# user = 'ccc'
# pwd = 'ccc123'
sql = "insert into userinfo(username,password) values(%s,%s)"
# cursor.execute(sql,(user,pwd,))
cursor.executemany(sql,[('ww','ww123'),('aaa','aaa123')]) #增加多行,只适合insert时候用
# r = cursor.execute(sql,(user,pwd,)) #返回值,受影响的行数
conn.commit() #增删改 三种操作一定要 commit 不提交数据库不知道要修改
for 循环中 可以 全部execute后,再进行一次 commit
''' #查
sql = "select * from userinfo"
cursor.execute(sql) # 既然fetchone像文件中的readline,那么就有类似 tell和seek的语句:
cursor.scroll(2,mode='relative') #根据当前位置,相对移动 .2表示从当前位置,选择下下行,自然1就是选择下一行了
cursor.scroll(2,mode='absolute') #根据绝对位置移动 2表示从第1行往后的2行,即表示选择第3行 #可以通过设置cursor,来让fetchone显示的时候,带上字段名,以字典形式
ret = cursor.fetchone() #只有查询的时候才需要
print(ret)
ret = cursor.fetchone()
print(ret)
ret = cursor.fetchall()
print(ret)
#
# ret1 = cursor.fetchall() #返回一个元组,每条数据又是一个元组
# print(ret1) #要注意的是,尽量还是要用sql语句操作,因为比如fetchall/fetchmany 会把所有数据放到内存中来,比如10万条数据,那么是相当占内存的
#还是建议用 sql中的 limit 分页
# ret1 = cursor.fetchall()
# ret2 = cursor.fetchmany(3) cursor.close()
conn.close()

pymysql—增删改查

#新插入数据的自增id  cursor.lastrowid
import pymysql conn = pymysql.connect(host='localhost',user='root',password='gkx321',database='db222')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) sql = "insert into userinfo(username,password) values('ddd','ddd123')"
cursor.execute(sql)
print(cursor.lastrowid) #
#获取插入数据的自增id
#那如果是插入多行呢,也只获取最后一行的 自增id,所以有需要外键的话,只能一个个插入再获取
conn.commit() cursor.close()
conn.close()

新插入数据的自增id

import pymysql
import sys
conn = pymysql.connect(host='localhost',user='root',password='gkx321',database='db222')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) def select(sql,cursor,conn):
ret = cursor.execute(sql)
print('受影响行数为%s行'%ret)
ret2 = cursor.fetchall()
return ret2 def drop(sql,cursor,conn):
ret = cursor.execute(sql)
return '\033[34m操作成功\033[0m' def change(sql,cursor,conn):
ret = cursor.execute(sql)
conn.commit()
if ret:
return '\033[34m受影响行数为%s行\033[0m'%ret
else:
print('输入不正确')
return False # def quit(sql, cursor, conn):
# print('退出了')
# exit()
def login(cursor,conn):
count = 0
while count<3:
user = input('用户名:')
pwd = input('密码:')
sql = "select * from user where username=%s and password=%s"
ret = cursor.execute(sql,(user,pwd))
if ret:
sql = "select * from u_g left join grant_info on u_g.g_id = grant_info.id left join user on u_g.u_id = user.id;"
cursor.execute(sql)
ret = cursor.fetchall()
for line in ret:
if line['username'] == user:
print('%s的权限是%s'%(line['username'],line['grant_lst']))
return True
else:
print('用户信息有误')
count += 1
return False def op():
while True:
menu = [('查询','select'),
('增加','change'),
('新建表','drop'),
('删除','drop'),
('更新','change'),
('退出','')
]
for index,item in enumerate(menu,1):
if index < len(menu):
print('%s:%s'%(index,item[0]),end=' ')
else:
print('%s:%s'%(index,item[0]))
try:
choi = int(input('选择操作序号:'))
if choi == 6:
print('\033[31m已选择退出\033[0m')
break
print('\033[32m进行%s操作\033[0m' % menu[choi - 1][0])
except:
print('\033[31m选择有误,请输入序号\033[0m')
continue
print('\033[34m输入sql语句,或者按q返回\033[0m')
sql = input('输入sql命令>>:')
if sql == 'q':
continue try:
func = getattr(sys.modules[__name__], menu[choi - 1][1])
ret = func(sql, cursor, conn)
if choi != 1:
print(ret)
else:
for index,item in enumerate(ret):
print(item)
except:
print('\033[31m语法有误,重新输入\033[0m') cursor.close()
conn.close() ret = login(cursor, conn)
if ret:
op()

pymysql作业1

pymysql作业1:简单实现用pymysql进行数据库管理

import random
import pymysql conn = pymysql.connect(host='localhost',user='root',password='gkx321',database='db300w',charset='utf8')
cursor = conn.cursor() #
# name = 'alex'
# emai = 'email'
# for i in range(0,2000000):
# i = str(i)
# gender = random.choice(['男', '女'])
# sql = "insert into userinfo(name,email,gender) values(%s,%s,gender)"
# cursor.execute(sql % (name+i,emai+i))
# conn.commit()
#
# cursor.close()
# conn.close() name = 'alex'
emai = 'email'
for i in range(3,3000001):
gender = random.choice(['男', '女'])
sql = " update userinfo set gender='%s' where id =%s;"% (gender,i)
cursor.execute(sql)
conn.commit() cursor.close()
conn.close() #sql 拼接的时候,如果 %s 不加单引号,那么sql语句里也不会有单引号

pymysql作业2

pymysql作业2:用pymysql向表格插入300万条数据

# ===>要先问清楚,要求是什么:公司开发一个项目,假设100天,可能有70天在明确需求
# 1. 基于角色的权限管理
# 2. 明确需求分析 # 基于角色的权限管理
#
# 用户信息
# id username pwd role_id
# 1 alex 123123 1
# 2 eric 123123 1
#
# 权限
# 1 订单管理
# 2 用户劵
# 3 Bug管理
# ....
#
# 角色表:
# 1 IT部门员工
# 2 咨询员工
# 3 IT主管
#
# 角色权限管理
#角色 #权限
# 1 1
# 1 2
# 3 1
# 3 2
# 3 3 #那如果一个用户有多个角色呢,可以再多一个表 角色和用户关系表

作业心得

要先问清楚,要求是什么:公司开发一个项目,假设100天,可能有70天在明确需求

import pymysql

conn = pymysql.connect(host='localhost',user='root',password='gkx321',database='sql_homework',charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.callproc('p3',(12,2)) #n1是12,无所谓n2是多少,不影响
ret = cursor.fetchall()
print(ret) #out的值要这么获取,固定写法
cursor.execute('select @_p3_0,@_p3_1') #可以只写一个 0获取n1,1获取n2
ret = cursor.fetchall()
print(ret) '''
为什么格式是这样呢,因为pymysql内部帮我们做了这样子的操作
set @ _p3_0 = 12
set @ _p3_1 = 2
call p3( @ _p3_0,@_p3_1)
select @ _p3_0,@_p3_1
''' # cursor.execute('select * from teacher')
# ret = cursor.fetchall()
# print(ret) cursor.close()
conn.close()

Pymysql-存储过程的操作

# http://www.runoob.com/mysql/mysql-sql-injection.html
# https://www.cnblogs.com/sdya/p/4568548.html # sql = "select * from userinfo where username='%s' and password = '%s' "%(user,pwd)
#如果我们这么拼接字符串
# 当 %s 传入为 uu' or 1=1 -- 那么sql语句变为 # sql = "select * from userinfo where username='uu' or 1=1 -- ' and password = '%s' "%(user,pwd) (-- 为sql中的注释符号)
# 从而让sql变得不安全,可以随便登陆 具体见 上面例子中,《pymysql初识》

mysql注入

二.navicat是MySQL其中一个可视化软件

#·······建议还是用终端使用MySQL·······

#通过以下2步才能用 8.0登陆 navicat:
# mysql加密规则 https://www.cnblogs.com/atuotuo/p/9402132.html
# ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'gkx321';
# 把mysql 8.0加密规则 从caching_sha2_password 更改为 mysql_native_password
# Update user set host='%' where user='root';
# select user,host,plugin from user #我们用navicat这个IDE,主要还是为了学习过程中,存储和查看命令
#生产过程中一定不要用,会被鄙视。
'''
#1.转储数据库文件:
#对着db右键,可以把数据库的结构和数据导出
#导入的话,直接新建查询,把sql的文件粘贴进入,运行即可。 #那在终端要怎么备份呢?
#用 mysqldump
#备份:数据表结构和数据
#打开cmd 输入命令: mysqldump -u root db1 > db1.sql -p 回车输入密码即可
#保存位置在 cmd行输入时候的路径,文件名为 db1.sql
#只备份:数据表结构:
#mysqldump -u root -d db1 > db1.sql -p #多了个 -d
#那备份的怎么导入呢?
#首先创建一个数据库 create database db_name;
#命令: mysqldump -uroot -p密码 db_name < 文件路径
'''

navicat初识

D:\python-全栈九期\navicat12

安装包

MySQL— pymysql模块(防止sql注入),可视化软件Navicat的更多相关文章

  1. python连接MySQL pymysql模块,游标,SQL注入问题,增删改查操作

    pymysql模块 pymysql是用python控制终端对MySQL数据库进行操作的第三方模块 import pymysql # 1.连接数据库 client = pymysql.connect( ...

  2. 数据库——可视化工具Navicat、pymysql模块、sql注入问题

    数据库--可视化工具Navicat.pymysql模块.sql注入问题 Navicat可视化工具 Navicat是数据库的一个可视化工具,可直接在百度搜索下载安装,它可以通过鼠标"点点点&q ...

  3. 第二百八十一节,MySQL数据库-SQL注入和pymysql模块防止SQL注入

    MySQL数据库-SQL注入和pymysql模块防止SQL注入 SQL注入就是通过SQL语句绕开程序判断,获取到数据库的内容 下面以一个简单的程序登录SQL注入举例: 正常登录 1.数据库有一张会员表 ...

  4. SQL语句,pymysql模块,sql注入问题

    一.完整版SQL语句的查询 select distinct post,avg(salary) from table where id > 1 group by post` having avg( ...

  5. pymysql模块 执行sql封装

    封装pymysql模块执行sql class HandCost(object): """ 处理数据库中的数据 """ def __init_ ...

  6. MySql(四)SQL注入

    MySql(四)SQL注入 一.SQL注入简介 1.1 SQL注入流程 1.2 SQL注入的产生过程 1.2.1 构造动态字符串 转义字符处理不当 类型处理不当 查询语句组装不当 错误处理不当 多个提 ...

  7. mysql(单表查询,多表查询,MySQl创建用户和授权,可视化工具Navicat的使用)

    单表查询 语法: 一.单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT ...

  8. day40:python操作mysql:pymysql模块&SQL注入攻击

    目录 part1:用python连接mysql 1.用python连接mysql的基本语法 2.用python 创建&删除表 3.用python操作事务处理 part2:sql注入攻击 1.s ...

  9. MySQL-注释-Navicat基本使用-复杂查询练习题-解题思路-pymysql操作数据库-SQL注入-05

    目录 mysql语句注释 navicat 的基本使用 特色(个人总结) 与数据服务器建立连接 创建&打开数据库.表 创建 打开 修改操作表结构 修改表结构 查询修改操作表数据 基本语句对应的操 ...

随机推荐

  1. 【vue】清理代码

    // 一次性将这个日期选择器附加到一个输入框上 // 它会被挂载到 DOM 上. mounted: function () { // Pikaday 是一个第三方日期选择器的库 this.picker ...

  2. 基于Java服务的前后端分离解决跨域问题

    导语:解决跨域问题,前后端都增加相应的允许跨域的代码段即可. 一.后端增加允许跨域的代码,可以在具体controler层加,最好是在filter中添加,这样添加一次就够了,不用在每个controler ...

  3. python基础_格式化输出(%用法和format用法)

      目录 %用法 format用法 %用法 1.整数的输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 1 >>> print('%o' % 2 ...

  4. postgres跨平台开发坑之空值

    ngx_lua架构下查询linux版postgres时,如果目标字段的值返回空,则返回结果为 ngx.null,同样的代码如果查询windows版postgres时,如果目标字段的值返回空,则返回结果 ...

  5. lsyncd+rsync配置图片资源双向同步

    需求:为保证国内外图片加载速度,国内请求上传图片资源地址阿里云oss,国外请求上传图片资源地址aws s3,为保证图片资源的一致性,需定时进行oss和s3图片双向同步 调研方案:由于之前配置过inot ...

  6. POJ 2663 Tri Tiling 【状压DP】

    Description In how many ways can you tile a 3xn rectangle with 2x1 dominoes?  Here is a sample tilin ...

  7. DP一下,马上出发

    简单DP i.May I ask you for a dance(体舞课软广植入) 这题的状态转移方程为:dp[i][j]=max(dp[i-1][j-1]+a[i][j],dp[i][j-1]);( ...

  8. (转)Extracting knowledge from knowledge graphs using Facebook Pytorch BigGraph.

    Extracting knowledge from knowledge graphs using Facebook Pytorch BigGraph 2019-04-27 09:33:58 This ...

  9. bzoj2131 免费的馅饼——树状数组优化dp

    中文题目,问你最后能最多够得到多少价值的馅饼.因为宽度10^8且个数为10^5.所以不可以用dp[x][y]表示某时间某地点的最大权值. 假设你在x点处接到饼后想去y点接饼.那么需要满足的条件是t[y ...

  10. C++_day8pm_多态

    1.什么是多态 2. 示例代码: #include <iostream> using namespace std; //形状:位置.绘制 //+--圆形:半径.(绘制) //+--矩形:长 ...