1、Navicat的安装下载

一、Navicat
在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时,
可以使用可视化工具Navicat,以图形界面的形式操作MySQL数据库 官网下载:https://www.navicat.com/en/products/navicat-for-mysql
网盘下载:https://pan.baidu.com/s/1bpo5mqj
链接:https://pan.baidu.com/s/1Hu-x0mPuSW3g9CxNFlnAng 密码:pqe5
使用网盘的有免费的注册码
# 打开 双击:
# D:\navicatformysql\Navicat for MySQL\navicat 需要掌握的基本操作
掌握:
#1. 测试+链接数据库
#2. 新建库
#3. 新建表,新增字段+类型+约束
#4. 设计表:外键
#5. 新建查询
#6. 备份库/表 注意:
批量加注释:ctrl+?键
批量去注释:ctrl+shift+?键

新建查询:

ctrl +? 注释代码

ctrl + shift+/ 解除代码注释

备份库:

方面自己开发时调试用

二、pymysql模块

介绍:
在python程序中操作数据库呢?这就用到了pymysql模块,
该模块本质就是一个套接字客户端软件,使用前需要事先安装
pip3 install pymysql
前提:
授权加创建
grant all on *.* to 'root'@'%' identified by '';
flush privileges; 端口:3306
ip: 192.168.1.102
mysql -uroot -p123 -h 192.168.1.102

pymysql模块之基本使用:

# pip3 install pymysql
import pymysql
user=input('user>>').strip()
pwd=input('password>>').strip() # 建立连接
conn=pymysql.connect(
host=服务端ip
port=3306,
user='root',
password='',
db='db8',
charset='utf8'
)
# 拿到游标
cursor=conn.cursor()
# 相当于 >mysql 输入命令 # 执行sql语句
sql = 'select * from user_info where user="%s" and pwd="%s"' %(user,pwd) rows = cursor.execute(sql)
cursor.close()
conn.close()
if rows:
print('welcome')
else:
print('sorry')

pymysql模块之sql注入问题,解决上面程的bug

非法字符的输入可以登录成功

 2.sql注入
注意:符号--会注释掉它之后的sql,正确的语法:--后至少有一个任意字符
1、sql注入之:用户存在,绕过密码
egon' -- 任意字符 2、sql注入之:用户不存在,绕过用户与密码
xxx' or 1=1 -- 任意字符

-- 之后的sql均被注释掉了

or 1=1 永远成立

 解决办法:

 解决办法:
# 原来是我们对sql进行字符串拼接
# sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd)
# print(sql)
# res=cursor.execute(sql) #改写为(execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了)
sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引号,因为pymysql会自动为我们加上
res=cursor.execute(sql,(user,pwd)) #pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。

代码:

# pip3 install pymysql
import pymysql
user=input('user>>').strip()
pwd=input('password>>').strip() # 建立连接
conn=pymysql.connect(
host='10.10.40.140',
port=3306,
user='root',
password='',
db='db8',
charset='utf8'
)
# 拿到游标
cursor=conn.cursor()
# 相当于 >mysql 输入命令 # 执行sql语句
#sql = 'select * from user_info where user="%s" and pwd="%s"' %(user,pwd)
#print(sql)
sql = 'select * from user_info where user=%s and pwd=%s' rows = cursor.execute(sql,[user,pwd])
cursor.close()
conn.close()
if rows:
print('welcome,登录成功')
else:
print('sorry,登录失败')

3、pymysql模块之增删改查

增:

import pymysql
#链接
conn=pymysql.connect(host='localhost',user='root',password='',database='egon')
#游标
cursor=conn.cursor() #执行sql语句
#part1
# sql='insert into userinfo(name,password) values("root","123456");'
# res=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数
# print(res) #part2
# sql='insert into userinfo(name,password) values(%s,%s);'
# res=cursor.execute(sql,("root","123456")) #执行sql语句,返回sql影响成功的行数
# print(res) #part3
sql='insert into userinfo(name,password) values(%s,%s);'
res=cursor.executemany(sql,[("root",""),("lhf",""),("eee","")]) #执行sql语句,返回sql影响成功的行数
print(res) conn.commit() #提交后才发现表中插入记录成功
cursor.close()
conn.close()

查:fetchone(每次查询一个),fetchmany(查询多个),fetchall全部

import pymysql
#链接
conn=pymysql.connect(host='localhost',user='root',password='',database='egon')
#游标
cursor=conn.cursor() #执行sql语句
sql='select * from userinfo;'
rows=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数rows,将结果放入一个集合,等待被查询 # cursor.scroll(3,mode='absolute') # 相对绝对位置移动
# cursor.scroll(3,mode='relative') # 相对当前位置移动
res1=cursor.fetchone()
res2=cursor.fetchone()
res3=cursor.fetchone()
res4=cursor.fetchmany(2)
res5=cursor.fetchall()
print(res1)
print(res2)
print(res3)
print(res4)
print(res5)
print('%s rows in set (0.00 sec)' %rows) conn.commit() #提交后才发现表中插入记录成功
cursor.close()
conn.close() '''
(1, 'root', '123456')
(2, 'root', '123456')
(3, 'root', '123456')
((4, 'root', '123456'), (5, 'root', '123456'))
((6, 'root', '123456'), (7, 'lhf', '12356'), (8, 'eee', '156'))
rows in set (0.00 sec)
'''

获取插入的最后一条数据的自增ID

print(cursor.lastrowid) #在插入语句后查看

import pymysql
conn=pymysql.connect(host='localhost',user='root',password='',database='egon')
cursor=conn.cursor() sql='insert into userinfo(name,password) values("xxx","123");'
rows=cursor.execute(sql)
print(cursor.lastrowid) #在插入语句后查看 conn.commit() cursor.close()
conn.close()

 

 

11 MySQL--Navicat与pymysql模块的更多相关文章

  1. navicat工具 pymysql模块

    目录 一 IDE工具介绍(Navicat) 二 pymysql模块 一 IDE工具介绍(Navicat) 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具,我们使用Navi ...

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

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

  3. MySQL多表查询,Navicat使用,pymysql模块,sql注入问题

    一.多表查询 #建表 create table dep( id int, name varchar(20) ); create table emp( id int primary key auto_i ...

  4. mysql五:pymysql模块

    一.介绍 之前都是通过MySQ自带的命令行客户端工具Mysql来操作数据库,那如何在Python程序中操作数据库呢?这就需要用到pymysql模块了. 这个模块本质就是一个套接字客户端软件,使用前需要 ...

  5. navicat 使用 pymysql模块

    新健库 ,新增字段+类型+约束 设计表:外键(自增) 新建查询 建立表模型 /* 数据导入: Navicat Premium Data Transfer Source Server : localho ...

  6. 数据库 - Navicat与pymysql模块

    一.Nabicat 在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时, 可以使用可视化工具Navicat,以图形界面的形式操作MySQL数据库 官网下载:htt ...

  7. MySQL学习12 - pymysql模块的使用

    一.pymysql的下载和使用 1.pymysql模块的下载 2.pymysql的使用 二.execute()之sql注入 三.增.删.改:conn.commit() 四.查:fetchone.fet ...

  8. Python连接MySQL数据库之pymysql模块使用

    安装PyMySQL pip install pymysql PyMySQL介绍 PyMySQL是在python3.x版本中用于连接MySQL服务器的一个库,2中则使用mysqldb. Django中也 ...

  9. 操作mysql(import pymysql模块)

    pymysql模块 import pymysql #1.连上数据库.账号.密码.ip.端口号.数据库 #2.建立游标 #3.执行sql #4.获取结果 #5.关闭游标 #6.连接关闭 #charest ...

  10. Python连接MySQL数据库之pymysql模块

    pymysql 在python3.x 中用于连接MySQL服务器的一个库:Python2中则使用mysqldb pymysql的模块的基本的使用 # 导入pymysql模块 import pymysq ...

随机推荐

  1. 谈谈oracle里的join、left join、right join、full join-版本2

    --1.left join  左表为主表,左表返回全部数据,右表只返回与左表相匹配的数据select   t1.fpdm,t1.fphm ,t1.zjr,t1.zjsj,t1.zjjx,t1.zjje ...

  2. krb5-libs这个RPM包删掉了导致ssh无法连接

    不小心把krb5-libs-1.4.3-5.1.i386.rpm这个包给删掉了~~(用了--nodeps参数,万恶之源阿~~) 背景: 安装kerberos-server时,后来发现不需要了就把安装软 ...

  3. vue.js单个slot

    刚开始看这个slot的时候有点蒙,想了几分钟才明白过来,汗颜 <script> var mycompoent = Vue.extend({ template:"<div&g ...

  4. 143. Long Live the Queen 树形dp 难度:0

    143. Long Live the Queen time limit per test: 0.25 sec. memory limit per test: 4096 KB The Queen of ...

  5. vue.js 源代码学习笔记 ----- html-parse.js

    /** * Not type-checking this file because it's mostly vendor code. */ /*! * HTML Parser By John Resi ...

  6. linux find查找并拷贝 exec xargs区别

    -exec    1.参数是一个一个传递的,传递一个参数执行一次rm    2.文件名有空格等特殊字符也能处理-xargs     1.一次将参数传给命令,可以使用-n控制参数个数    2.处理特殊 ...

  7. OutOfMemoryError系列(1): Java heap space

    每个Java程序都只能使用一定量的内存, 这种限制是由JVM的启动参数决定的.而更复杂的情况在于, Java程序的内存分为两部分: 堆内存(Heap space)和 永久代(Permanent Gen ...

  8. C# 调用C++ DLL 的类型转换(转载版)

    最近在做视频监控相关的demo开发,实现语言是C#,但视频监控的SDK是C++开发的,所以涉及到C#调用C++的dll库.很多结构体.参数在使用时都要先进行转换,由非托管类型转换成托管类型后才能使用. ...

  9. Linux运维学习笔记-常用快捷键及vi、vim总结

    vim是vi的增强版,vim完全兼容vi

  10. Git核心概念

    Git作为流行的分布式版本管理系统,用好它要理解下面几个核心的概念. 1.Git保寸的是文件完整快照,而不是差异变化或者文件补丁.每次提交若文件有变化则会指向上一个版本的指针而不重复生成副本. Git ...