pymysql

python插入mysql数据(2)

六月ゝ 毕业季﹏ 提交于 2019-12-03 20:09:41
python插入mysql数据(2) """插入操作""" import pymysql import datetime from pymysql import cursors # import pymysql.cursors # 连接数据库 connect = pymysql.Connect( host='10.10.146.28', port=3306, user='admin_m', passwd='fcfmTbRw1tz2x5L5GvjJ', db='test', charset='utf8mb4' ) def insert_record(): cursors = connect.cursor() sql = """ insert into employee(first_name, last_name,age,sex,income) values('xiao', 'zhi', 22, 1, 45000) """ try: cursors.execute(sql) print("插入数据成功") except Exception as e: print('插入数据失败') finally: cursors.close() def main(): insert_record() if __name__ == "__main__": main() 来源: https://www

Python - MySQL 数据库连接 - PyMySQL 驱动 - 第二十五天

情到浓时终转凉″ 提交于 2019-12-03 17:22:52
序言 本文我们为大家介绍 Python3 使用 PyMySQL 连接数据库,并实现简单的增删改查。 什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。 PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。 PyMySQL 安装 在使用 PyMySQL 之前,我们需要确保 PyMySQL 已安装。 PyMySQL 下载地址:https://github.com/PyMySQL/PyMySQL。 如果还未安装,我们可以使用以下命令安装最新版的 PyMySQL: pip install pymysql 数据库连接 连接数据库前,请先确认以下事项: 您已经创建了数据库 TESTDB. 在TESTDB数据库中您已经创建了表 EMPLOYEE EMPLOYEE表字段为 FIRST_NAME, LAST_NAME, AGE, SEX 和 INCOME。 连接数据库TESTDB使用的用户名为 "testuser" ,密码为 "test123",你也可以自己设定或者直接使用root用户名及其密码,Mysql数据库用户授权请使用Grant命令。 在你的机子上已经安装了 Python MySQLdb 模块。 实例: import

Python mysql (using pymysql) auto reconnect

限于喜欢 提交于 2019-12-03 16:52:12
问题 I'm not sure if this is possible, but I'm looking for a way to reconnect to mysql database when the connection is lost. All the connections are held in a gevent queue but that shouldn't matter I think. I'm sure if I put some time in, I can come up with a way to reconnect to the database. However I was glancing pymysql code and I saw that there is a 'ping' method in Connection class, which I'm not sure exactly how to use. The method looks like it will reconnect first time but after that it

pymysql分页

巧了我就是萌 提交于 2019-12-03 15:00:06
def sqlexec(last_nid, is_next): import pymysql conn = pymysql.connect(host='192.168.12.29', port=3306, user='root', passwd='123', db='IndexDB', charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 执行存储过程,获取存储过程的结果集,将返回值设置给了 @_存储过程名_序号 = if is_next: cursor.execute('select * from tb1 where nid>%s limit 10',last_nid) result = cursor.fetchall() else: cursor.execute('select * from tb1 where nid<%s order by nid desc limit 10', last_nid) result = cursor.fetchall() result = list(reversed(result)) conn.commit() cursor.close() conn.close() return result current_last_nid = 0 current_

pymysql存储过程代码

南笙酒味 提交于 2019-12-03 14:54:34
import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='666', db='sqlexample',charset='utf-8') cursor = conn.cursor(cursor=pymsql.cursor.DictCursor) #执行存储过程,获取存储过程的结果集,将返回值设置给了@_存储过程名_序号 = r1 = cursor.callproc('p11', args=(1, 22, 3, 4)) # set @_p11_0 = 1 # set @_pll_1 = 22 # set @_p11_2 = 3 # set @_p11_3 = 4 # call p11(1, 22, 3, 4) print(r1) result1 = cursor.fetchall() print(result1) #获取执行完整存储的参数 r2 = cursor.execute("select @_p11_0,@_p11_1,@_p11_2,@_p11_3") print(r2) result2 = cursor.fetchall() print(result2) conn.commit() cursor.close() conn.close() 来源: https

day36

a 夏天 提交于 2019-12-03 14:18:49
一、python操作mysql pymysql的安装 pip install pymysql sql注入问题 产生的原因 因为过于相信用户输入的内容,根本没有做任何的检验 解决的方法 sql = "select * from user where 字段1=%s and 字段2=%s" cursor.execute(sql, (值1, 值2)) 连接 连接数据库的参数 conn=pymysql.connect(host='localhost',user='root',password='123qwe',database='test',charset='utf8') cursor = conn.cursor() ### 默认返回的值是元祖类型 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) ### 返回的值是字典类型 (*********) 增 设定sql语句 sql = sql增加数据语句 设定要加入的数据 data = [(),(),()...] 执行到数据库中 cursor.executemany(sql,data) 提交 conn.commit() 查 fetchall() : 取出所有的数据 返回的是列表套字典 fetchone() : 取出一条数据 返回的是字典 fetchmany(size) :

day44

爷,独闯天下 提交于 2019-12-03 11:44:57
目录 pymysql操作mysql 安装: 使用的时候导入: 连接数据库的参数: pymysql的sql注入 sql注入问题 产生的原因: 解决的方法: 连接: 查: 增: 修: 删除: 索引 为什么实用索引以及索引的作用: 类比: 索引的本质: 索引的底层原理: 索引的种类:(********************) 主键索引: 唯一索引: 普通索引: 索引的创建: 主键索引: 唯一索引: 普通索引: 索引的优缺点: 慢查询日志: pymysql操作mysql 安装: pip install pymysql 使用的时候导入: import pymysql 连接数据库的参数: import pymysql conn = pymysql.connect(host='localhost',user='root',password='123',database='test',charset='utf8') # cursor = conn.cursor() 默认返回的值是元组类型 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 返回的值是字典类型(********) sql = "select * from userinfo" cursor.execute(sql) # res = cursor.fetchall() #

day36 学习小结

半世苍凉 提交于 2019-12-03 10:10:25
目录 一、python操作mysql 1. pymysql的安装 2. sql注入问题 3. 连接 4. 增 5. 查 6. 改 7. 删 二、索引 1. 为啥使用索引以及索引的作用 2. 索引的本质 3. 索引的底层原理 4. 索引的种类 5. 索引的创建与删除 5.1 创建主键索引 5.2 删除主键索引 5.3 创建唯一索引 5.4 删除唯一索引 5.5 创建普通索引 5.6 删除普通索引 6. 索引的优缺点 7. 不会命中索引的情况 三、慢查询日志 1. 查看慢sql的相关变量 2. 配置慢sql的变量 一、python操作mysql 1. pymysql的安装 pip install pymysql 2. sql注入问题 产生的原因 因为过于相信用户输入的内容,根本没有做任何的检验 解决的方法 sql = "select * from user where 字段1=%s and 字段2=%s" cursor.execute(sql, (值1, 值2)) 3. 连接 ### 连接数据库的参数 conn=pymysql.connect(host='localhost',user='root',password='123qwe',database='test',charset='utf8') # cursor = conn.cursor() ### 默认返回的值是元祖类型

mysql总复习

喜你入骨 提交于 2019-12-03 10:08:00
目录 数据库操作 库操作 表操作 数据行操作 表关系操作 单表操作 外键创建 多表联查 pymysql模块 索引 主键索引 唯一索引 普通索引 数据库操作 库操作 create database 库名 charset utf8; //创建表 show databases; //查看所有库 show create database 库名; // 查看创建库的语句 select database(); //查看当前的数据库 use 库名; //使用数据库 drop database 库名; //删除库 alter database 库名 charset utf8; //更改库字符编码 表操作 // 创建表 create table t1 ( id int primary key, name varchar(32) ) charset utf8; // 修改表名 alter table t1 rename t11; // 添加字段 alter table 表名 add 字段名 列类型 ; alter table 表名 add 字段名 列类型 first; alter table 表名 add 字段名 列类型 after 字段; // 修改字段名 alter table 表名 change 旧字段名 新字段名 数据类型; //修改字段属性 alter table 表名 modify 字段名

python使用pymysql操作mysql数据库

时光总嘲笑我的痴心妄想 提交于 2019-12-03 08:08:12
1、安装pymysql pip install pymysql 2、数据库查询示例 import pymysql # 连接database conn =pymysql.connect(user='root', password='1234' ,host='127.0.0.1',database='xinfang') #创建游标 cursor = conn.cursor() #执行函数 返回受影响的函数 effect_rows = cursor.execute('select * from t_base_letters where MemberNum>%s and SubjectTno1=%s',[1,'030000']) print("受影响的行数",effect_rows) #提取所有结果 results = cursor.fetchall() for row in results: id = row[0] SLID = row[1] SubjectType = row[4] # 打印结果 print("id=%s,SLID=%s,SubjectType=%s" % (id, SLID, SubjectType)) #关闭游标 cursor.close() #关闭连接 conn.close() 3、数据增删改示例   import pymysql # 连接database