python编程快速上手(持续更新中…)
Python操作数据库查
Python操作数据库步骤
Python连接MySQL
-
作用:使用python代码充当客户端,连接数据库进行操作
-
使用的步骤:
-
导入模块 pymysql
improt pymsql
-
建立连接对象 pymysql.connect()
# host 主机 # user 用户名 # password 密码 # database 指定数据库 conn = pymysql.connect(host='localhost',user='root', password='mysql', database='jing_dong')
port 默认为3306
-
创建游标对象
cur = conn.cursor()
-
使用游标对象执行SQL语句
cur.execute(“sql语句”)
execute() 有返回值
1)增删改,影响的行数
2)查询, 总记录数
-
获取执行的结果
-
取1条 cur.fetchone() # 元组
-
取所有 cur.fetchall() # ((),(),())
-
-
打印输出获取的内容
for line in result_list:
? print(line)
-
关闭游标对象
cur.close()
-
关闭连接对象
conn.close()
-
Python操作数据库增删改
操作步骤
-
导入模块
-
创建连接对象
-
创建游标对象
-
使用游标对象执行SQL
execute()
-
提交
conn.commit()
-
获取执行的结果(影响的行数)并打印执行的结果
-
关闭游标
-
关闭连接
相关代码
import pymysql
# 1、导入模块
# 2、创建连接对象
conn = pymysql.connect(host="localhost",user="root",password="root", database="python_test_1")
# 3、创建游标对象
cur = conn.cursor()
# 4、使用游标对象执行SQL
# sql = "insert into goods values(null, '老王牌拖拉机',1,1,9998,1,1)"
# sql = "delete from goods where id = 7"
sql = "update goods set name='最新款老王牌拖拉机' where id = 6"
ret = cur.execute(sql)
# 5、提交
# conn.commit() 提交刚刚执行的SQL
conn.commit()
# 6、获取执行的结果(影响的行数)
# 7、打印执行的结果
print("影响行数:", ret)
# 8、关闭游标
cur.close()
# 9、关闭连接
conn.close()
SQL防注入
防注入的思路:
-
sql中需要变化的地方,可以占位符 %s %d…
sql =“select * from goods where name = %s order by id desc”
注意:SQL 可以出现多个占位符,后续列表中元素的个数要与之对应
-
把参数封装到 列表中
params = [input_name]
-
把列表传递给 execute(sql, 列表)
-
result = cur.execute(sql, params)
参数查询
import pymysql
# 1、导入模块 pymysql
# 2、建立连接对象 pymysql.connect()
conn = pymysql.connect(host='localhost', user='root', password='root', database='python_test_1')
# 3、创建游标对象
cur = conn.cursor()
input_name = input("请输入要查询的名称:\n")
# 4、使用游标对象执行SQL语句
sql = "select * from goods where name = '%s' order by id desc" % input_name
result = cur.execute(sql)
print("查询到:%s条数据" % result)
# 5、获取执行的结果
# cur.fetchone() 从查询的结果中取出一条数据
# result_list = cur.fetchone()
# ((),(),())
result_list = cur.fetchall()
# 6、打印输出获取的内容
# print(result_list)
for line in result_list:
# line 一行 是一个元组
print(line)
# 7、关闭游标对象
cur.close()
# 8、关闭连接对象
conn.close()
SQL注入
sql = “select * from goods where name = ‘%s’ order by id desc” % input_name
input_name = ’ or 1 or ’
防注入
sql = “select * from goods where name = %s order by id desc”
params = [input_name]
result = cur.execute(sql, params)
索引
代码
-
索引作用:提升查询效率
-
索引的使用:
-
查看索引 show index from 表名
-
创建索引: crete index 索引名 on 表名(表中的字段名(字段长度))
如果字段是字符串类型,需要指定长度
如果字段不是字符串类型,可以不指定长度
-
删除索引: drop index 索引名 on 表名;
-
-
插入10万条数据到数据库中
目标:插入 100000 数据到 库中的 test_index 表
pymysql操作步骤:
1、导入模块
2、创建连接对象
3、创建游标对象
4、for循环,插入10万条数据
5、提交数据
6、关闭游标
7、关闭连接
import pymysql
# 1、导入模块
# 2、创建连接对象
conn = pymysql.connect(host="localhost", user="root", password="root", database="python_test_1")
# 3、创建游标对象
cur = conn.cursor()
# 4、for循环,插入10万条数据
for i in range(100000):
cur.execute("insert into test_index(title) values('ha-%d')" % i)
# 5、提交数据
conn.commit()
# 6、关闭游标
cur.close()
# 7、关闭连接
conn.close()
创建索引
字符串需要加长度
create index idx_title on test_index(title(100));
或者
alter table test_index add index idx_title (title)
删除索引
drop index idx_title on test_index;
验证索引效果
- 开启检测 set profiling = 1;
- 执行sql select * from test_index where title = ‘ha-99999’;
- 查看每一个sql执行的时间 show profiles;
- 添加索引 alter table test_index add index idx_title (title);
- 执行sql select * from test_index where title = ‘ha-99999’;
- 查看 执行效率 show profiles;