pymysql
-
最基础操作
1 # -*- coding utf-8 -*- 2 # coding=utf-8 3 4 import pymysql 5 """ 6 最基础操作 7 """ 8 """ 9 1.创建连接 10 2.创建游标 11 3.执行SQL语句 12 4.提交 13 5.关闭游标 14 6.关闭连接 15 16 """ 17 18 # 创建连接 创建一个通道; 主机,用户名,端口,用户名,密码,要连接的数据库名 19 conn = pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='yy123456',db='db1') 20 21 # 创建游标 帮我们存取数据,相当于一个手,去数据库里拿东西 22 cursor = conn.cursor() 23 24 25 # 执行SQL语句,并返回受影响的行数 26 effect_row = cursor.execute("update tb1 set name='xiaobobo'") 27 28 29 30 31 32 33 34 35 36 # 提交,否则无法保存新建或已修改的数据 37 conn.commit() 38 39 # 关闭游标 40 cursor.close() 41 # 关闭连接 42 conn.close()
-
基本进阶(获取数据)
1 # -*- coding utf-8 -*- 2 # coding=utf-8 3 4 import pymysql 5 6 """ 7 基本进阶(获取数据) 8 """ 9 10 # 创建连接 创建一个通道; 主机,用户名,端口,用户名,密码,要连接的数据库名 11 conn = pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='yy123456',db='db1') 12 # 创建游标 帮我们存取数据,相当于一个手,去数据库里拿东西 13 # cursor = conn.cursor() 14 # 设置游标以字典的方式去存取数据(空参为元组形式) 15 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 16 17 18 # 执行SQL语句,并返回受影响的行数 19 # 改数据 20 # effect_row = cursor.execute("update tb1 set name='xiaobobo'") 21 # effect_row = cursor.execute("insert into tb1(name) values('jialing')") 22 # effect_row = cursor.execute("insert into tb1(name) values(%s)",('ccc')) 23 # executemany:相当于内部做了个循环,把['aaa','bbb']迭代进去,相当于执行多次execute 24 # effect_row = cursor.executemany("insert into tb1(name) values(%s)",['aaa','bbb']) 2 aaa, bbb 25 # effect_row = cursor.executemany("insert into tb1(name) values(%s)",('aaa')) 3 a,a,a 26 # print(effect_row) 27 28 # 查数据 29 effect_row = cursor.execute('select name from tb1') 30 # 获取数据 查询数据后,execute返回的是受影响的行数,数据已经存储在游标中, 31 # 所以我们需要执行cursor.fetchall(),把游标中的所有数据拿出来 32 result = cursor.fetchall() 33 34 # 一次拿一个,相当于指针 next 35 # result = cursor.fetchone() 36 # 相当于执行三次fetchone() 37 # result = cursor.fetchmany(3) 38 39 # 游标在相对位置(当前位置)移动3个位置, absolute(绝对位置,0就是表里第一个数据) 40 # cursor.scroll(0,mode='absolute') 41 # result = cursor.fetchone() 42 print(result) 43 44 45 46 47 # 当表设置有自增时,可以通过cursor.lastrowid来获取最后数据的自增id 48 print(cursor.lastrowid) 49 50 51 # 提交,否则无法保存新建或已修改的数据 52 conn.commit() 53 54 # 关闭游标 55 cursor.close() 56 # 关闭连接 57 conn.close()
-
在pymsql中执行MySQL中的存储过程,获取返回值
1 # -*- coding utf-8 -*- 2 # coding=utf-8 3 4 import pymysql 5 """ 6 在pymsql中执行MySQL中的存储过程,获取返回值 7 """ 8 9 10 # 创建连接 创建一个通道; 主机,用户名,端口,用户名,密码,要连接的数据库名 11 conn = pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='yy123456',db='db2') 12 13 # 创建游标 帮我们存取数据,相当于一个手,去数据库里拿东西,设置为以字典形式 14 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 15 16 """ 17 MySQL中的 proc_3 存储过程 18 drop procedure if exists proc_3; 19 create procedure proc_3( 20 in i1 int, -- in 是传入参数 21 out i2 int, -- out 是返回参数 22 inout i3 int -- inout 既传入又返回 23 ) 24 begin 25 declare d2 int default 3; -- 定义要放在首行 26 set i3=i3+1; 27 28 select * from proc; 29 30 if i1 = 1 then 31 set i2 = 100+d2; 32 elseif i1 = 2 then 33 set i2 = 200+d2; 34 else 35 set i2 = 300+d2; 36 end if; 37 end 38 """ 39 40 # 执行存储过程 41 effect_row = cursor.callproc('proc_3',(1,2,3)) #第二个参数是out,所以这个2是不起作用的 42 # 存储过程的查询结果 43 result = cursor.fetchall() #此时并没有拿到返回值,只是拿到函数里面的查询函数 44 print(result) 45 46 # 获取存储过程的返回 47 # @_proc_3_0 :函数 proc_3 的第一个参数 48 effect_row = cursor.execute("select @_proc_3_0,@_proc_3_1,@_proc_3_2") 49 # 取得存储过程的返回值 50 result = cursor.fetchall() 51 print(result) 52 53 54 # 提交,否则无法保存新建或已修改的数据 55 conn.commit() 56 57 # 关闭游标 58 cursor.close() 59 # 关闭连接 60 conn.close()

浙公网安备 33010602011771号