pymysql

数据库链接池

风流意气都作罢 提交于 2019-12-05 07:33:22
什么是数据库链接池?以及作用? ''' 数据库链接池的基本原理:为数据库建立一个缓冲池,预先在池中放入一定数量的数据库链接管道,需要时,从链接池中取出管道进行使用,操作完毕后,再将链接放回到池子中,从而避 免了频繁的链接数据库,资源的申请和释放的性能损耗 由于数据库链接得到重用,避免了频翻创建,释放链接引起的大量性能开销,在减少系统消耗的基础上,增进了系统环境的平稳性,更快的系统响应速度 ''' 数据库链接池函数多线程版 from DBUtils.PooledDB import PooledDB from concurrent.futures import ThreadPoolExecutor POOL = PooledDB( creator=pymysql, # 使用链接数据库的模块 maxconnections=6, # 连接池允许的最大连接数,0和None表示不限制连接数 mincached=2, # 初始化时,链接池中至少创建的链接,0表示不创建 blocking=True, #连接池中如果没有可用连接后, #是否阻塞等待。True,等待;False,不等待然后报错 ping=0, # ping MySQL服务端,检查是否服务可用。 # 如:0 = None = never, 1 = default = whenever it is requested, # 2 =

django在pycharm设置数据库

喜欢而已 提交于 2019-12-05 07:11:04
数据库操作 orm object relational mapping 对象关系映射 使用: 第一步: 在应用文件夹下面的models.py文件中写对应的类,看下面的示例: class UserInfo(models.Model): id = models.AutoField(primary_key=True) username = models.CharField(max_length=10) password = models.CharField(max_length=32) 到mysql数据库中创建一个库,比如名为orm01,create database orm01; 第二步:做数据库配置,settings.py文件中写上以下配置 # DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), # } # } 连接mysql的配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME':'orm01', 'HOST':'127.0.0.1', 'PORT':3306, 'USER':'root',

Error while using pymysql in flask

荒凉一梦 提交于 2019-12-05 06:42:24
I am using pymysql client to connect to mysql in my flask API, everything works fine for some days(around 1-2 days) after that suddenly it starts to throw this error Traceback (most recent call last): File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 1039, in _write_bytes self._sock.sendall(data) TimeoutError: [Errno 110] Connection timed out During handling of the above exception, another exception occurred: Traceback (most recent call last): File "Main.py", line 194, in post result={'resultCode':100,'resultDescription':'SUCCESS','result':self.getStudentATData

py

空扰寡人 提交于 2019-12-05 05:28:49
大神 # -*- coding: utf-8 -*- import requests from bs4 import BeautifulSoup import lxml import json import re import time import tushare as ts import pandas as pd import pymysql class Mysqldb(): def __init__(self, conn, sql): self.conn = conn self.cursor = cursor self.sql = sql def execute(self, sql): try: self.cursor.execute(self.sql) except: self.dbname.rollback() print("SQL执行失败,数据已回滚") def commit(self): self.conn.commit() def close(self): self.cursor.close() self.conn.close() def Tstockbasic(): # 调用stock_basic,获取 stkbasic = pro.stock_basic(list_status='L', fields='ts_code,symbol,name,industry

python常用模块

☆樱花仙子☆ 提交于 2019-12-05 05:24:01
random #用于生成随机数 In [1]: import random #用于生成随机数 In [3]: random.random()#随意生成 Out[3]: 0.5923203537141584 In [4]: random.uniform(1,2)#制定区间 Out[4]: 1.0999623761862738 In [8]: random.randint(1,3)#生成整数 Out[8]: 1 In [9]: random.randrange(10,100,2)#制定步长 Out[9]: 12 In [12]: random.choice([1,2,3,4,5])#序列中随机选择一个.一定要有序数列 Out[12]: 5 In [16]: p=[1,2,3,4,5] random.shuffle(p)#将一个列表中的元素打乱 p Out[16]: [4, 1, 2, 5, 3] time #用于处理时间 In [17]: import time #用于处理时间 In [18]: time.time()#返回当前时间的时间戳(1970年后的浮点秒数) Out[18]: 1572419713.7745376 In [19]: time.localtime()#返回本地的当前时间 Out[19]: time.struct_time(tm_year=2019, tm_mon

django查看数据库

别来无恙 提交于 2019-12-05 05:04:10
#views import pymysql def get_date(request): conn = pymysql.connect( host='localhost', port=3306, user='root', passwd='mysql', db='homework2', charset='utf8', autocommit=True ) cursor = conn.cursor(pymysql.cursors.DictCursor) # sql = "select * from ?" # sql = sql.replace('%s','?') sql = "select * from teacher_info" # value = "teacher_info" # cursor.execute(sql,value) cursor.execute(sql) user_list = cursor.fetchall() with open(r'templates/get_data.html','r',encoding='utf8') as fr: data = fr.read() return render(request,'get_data.html',{'user_list':user_list}) #html <!DOCTYPE html> <html lang=

安装pymysql模块及使用

僤鯓⒐⒋嵵緔 提交于 2019-12-05 04:27:11
安装pymysql模块: https://www.cnblogs.com/Eva-J/articles/9772614.html file--settings for New Projects---Project Interpreter----+---pymysql安装就好。 若忘记函数用法,鼠标放在内建函数上,Ctrl+B,看源码 pymysql常见报错: ​ https://www.cnblogs.com/HByang/p/9640668.html 在cmd中mysql中建homework库中表student: 创建表: create table student ( stuid int not null, stuname varchar(4) not null, stusex bit default 1, stuaddr varchar(50), colid int not null comment '学院编号', primary key (stuid) ); 插入数据: insert into tb_student values (1001,'小强',1,'四川成都',30), (1002,'花月',1,'四川成都',10), (1003,'小红',1,'四川成都',20), (1004,'小白',1,'四川成都',10), (1005,'小青',1,'四川成都',30),

Python3:模块:ModuleNotFoundError No module named 'MySQLdb'

房东的猫 提交于 2019-12-05 04:23:45
Python3:模块:ModuleNotFoundError No module named 'MySQLdb' Centos: yum install mysql-devel gcc gcc-devel python-devel pip3 install pymysql mysqlclient Mac: brew install mySQL brew install openssl export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/ pip3 install pymysql mysqlclient 来源: https://www.cnblogs.com/stone1989/p/11905512.html

pymysql 1064, 'You have an error in your SQL syntax; check the manual that corresponds to

放肆的年华 提交于 2019-12-05 01:59:15
在python 连接mysql时,最近一直出现了 1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'"58 convenience stores in Beijing Pilot selling class B over-the-counter drugs""\' at line 4'这个异常信息,今早发现后就进行解决,尝试了多种方式,google提示用pymysql.escape_string函数对参数防止转义,最开始用的是{0} 这种占位符的方式,发现还是无法解决,后面还试了一种方式,在execute里面处理 sql_01 = ''' insert into cmstop_content(contentid,catid,modelid,title, tags,status,published,publishedby ,createdby,created,modifiedby,modified,sourceid,score) values (null,%s,"1",%s,%s,'3',UNIX_TIMESTAMP(SYSDATE()),'28',

python3.7 django2.2 mysql 异常

混江龙づ霸主 提交于 2019-12-05 01:51:30
错误日志 mysqlclient 1.3.13 or newer is required; File "/Users/huoyinghui/workspaces/drf3/lib/python3.7/site-packages/django/db/utils.py", line 201, in __getitem__ backend = load_backend(db['ENGINE']) File "/Users/huoyinghui/workspaces/drf3/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend return import_module('%s.base' % backend_name) File "/Users/huoyinghui/workspaces/drf3/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/Users/huoyinghui/workspaces/drf3/lib/python3.7/site-packages/django