# #### 引入pymysql 模块
import pymysql
# 基本语法
创建连接 host user password database
conn = pymysql.connect(host = "192.168.50.128",user="wangzhen",password = "123",database ="wangzhen",charset = "utf8",port = 3306 )
创建游标对象 对象可以进行增删改查操作
cursor = conn.cursor()
执行sql语句
sql = "select * from class"
res = cursor.execute(sql)
print(res) # 数据的总条数
res = cursor.fetchone()
print(res)
res = cursor.fetchone()
print(res)
# 释放游标和关闭连接
cursor.close()
conn.close()
# 1.创建一张表
sql = """
create table t1(
id int unsigned primary key auto_increment,
first_name char(10) not null,
last_name char(10) not null,
age int unsigned,
sex tinyint,
money float
)
"""
res = cursor.execute(sql)
print(res)
# 2.查看表结构
sql = "desc t1"
res = cursor.execute(sql)
print(res) # 返回字段的个数
print(cursor.fetchone())
# 3.删除表
sql = "drop table t1"
res = cursor.execute(sql)
print(res)
cursor.close()
conn.close()
### 事务处理
python 操作事务处理 必须通过commit提交数据,
才会真正的更新数据,否则rollback回滚,恢复到以前状态
conn = pymysql.connect(host="127.0.0.1",user="root",password="",database="db0618")
cursor = conn.cursor()
sql1 = "begin"
sql2 = "select * from employee"
sql3 = "update employee set emp_name = '111egon' where id = 1 "
sql4 = "commit"
res1 = cursor.execute(sql1)
res2 = cursor.execute(sql2)
res3 = cursor.execute(sql3)
res4 = cursor.execute(sql4)
cursor.close()
conn.close()
<------------------------------------------------------------------------------->
# ### sql 注入攻击
创建一张表
sql = """
create table usr_pwd(
id int unsigned primary key auto_increment,
username varchar(255) not null,
password varchar(255) not null
)
"""
sql注入攻击
import pymysql
user = input("user>>>: ").strip()
pwd = input("password>>>: ").strip()
conn = pymysql.connect(host="127.0.0.1",user="root",password="",database="db0619")
cursor = conn.cursor()
sql = "select * from usr_pwd where username='%s' and password='%s' " % (user,pwd)
print(sql)
res = cursor.execute(sql)
print(res) # 查询数据的个数
if res:
print("登陆成功")
else:
print("登录失败")
cursor.close()
conn.close()
***注入攻击的原理***
输入时: erwe234' or 10 = 10 -- 234dfsdf
select * from usr_pwd where username='erwe234' or 10 = 10 -- 234dfsdf' and password='dfsdf'
where username='erwe234' or 10 = 10 username的判断是假的 但是 后面or拼接的条件是真的,所以可以查询成功
-- 代表后面的代码是注释
把用户名和密码都绕开了,进行sql注入攻击
***避免注入攻击的解决办法***
使用预处理机制,可以避免绝大多数的sql注入问题
execute 参数1是一个sql语句,如果sql语句和里面的参数值分开执行,默认开启预处理
execute(sql , (参数1,参数2,参数3))
import pymysql
user = input("user>>>: ").strip()
pwd = input("password>>>: ").strip()
conn = pymysql.connect(host="127.0.0.1",user="root",password="",database="db0619")
cursor = conn.cursor()
sql = "select * from usr_pwd where username=%s and password = %s"
res = cursor.execute(sql, (user,pwd))
print("登录成功" if res else "登录失败")
cursor.close()
conn.close()
<--------------------------------------------------------------------------------->
# ### python 操作mysql 增删改查
import pymysql
python 操作mysql时候,默认开启事务,必须在增删改之后
提交数据,才会对数据库产生影响,否则默认回滚
提交数据: conn.commit()
回滚数据: conn.rollback()
execute 指定单条
executemany 执行多条
# 1.创建mysql连接
conn = pymysql.connect(host="127.0.0.1",user="root",password="",database="db0619")
# 查询数据,默认是元组,可以设置返回字典类型, pymysql.cursors.DictCursor
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
# 增
sql = "insert into t1(first_name , last_name ,age ,sex,money) values(%s,%s,%s,%s,%s)"
一次插入一条数据
res = cursor.execute(sql,("王","振",80,0,12000))
print(res)
一次插入多条数据 用列表存放多条数据
res = cursor.executemany(sql,[("尉","翼麟",18,1,12000),("谢","晨",80,0,200),("主","胜",3,0,9.9)])
print(res)
获取最后插入这条数据的id号(针对于单条数据执行,获取最后的id,如果多条数据的执行,以第一条数据的id为主)
print(cursor.lastrowid)
针对于多条数据最后的id,可以通过倒序查询,找到id号
select id from t1 order by id desc limit 1
# 删
sql = "delete from t1 where id = %s"
res = cursor.execute(sql,(8,))
print(res)
if res:
print("删除成功")
else:
print("删除失败")
# 改
sql = "update t1 set first_name = %s where id = %s"
res = cursor.execute(sql,('王',10))
print(res)
if res:
print("更新成功")
else:
print("更新失败")
# 查
sql = "select * from t1"
res = cursor.execute(sql)
print(res) # 总条数
(1) 获取一条数据 fetchone
res = cursor.fetchone()
print(res)
(2) 获取多条数据 fetchmany 默认从上一条数据继续向下搜索(性质类似于迭代器)
data = cursor.fetchmany() # 没有参数,默认只获取一条
data = cursor.fetchmany(3)
print(data) 获取多条是用列表装多个字典
(3) 获取所有数据 fetchall 基于上一条数据往下搜索(性质类似于迭代器)
data = cursor.fetchall()
print(data)
(4) 自定义搜索查询的位置
sql = "select * from t1 where id >=10"
res = cursor.execute(sql)
res = cursor.fetchone()
print(res)
# 1 相对滚动
cursor.scroll(3,mode="relative")
res = cursor.fetchone()
print(res)
# 方向向前 (无论前后,不能超出范围)
cursor.scroll(-3,mode="relative")
res = cursor.fetchone()
print(res)
# 2 绝对滚动 , 永远基于第一条数据的位置滚动
cursor.scroll(0,mode="absolute")
print(cursor.fetchone())
cursor.scroll(1,mode="absolute")
print(cursor.fetchone())
cursor.scroll(2,mode="absolute")
print(cursor.fetchone())
# 往前滚没数据,超出范围error
cursor.scroll(-1,mode="absolute")
print(cursor.fetchone())
conn.commit() # 增删改一定要提交数据,否则不生效
cursor.close()
conn.close()
<--------------------------------------------------------------------------------->
### ### 数据库的导入和导出
第一步 : 先退出数据库
第二部 : 切换到对应的导出路径
第三步 :
# 导出所有
mysqldump -uroot -p 数据库名 > 数据库名.sql
# 导出单个
mysqldump -uroot -p db0619 t1 > t1.sql
# 导入数据库
第一步 : 县创建一个空的数据库
第二部 : 找到对应的sql文件的路径
第三步 : source 路径/sql文件
王振2020-6-19笔记
最新推荐文章于 2024-08-29 18:24:55 发布