灵虚御风
醉饮千觞不知愁,忘川来生空余恨!

导航

 
import pymysql

conn = pymysql.connect(
    host = '127.0.0.1',
    port = 3306,
    user = 'root',
    password = 'llx20190411',
    database = 'day38',
    charset = 'utf8'# 编码千万不要加- 如果写成了utf-8会直接报错

)
cursor = conn.cursor(pymysql.cursors.DictCursor)  # 产生一个游标对象  以字典的形式返回查询出来的数据 键是表的字段  值是表的字段对应的信息
sql = 'select * from teacher'
cursor.execute(sql)  # 执行传入的sql语句
# res = cursor.execute(sql)
# print(res)  # res是执行语句返回的数据条数
print(cursor.fetchone()) ## 只获取一条数据
print(cursor.fetchone())  # 只获取一条数据
print(cursor.fetchone())  # 只获取一条数据
cursor.scroll(2,'absolute') # 控制光标移动   absolute相对于其实位置 往后移动几位
cursor.scroll(1,'relative')  # relative相对于当前位置 往后移动几位
print(cursor.fetchall())  # 获取所有的数据  返回的结果是一个列表
02 python操作MySQL.py
import pymysql

conn = pymysql.connect(
    host = '127.0.0.1',
    port = 3306,
    user = 'root',
    password = 'llx20190411',
    database = 'day38',
    charset = 'utf8',  # 编码千万不要加- 如果写成了utf-8会直接报错
    autocommit = True  # 这个参数配置完成后  增删改操作都不需要在手动加conn.commit了
)
cursor = conn.cursor(pymysql.cursors.DictCursor)  # 产生一个游标对象  以字典的形式返回查询出来的数据 键是表的字段  值是表的字段对应的信息

# sql = 'insert into user(name,password) values("jerry","666")'
# sql = 'update user set name = "jasonhs" where id = 1'
# sql = 'delete from user where id = 6'
# cursor.execute(sql)


"""
增删改操作 都必须加一句
conn.commit()操作
"""
# conn.commit()
username = input('username>>>:')
password = input('password>>>:')
sql = "select * from userinfo where name =%s and  passwprd= %s"
print(sql)
res = cursor.execute(sql,(username,password))  # 能够帮你自动过滤特殊符号 避免sql注入的问题
# # execute 能够自动识别sql语句中的%s 帮你做替换
if res:
    print(cursor.fetchall())
else:
    print('用户名或密码错误')


"""
sql注入 就是利用注释等具有特殊意义的符号 来完成一些骚操作

后续写sql语句  不要手动拼接关键性的数据
而是让excute帮你去做拼接




"""
3.sql 注入问题.py

 


# exist(了解)
EXISTS关字键字表示存在。在使用EXISTS关键字时,内层查询语句不返回查询的记录,
而是返回一个真假值,True或False。
当返回True时,外层查询语句将进行查询
当返回值为False时,外层查询语句不进行查询。
select * from emp
where exists
(select id from dep where id > 203);


Navicat使用
下载地址:<https://pan.baidu.com/s/1bpo5mqj>


掌握:
#1. 测试+链接数据库
#2. 新建库 utf8mb4 可以存表情
#3. 新建表,新增字段+类型+约束
#4. 设计表:外键
#5. 新建查询
#6. 建立表模型

#注意:
批量加注释:ctrl+?键
批量去注释:ctrl+shift+?键
```

数据导入:
Navicat Premium Data Transfer


Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50624
Source Host : localhost
Source Database : sqlexam

Target Server Type : MySQL
Target Server Version : 50624
File Encoding : utf-8

Date: 10/21/2016 06:46:46 AM
*/

2.python 操作 mysql

pymysql模块
#链接
conn=pymysql.connect(
host='localhost',
user='root',
password='123',
database='egon',
charset='utf8')
#游标
cursor=conn.cursor() #执行完毕返回的结果集默认以元组显示
#cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) # 以字典的方式显示数据

# 3.pymysql操作数据库
#执行sql语句
user = input(">>>:").strip()
pwd = input(">>>:").strip()
sql='select * from userinfo where name="%s" and password="%s"' %(user,pwd) #注意%s需要加引号

rows=cursor.execute(sql) #执行sql语句,返回sql查询成功的记录数目
# 获取真实数据cursor.fetchone(),cursor.fetchall(),cursor.fetchmany(),类似管道取值,获取一条,所有,多条

cursor.scroll(1,'relative') # 相对移动
cursor.scroll(3,'absolute') # 绝对移动


cursor.close()
conn.close()
```


# 1.安装:pip3 insatll pymysql
# 2.代码链接
import pymysql


3.SQL注入问题及解决


posted on 2022-03-28 20:26  没有如果,只看将来  阅读(32)  评论(0编辑  收藏  举报