pymysql

python+selenium之数据库连接

℡╲_俬逩灬. 提交于 2020-03-05 07:11:25
首先要安装Python和MySQL的连接工具 下载地址如下: https://pypi.python.org/pypi/PyMySQL https://github.com/PyMySQL/PyMySQL 脚本如下: #coding = utf-8 import pymysql import os #连接数据库 connection = pymysql.connect(host = 'localhost',user= 'root',password='123456',db='testonecard',port=3306,charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor) # 通过cursor创建游标 cursor = connection.cursor() # 创建sql 语句,并执行 sql = "update cert_card_consume SET real_payment_amount =300 where real_payment_amount =200" cursor.execute(sql) #提交sql connection.commit() 注: 无论使用什么的工具或库,都需要连接数据库, host为数据库的主机IP地址, port为MySQL的默认端口号, user为数据的用户名,

pymysql python 操作mysql

你。 提交于 2020-03-03 08:03:08
import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='work') cursor = conn.cursor()#创建游标 #增加单个 # r = cursor.execute("insert into class(caption)values (%s)",('BTCE1班',)) # print (r) #打印受影响的条数 # print (cursor.lastrowid ) #打印自增id # ---批量增加 # l = [("BTEC1班"),("BTEC2班"),("BTEC3班"),("BTEC4班"),] # r = cursor.executemany("insert into class(caption)values (%s)",l) # print (r) #打印受影响的条数 # new_id = cursor.lastrowid # print (new_id ) #打印自增id # over_id = new_id +r # k = []; # # #批量获取自增id的cid; # while over_id > new_id: # k.append(new_id) # new_id += 1; # print(k) #

手撸ORM

99封情书 提交于 2020-03-03 00:20:14
ORM即Object Relational Mapping,全称对象关系映射。 当我们需要对数据库进行操作时,势必需要通过连接数据、调用sql语句、执行sql语句等操作,ORM将数据库中的表,字段,行与我们面向对象编程的类及其方法,属性等一一对应,即将该部分操作封装起来,程序猿不需懂得sql语句即可完成对数据库 Django's ORM 优点: 1. 易用,学习曲线短 2. 和Django紧密集合,用Django时使用约定俗成的方法去操作数据库 缺点: 3. 不好处理复杂的查询,强制开发者回到原生SQL 4. 紧密和Django集成,使得在Django环境外很难使用 peewee 优点: 5. Django式的API,使其易用 6. 轻量实现,很容易和任意web框架集成 缺点: 7. 多对多查询写起来不直观 SQLAlchemy 优点: 8. 企业级 API,使得代码有健壮性和适应性 9. 灵活的设计,使得能轻松写复杂查询 缺点: 10. 重量级 API,导致长学习曲线 其它:SQLObject 、Storm 、、、、 ORM 池版 Db_pool from DBUtils.PooledDB import PooledDB import pymysql POOL = PooledDB( creator=pymysql, # 使用链接数据库的模块 maxconnections=6,

Python的MySQL驱动pymysql与mysqlclient性能对比

[亡魂溺海] 提交于 2020-03-02 03:51:01
Python版本3.6 测试语句: select * from FOO; mysql终端直接执行: 46410 rows in set (0.10 sec) python程序需安装profilehooks进行调用耗时分析 pymysql驱动测试程序: # 安装:pip install pymysql from profilehooks import profile import pymysql.cursors import pymysql connection = pymysql.connect(host='localhost', user='root', db='foo') c = connection.cursor() @profile(immediate=True) def read_by_pymysql(): c.execute("select * from FOO;") res = c.fetchall() read_by_pymysql() 分析结果:耗时2.4s,与原始mysql读取差一个数量级 mysqlclient驱动测试程序: # 安装:pip install mysqlclient 或 pip install git+https://github.com/PyMySQL/mysqlclient-python.git from profilehooks

python环境下使用mysql数据及数据结构和二叉树算法(图)

无人久伴 提交于 2020-03-02 00:04:53
python环境下使用mysql数据及数据结构和二叉树算法(图): 1 python环境下使用mysql 2使用的是 pymysql库 3 开始-->创建connection-->获取cursor-->操作-->关闭cursor->关闭connection->结束 4 5 代码框架 6 import pymysql.cursors 7 ###连接数据库 8 connection = pymysql.connect(host='127.0.0.1',port=3306,user='root', 9 password='...',db=DATABASE_NAME,charset='uft8mb4',cursorclass=pymysql.cursors.DictCursor) 10 ##创建游标 11 cursor = connection.cursor() 12 13 ##执行操作 14 sql='操作语句,mysql语法' 15 创建,删除,查询,添加,修改等等... 16 ##执行 17 cursor.execute(sql) 18 19 ##提交到数据库 20 connection.commit() 21 22 ##关闭连接 23 cursor.close() 24 connection.close() 25 ########操作语句 26 插入数据与mysql一样 27

Python数据库连接池DBUtils

可紊 提交于 2020-03-01 12:35:30
DBUtils简介 DBUtils是Python的一个用于实现数据库连接池的模块。 此连接池有两种连接模式: 模式一:为每个线程创建一个连接,线程即使调用了close方法,也不会关闭,只是把连接重新放到连接池,供自己线程再次使用。当线程终止时,连接自动关闭。 POOL = PersistentDB( creator=pymysql, # 使用链接数据库的模块 maxusage=None, # 一个链接最多被重复使用的次数,None表示无限制 setsession=[], # 开始会话前执行的命令列表。如:["set datestyle to ...", "set time zone ..."] ping=0, # ping MySQL服务端,检查是否服务可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always closeable=False, # 如果为False时, conn.close() 实际上被忽略,供下次使用,再线程关闭时,才会自动关闭链接。如果为True时, conn.close()则关闭链接,那么再次调用pool.connection时就会报错

python连接Mysql数据库

こ雲淡風輕ζ 提交于 2020-03-01 06:43:27
(1)首先需要安装pymysql库,命令行执行以下语句即可: pip install pymysql (2)创建表 import pymysql """ 1、连接本地数据库 2、建立游标 3、创建表 4、插入表数据、查询表数据、更新表数据、删除表数据 """ def create_table ( ) : #连接本地数据库 db = pymysql . connect ( host = 'localhost' , #数据库IP地址 port = 3306 , #数据库连接端口,默认是2206 user = 'root' , #数据库用户名 passwd = '123456' , #数据库用户密码 db = 'test_db' , #数据库名称 charset = 'utf8' ) #创建游标 cursor = db . cursor ( ) #新建一张student表,如果存在student表,则删除 cursor . execute ( "DROP TABLE IF EXISTS student" ) #创建student表 sql = """ create table student( id int not null, name char(10), age int, address char(20), create_time datetime) """ try : #

python3,pymysql操作数据库

橙三吉。 提交于 2020-02-29 10:42:40
1.pymysql和mysqldb主要区别:mysqldb只支持python2,还不支持3,所以在python3中用pymysql代替 2.使用python脚本实现增删改查和事物处理: import sysimport pymysql class dataBase(): global TABLENAME def connectDB(self,sql): try:       #连接数据库   conn=pymysql.connect(host="10.20.89.15",port=3306,user="root",password="123456", db="cs-sincerity",charset="utf8")       #获取游标 curs=conn.cursor(pymysql.cursors.DictCursor)       #执行sql curs.execute(sql)       alldatas=curs.fetchall()    if sql.find('insert')>-1: alldatas=conn.insert_id() conn.commit() curs.close() conn.close() return alldatasexcept: print("Can't connect database") sys.exit()     def

python3操作PyMySQL笔记

巧了我就是萌 提交于 2020-02-29 10:42:20
python3操作mysql需要先安装PyMySQL pip install PyMySQL 在linux登录mysql ,并且在安装数据库时设置了数据库的用户名“root”和密码“root”,mysql安装的版本为mysql5.7 [root@web ~]# mysql -uroot -p -h192.168.10.100 在mysql里面创建一个mysql库 mysql> create database mrsoft; Query OK, 1 row affected (0.00 sec) 下面是新建一个py文件远程创建一个mysql连接,下面通过connect()方法连接MySQL数据库mrsoft,具体代码如下: import pymysql # 打开数据库连接, 参数1:主机名或者ip;参数2:用户名;参数3:密码;参数4:数据库名称;参数5:用utf8格式打开数据库表防止出现中文乱码 db = pymysql.connect("192.168.10.100", "root", "root", "mrsoft", charset="utf8") # 使用cursor()方法创建一个游标对象cursor cursor = db.cursor() # 使用execute()方法执行SQL查询 cursor.execute("SELECT VERSION()") #