pymysql

【Python数据库连接池基本用法】

有些话、适合烂在心里 提交于 2019-11-28 10:15:17
目录 基本用法 自制sqlhelper 原文: http://blog.gqylpy.com/gqy/346 @(Python数据库连接池) 确保已安装: pip install DBUtils *** 基本用法 先准备些数据 # 建了个表 create table userinfo( id int, name varchar(32), age int(3) ); # 插入记录 insert into userinfo values (1, 'user01', 21), (2, 'user02', 22), (3, 'user03', 23), (4, 'user04', 24); 创建使用数据库连接池 import pymysql from DBUtils.PooledDB import PooledDB, SharedDBConnection POOL = PooledDB( creator=pymysql, # 使用连接数据库的膜拜 maxconnections=6, # 连接池允许的最大连接数,0和None表示不限制连接数 mincached=2, # 初始化时,连接池中至少创建的空闲的连接,0表示不创建 maxcached=5, # 连接池中最多闲置的连接,0和None表示不限制 maxshared=3, # 连接池中最多共享的连接数量,0和None表示全部共享

pymkysql 的操作

不打扰是莪最后的温柔 提交于 2019-11-28 08:37:32
# 1.安装:pip3 insatll pymysql # 2.代码链接 import pymysql #链接 conn=pymysql.connect( host='localhost', user='root', password='123', database='egon', charset='utf8') #游标 cursor=conn.cursor() #执行完毕返回的结果集默认以元组显示 #cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) # 以字典的方式显示数据 # 3.pymysql操作数据库 #执行sql语句 user = input(">>>:").strip() pwd = input(">>>:").strip() sql='select * from userinfo where name="%s" and password="%s"' %(user,pwd) #注意%s需要加引号 rows=cursor.execute(sql) #执行sql语句,返回sql查询成功的记录数目 # 获取真实数据cursor.fetchone(),cursor.fetchall(),cursor.fetchmany(),类似管道取值,获取一条,所有,多条 cursor.scroll(1,'relative') #

第三十九天

删除回忆录丶 提交于 2019-11-28 08:36:33
视图 什么是视图?   一个查询语句的结果是一张虚拟表,将这种虚拟表保存下来,它就变成了一个视图 为什么要用视图? 当频繁需要用到多张表的连表结果,你就可以事先生成号视图之后直接调动即可,避免了反复写连表操作的sql语句 如何使用? create view teacher_course as select * from teacher INNER JOIN course on teacher.tid = course.teacher_id; 1.视图只有表结构,视图中的数据还是来源于原来的表2.不要改动视图表中的数据3.一般情况下不会频繁的使用视图来写业务逻辑 触发器 到达某个条件时自动触发 当你在对数据进行增、删、改的情况下会自动触发触发器的运行 触发器分为六种情况 固定语法结构 create trigger 触发器的名字 after/before insert/update/delete on 表名 for each row begin sql语句 end # 详细版本 create trigger tri_before/after_insert/update/delete_t1 after/before insert/update/delete on t1 for each row begin sql语句 end 增前   可以修改MySQL默认的结束符(;)

第三十八天

心不动则不痛 提交于 2019-11-28 08:36:21
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') #

学习笔记day38

一世执手 提交于 2019-11-28 08:23:10
python操作MySQL #首先要导入模块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语句 sql = 'select * from teacher' #执行sql语句 cursor.execute(sql) # 执行传入的sql语句 # print(res) # res是执行语句返回的数据条数 #获取结果 print(cursor.fetchone()) # 只获取一条数据 print(cursor.fetchone()) # 只获取一条数据 print(cursor.fetchone()) # 只获取一条数据 # cursor.scroll(2,'absolute') # 控制光标移动 absolute相对于其实位置 往后移动几位

【Python pymysql】

百般思念 提交于 2019-11-28 08:02:58
原文: http://blog.gqylpy.com/gqy/257 目录 关于sql注入 用户存在,绕过密码 用户不存在,绕过用户与密码 解决sql注入问题 commit() 增 改 删 查询数据库 fetchone() fetchall() fetchmany() > 补充: > > > 建立链接时间过长后会自动断开链接,可像下面这样解决: > ```python > conn.ping(reconnect=True) > ``` > 检查链接是否还存在,参数`reconnect=True` 表示如果链接已不存在,则重新建立链接 > > 补充: > # 回滚,通常用于事务conn.rollback() pymysql模块用于在Python程序中操作数据库. 该模块本质是一个套接字客户端软件. Windows安装命令:pip3 install pymysql 基本使用: # 准备数据库、数据和远程用户: mysql> select * from blog.userinfo;+----+------+-----+| id | name | pwd |+----+------+-----+| 1 | zyk | ___ |+----+------+-----+1 row in set (0.00 sec) mysql> show grants for 'zyk'@'%';+-----

Python3数据库封装

霸气de小男生 提交于 2019-11-28 07:44:52
# coding=utf-8__author__ = "leslie"import pymysql,pymysql.errfrom CaseApi.common.readConfig import readfrom CaseApi.common.caseLog import Logclass MysqlDb: def __init__(self): self.log = Log() # 实例化日志类 def __db(self):     '''定义私有方法连接数据库''' host = read('mysql','host') user = read('mysql','user') password = read('mysql','password') database = read('mysql','db') try: self.db = pymysql.connect(host,user,password,database) return self.db except Exception as e: self.log.error ("链接出错: %s"%e) # self.__db = pymysql.connect(host, user, password, database) # return self.__db def select(self,form,table

Cannot make remote connection with PyMySQL (pymysql.err.InternalError: Packet sequence number wrong)

放肆的年华 提交于 2019-11-28 06:45:00
问题 update: Problem solved, solution posted below I am new to the process of making remote database connections, but it seems that there tends not to be an obvious solution for this error. pymysql.err.InternalError: Packet sequence number wrong - got 80 expected 0 arises when attempting to make the following pymysql connection I'm running MacOS 10.12.5, Python 2.7.10 in PyCharm (also tried with Terminal), and PyMySQL 0.7.11 (also tried 0.7.9) update : also tried on Windows 10, Python 2.7.13 with

pycharm操作数据库

半城伤御伤魂 提交于 2019-11-28 06:34:41
一、pycharm查询数据库 1.下载pymysql模块 2.查询数据库 # 模块: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相对于起始位置

pymysql模块初见

元气小坏坏 提交于 2019-11-28 06:33:59
一.pymysql的基本使用方法 import pymysql db = pymysql.connect( host = '127.0.0.1', port = 3306, user = 'root', password = '123', database = 'day36', charset = 'utf8', # 写成了utf-8会直接报错 autocommit = True # 这个参数配置完成后 增删改操作都不需要在手动加conn.commit了 ) # cursor = db.cursor() # 产生一个游标对象 以元组的形式进行返回 cursor = db.cursor(pymysql.cursors.DictCursor) # 产生一个游标对象 以字典的形式返回查询出来的数据 键是表的字段 值是表的字段对应的信息 sql = 'select * from emp' res = cursor.execute(sql) # 使用 execute() 方法执行 SQL,返回值是查询到的数据条数 print(res) if res: # print(cursor.fetchone()) # 获取一条数据,返回值字典 # cursor.scroll(0,'absolute') #控制光标移动,相对于起始位置,往后移动多少 # cursor.scroll(1,'relative