pymysql

Flask--数据库连接池

混江龙づ霸主 提交于 2019-12-03 07:41:41
目录 数据库连接池 pymsql链接数据库 数据库连接池版 数据库连接池 pymsql链接数据库 import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='s8day127db') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # cursor.execute("select id,name from users where name=%s and pwd=%s",['lqz','123',]) cursor.execute("select id,name from users where name=%(user)s and pwd=%(pwd)s",{'user':'lqz','pwd':'123'}) obj = cursor.fetchone() conn.commit() cursor.close() conn.close() print(obj) 数据库连接池版 方式一 为每个线程创建一个连接,线程即使调用了close方法,也不会关闭,只是把连接重新放到连接池,供自己线程再次使用。当线程终止时,连接自动关闭 from DBUtils.PersistentDB

1031 笔记

拟墨画扇 提交于 2019-12-03 07:32:51
目录 pymysql 1.基本操作 1.新建表 2.增加数据 3.删除数据 4.更改数据 5.查找数据 6.断开连接 2.sql注入 解决方法 3.创建大数据库 索引 定义 作用 实现原理 B+树的性质 B+树的分类 InnoDB存储引擎表示索引组织表,即表中数据按照主键顺序存放。而聚集索引(clustered index)就是按照每张表的主键构造一棵B+树,同时叶子结点存放的即为整张表的行记录数据,也将聚集索引的叶子结点称为数据页。聚集索引的这个特性决定了索引组织表中数据也是索引的一部分。同B+树数据结构一样,每个数据页都通过一个双向链表来进行链接。 如果未定义主键,MySQL取第一个唯一索引(unique)而且只含非空列(NOT NULL)作为主键,InnoDB使用它作为聚簇索引。 如果没有这样的列,InnoDB就自己产生一个这样的ID值,它有六个字节,而且是隐藏的,使其作为聚簇索引。 由于实际的数据页只能按照一棵B+树进行排序,因此每张表只能拥有一个聚集索引。在多少情况下,查询优化器倾向于采用聚集索引。因为聚集索引能够在B+树索引的叶子节点上直接找到数据。此外由于定义了数据的逻辑顺序,聚集索引能够特别快地访问针对范围值得查询。 索引的种类 主键索引 primary key 唯一索引 unique(name) 唯一联合索引 uniqe (name, email) 普通索引

数据存储之MySQL

扶醉桌前 提交于 2019-12-03 07:04:23
包的导入: @python3 import pymysql @python2 import MySQLdb 基本应用 import pymysql #建立连接 db = pymysql.connect(host=‘localhost’, port=3306, db=‘test’, user=‘root’, password=‘root’) #使用cursor()方法 新建游标对象 cursor = db.cursor() #使用execute()方法执行SQL语句 cursor.execute(“SELECT * FROM teacher”) #使用fetchall()获取全部数据 data = cursor.fetchall() #打印获取的的数据 print(data) #关闭游标和数据库的连接 cursor.close() 详解 pymysql.connect中可接受的参数: host:主机名或地址 user:用户名 password:密码 简写passwd database:指定数据库 简写db port:端口,默认3306 charset:指定字符编码 **kw:关键字参数,用于字典.(*args是非关键字参数,用于元组,必须要在**kwargs前) cursor其实是调用了cursors模块下的Cursor的类,这个模块主要的作用就是用来和数据库交互的

Python mysql (using pymysql) auto reconnect

落花浮王杯 提交于 2019-12-03 06:47:29
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 switched the reconnect flag to False again? Can I use this method, or is there a different way to

python爬取动态数据实战---猫眼专业版-实时票房(二)

怎甘沉沦 提交于 2019-12-03 04:38:47
学习python进行简单的数据爬取(基于python 3.x)。再进行数据页面解析之后,使用scrapy框架进行爬取数据。没有实现自己预想的效果,着实是自己能力有限,无法灵活使用该框架。就使用自己的办法进行数据爬取。需要用到的模块有 urllib,json,pymysql,datatime,os 首先定义一个类:class MovieSpider(object): 在外部调用类的方法 if __name__ == "__main__": #数据目标地址 DataUrl = "http://piaofang.meituan.com/second-box" #定义类对象 Spider = MovieSpider() #调用类方法 Spider.DownloadData(DataUrl) 定义下载数据的方法: def DownloadData(self, url): self.GetData(url) 打开数据页面,在这里使用urllib模块进行访问打开页面,返回html页面,内容是json格式进行编辑 def OpenPage(self, url): """打开数据页面""" rep = urllib.request.Request(url) response = urllib.request.urlopen(rep) #读取页面数据信息 html = response.read()

Django之orm

血红的双手。 提交于 2019-12-03 04:31:53
一、Django静态文件配置 ##settings.py## 静态文件配置: STATIC_URL = '/static/' # 静态文件配置 STATICFILES_DIRS = [ os.path.join(BASE_DIR,'static') ] # 暴露给外界能够访问服务器静态文件夹下面所有的资源 STATIC_URL = '/xxx/' # 接口前缀 跟你的静态文件夹的名字一点关系都没有 # 默认情况下这个前缀跟静态文件夹名字一样!!! # 静态文件配置 STATICFILES_DIRS = [ os.path.join(BASE_DIR,'static'), # 就是你的静态文件夹路径 os.path.join(BASE_DIR,'static1'), os.path.join(BASE_DIR,'static2') ] # ps:会依次查找列表中所有的静态文件路径 找到的话立刻停止,都没有找到返回404 静态文件目录结构: 项目目录-->static-->{css, js, images, bootstrap-3.3.7} <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script

索引

泄露秘密 提交于 2019-12-03 04:29:08
python 操作 pymysql : ​ 安装 : pip install pymysql ​ pymysql : ​ -sql 注入问题 : 安全问题 产生原因 : 用户输入特殊字符 ,没有对用户输入做校验 import pymysql #连接数据库 conn = pymysql.connect( host = 'localhost',user ='root', password = '155',database = 'db1',charset = 'utf-8') cursor = conn.cursor() # 默认返回元组类型数据 user = input('enter to username: ').strip() pwd = input ('enter to passsword: ').strip() #输入sql指令 : sql = "from db1 where name = '%s' and password = '%s' " %(user,pwd) print(sql) #交互 res = cursor.execute(sql) print(res) cursor.close() #关闭连接 conn.close() if res: print('login sucessful') else: print('login bad') sql 注入问题 : 1

Using python to write mysql query to csv, need to show field names

回眸只為那壹抹淺笑 提交于 2019-12-03 03:04:59
问题 I have the following: import MySQLdb as dbapi import sys import csv dbServer='localhost' dbPass='supersecretpassword' dbSchema='dbTest' dbUser='root' dbQuery='SELECT * FROM pbTest.Orders;' db=dbapi.connect(host=dbServer,user=dbUser,passwd=dbPass) cur=db.cursor() cur.execute(dbQuery) result=cur.fetchall() c = csv.writer(open("temp.csv","wb")) c.writerow(result) This produces a garbled mess. I am familiar with using printing record[0] etc. Not sure how I should be going about setting up the

No module named &#039;pymysql&#039;

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to use PyMySQL on Ubuntu. I've installed pymysql using both pip and pip3 but every time I use import pymysql , it returns ImportError: No module named 'pymysql' I'm using Ubuntu 15.10 64-bit and Python 3.5. The same .py works on Windows with Python 3.5, but not on Ubuntu. 回答1: Sort of already answered this in the comments, but just so this question has an answer, the problem was resolved through running: sudo apt-get install python3-pymysql 回答2: If even sudo apt-get install python3-pymysql does not work for you try this: Go to the

pymysql操作数据库与索引

一曲冷凌霜 提交于 2019-12-03 02:59:43
目录 pymysql操作数据库 简单操作 sql的注入问题 sql注入问题解决办法 sql注入问题模板总结 利用pymysql操作数据库 (增删改),conn.commit() 索引 1.为何要有索引 2.什么是索引? 3.索引使用的优缺点  4.索引原理 5.索引的种类 索引的创建 主键索引 唯一索引  普通索引 pymysql操作数据库 简单操作 import pymysql # pip install pymysql # 连接数据库的参数 conn = pymysql.connect(host= 'localhost', user='root', password = '123', database='db3', charset = 'utf8') # cursor = conn.cursor() # <pymysql.cursors.Cursor object at 0x000000000A0E2C50>,游标对象,默认返回的值是元祖类型 cursor = conn.cursor(cursor = pymysql.cursors.DictCursor) # <pymysql.cursors.DictCursor object at 0x000000000A0E2C88> 返回的值是字典类型 # print(cursor) sql = "select name from