navicat可视化界面操作数据库 python如何操作MySQL(pymysql模块) sql注入问题 pymysql模块增删改查数据操作

内容详细

Navicat软件

"""
一开始学习python的时候 >>>下载python解释器然后直接在终端书写
>>> pycharm能够更加方便快捷的帮助你书写python代码
excel word pdf

我们在终端操作MySQL 也没有自动提示也无法保存等等 不方便开发
Navicat内部封装了所有的操作数据库的命令 
用户在使用它的时候只需要鼠标点点即可完成操作 无需书写sql语句
"""

安装

直接百度搜索  或者破解一下也很简单
https://www.cr173.com/soft/126934.html
    
下载完成后是一个压缩包 直接解压 然后点击安装 有提醒直接点击next即可

navicat能够充当多个数据库的客户端


navicat图形化界面有时候反应速度较慢 你可以选择刷新或者关闭当前窗口再次打开即可

当你有一些需求该软件无法满足的时候 你就自己动手写sql预览里面(比如unique 唯一)

 查看表的字段:

如何创建数据库? (排序规则不需要填写)

 如何创建表?(Tab键切换换行)然后点保存即可

 如何添加表数据?

直接双击表名 然后进去填写即可(id 字段自动填写 有key键)

 如何创建外键foreign key??

 如何快速查看表与表之间的关系和字段与字段之间的关系?

数据怎么传出和传入呢???

传出>>>:

 

 传入>>>:

提示

"""
1 MySQL是不区分大小写的
	验证码忽略大小写
		内部统一转大写或者小写比较即可
			upper
			lower

2 MySQL建议所有的关键字写大写

3 MySQL中的注释 有两种
	--
	#

4 在navicat中如何快速的注释和解注释
	ctrl + ?  加注释
	ctrl + ?  基于上述操作再来一次就是解开注释
	如果你的navicat版本不一致还有可能是
	ctrl + shift + ?解开注释
"""

pymysql模块

"""
支持python代码操作数据库MySQL
"""
pip3 install pymysql

pip 阿里源
pip3 install pymysql -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

import pymysql

conn = pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    passwd='123',
    database='day48',
    charset='utf8'      # 编码千万不要加-
                )    # 链接数据库

cursor = conn.cursor() # 产生一个游标对象 相当于cmd 输入 mysql 进入后 mysql>>>: 等待输入命令的阶段
# cursor : 就是用来帮你执行命令的
sql = 'select * from emp;'
res = cursor.execute(sql)

# ======================================================================
# print(res)  # execute 返回的是你当前的sql到底拿出来了多少条数据
# print(cursor.fetchone())    # 只拿一条数据
# print(cursor.fetchall())    # 拿所有数据
# print(cursor.fetchmany(2))   # 括号内写几就拿几条数据 不写默认为1
# ======================================================================

print(cursor.fetchone())    #拿一条
print(cursor.fetchone())    # fetchone 读取数据类似于文件的光标的移动(读一次光标就移动到下一次)
cursor.scroll(1,'relative')     # 相对于光标所在位置继续往后移动一位
cursor.scroll(1,'absolute')     # 相对于数据的开头往后继续移动一位
print(cursor.fetchall())

execute():

输出的结果为: 

 fetchone() : 只拿一条数据 

 缺点: 当一条数据过长的时候不知道数据的结果是什么意思所以在cursor()里面加以下内容:

cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

'''
cursor=pymysql.cursors.DictCursor 将查询的结果以字典的形式返回

sql注入

"""
利用一些语法的特性 书写一些特点的语句实现固定的语法
MySQL利用的是MySQL的注释语法
select * from user where name='jason' -- jhsadklsajdkla' and password=''

select * from user where name='xxx' or 1=1 -- sakjdkljakldjasl' and password=''
"""
日常生活中很多软件在注册的时候都不能含有特殊符号
因为怕你构造出特定的语句入侵数据库 不安全

# 敏感的数据不要自己做拼接 交给execute帮你拼接即可
# 结合数据库完成一个用户的登录功能?
import pymysql


conn = pymysql.connect(
    host = '127.0.0.1',
    port = 3306,
    user = 'root',
    password = '123456',
    database = 'day48',
    charset = 'utf8'  # 编码千万不要加-
)  # 链接数据库
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

username = input('>>>:')
password = input('>>>:')
sql = "select * from user where name=%s and password=%s"
# 不要手动拼接数据 先用%s占位 之后将需要拼接的数据直接交给execute方法即可
rows = cursor.execute(sql,(username,password))  # 自动识别sql里面的%s用后面元组里面的数据替换
if rows:
    print('登录成功')
    print(cursor.fetchall())
else:
    print('用户名密码错误')

pymysql补充

# 1.针对增删改 pymysql需要二次确认才能真正的操作数据
import pymysql


conn = pymysql.connect(
    host = '127.0.0.1',
    port = 3306,
    user = 'root',
    passwd = '123456',
    db = 'day48',
    charset = 'utf8',
    autocommit = True
)
cursor = conn.cursor(pymysql.cursors.DictCursor)

# 增
sql = 'insert into user(name,password) values(%s,%s)'
# rows = cursor.execute(sql,('jackson',123))
rows = cursor.executemany(sql,[('xxx',123),('ooo',123),('yyy',123)])
print(rows)
# conn.commit()  # 确认
# 修改
# sql = 'update user set name="jasonNB" where id=1'
# rows = cursor.execute(sql)
# print(rows)
# conn.commit()  # 确认 同(autocommit = Ture )
# 删除
sql = 'delete from user where id=7'
rows = cursor.execute(sql)
print(rows)
conn.commit()  # 确认
# 查
# sql = 'select * from user'
# cursor.execute(sql)
# print(cursor.fetchall())

"""
增删改查中
    删改增它们的操作设计到数据的修改 
    需要二次确认
"""


# 还可以一次性插入N多条数据
rows = cursor.executemany(sql,[('xxx',123),('ooo',123)])

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值