首页 > 其他 > 详细

第三十八天

时间:2019-08-23 22:24:09      阅读:82      评论:0      收藏:0      [点我收藏+]

Python操作MySQL

模块pyMySQL

 

# 模块:pymysql
import pymysql


conn = pymysql.connect(
    host = 127.0.0.1,
    port = 3306,
    user = root,
    password = 123,
    database = day38,
    charset = utf8  # 编码千万不要加- 如果写成了utf-8会直接报错
)
cursor = conn.cursor(pymysql.cursors.DictCursor)  # 产生一个游标对象  以字典的形式返回查询出来的数据 键是表的字段  值是表的字段对应的信息
sql = select * from teacher
cursor.execute(sql)  # 执行传入的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())  # 获取所有的数据  返回的结果是一个列表
print(cursor.fetchmany(5))  # 获取括号中输入的数字个数个数据  返回的结果是一个列表

 

 

MySQL注入问题

 

import pymysql
conn = pymysql.connect(
    host = 127.0.0.1,
    port = 3306,
    user = root,
    password = 123,
    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()  # 写在执行后面,提交sql语句
"""
增删改操作 都必须加一句
conn.commit()操作
"""

# username = input(‘username>>>:‘)
# password = input(‘password>>>:‘)
# sql = "select * from user where name =%s and password = %s"
# print(sql)
# res = cursor.execute(sql,(username,password))  # 能够帮你自动过滤特殊符号 避免sql注入的问题
# # execute 能够自动识别sql语句中的%s 帮你做替换
# if res:
#     print(cursor.fetchall())
# else:
#     print(‘用户名或密码错误‘)


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

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

"""

 

第三十八天

原文:https://www.cnblogs.com/AbrahamChen/p/11400373.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!