pymysql

pymysql DAO简单封装

匿名 (未验证) 提交于 2019-12-02 22:06:11
#!/usr/bin/env python # -*-coding:utf-8 -*- # # 无法执行多个query,self.conn.close()放在CdbConn类的单独函数中,每次query之后要手动close;否则多次query,会自动关闭 import pymysql class CdbConn (): def __init__ ( self , db_host , db_user , db_pwd , db_name , db_port = 3306 ): self . db_host = db_host self . db_port = db_port self . db_user = db_user self . db_pwd = db_pwd self . db_name = db_name self . status = True self . conn = self . getConnection () def getConnection ( self ): try : conn = pymysql . Connect ( host = self . db_host , # 设置MYSQL地址 port = int ( self . db_port ), # 设置端口号 user = self . db_user , # 设置用户名 passwd =

pymysql模块操作数据库与mysql数据备份

匿名 (未验证) 提交于 2019-12-02 22:06:11
host = '127.0.0.1' #计算机的IP地址 port = 3306 #数据库的端口号 user = 'root' #数据库的用户名 password = 'root' #用户密码 database = 'lucky' #要连接数据库名 charset = 'utf8' #链接数据的编码格式 import pymysql 1 :#查询 conn = pymysql . connect ( host = '127.0.0.1' , #主机 port = 3306 , #端口号 user = 'root' ,#用户名 password = '666' , #密码 database = 'day43' , #需要连接的库 charset = 'utf8' #指定编码 ) cursor = conn . cursor ()#获取游标,默认获取的是元祖嵌套形式的数据 sql = "select * from dep;" #sql语句 ret = cursor . execute ( sql ) #ret 受影响的行数 print ( cursor . fetchall ()) #取出所有的 print ( cursor . fetchmany ( 3 )) #取出多条不写参数默认是取一条 print ( cursor . fetchone ()) #取出单条 cursor .

MySQL的游标

匿名 (未验证) 提交于 2019-12-02 22:06:11
python操作mysql数据库,主要就是通过pymysql模块 pip install pymysql 1)建立数据库连接对象 conn 2)通过 conn 创建操作sql的 游标对象 3)编写sql交给 cursor 执行 4)如果是查询,通过 cursor对象 获取结果 5)操作完毕,端口操作与连接 注意 1. 对记录增删改默认需要 commit () 准备 import pymysql 一.建立连接 conn = pymysql . connect ( user = 'root' , passwd = 'root' , database = 't5' ) 二.获取游标对象 # 注:游标不设置参数,查询的结果就是数据元组,数据没有标识性 # 设置pymysql.cursors.DictCursor,查询的结果是字典,key是表的字段cursor = conn.cursor(pymysql.cursors.DictCursor) 三.增删改查 创建表 sql1 = "create table zx(id int)" cursor . execute ( sql1 ) 增 #单条添加 sql2 = 'insert into zx values(%s)' cursor . execute ( sql2 ,( 1 ,)) cursor . execute ( sql2 ,( 2 ,)

Python3 操作Mysql数据库

匿名 (未验证) 提交于 2019-12-02 22:06:11
Pymysql介绍 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,而Python2中则使用mysqldb。 PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。 通用步骤: 1.引入模块 2.获取与数据库的连接 3.执行SQL语句和存储过程 4.关闭数据库连接 PyMySQL 安装 1.打开cmd命令 cd C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Scripts #切换目录 pip install pymysql 数据库连接 import pymysql #模块导入 #打开数据库连接 db = pymysql.connect( host='数据库ip', user='用户名, passwd='密码', db='数据库名', port=3306, charset='utf8' ) #使用 cursor() 方法创建一个游标对象 cursor cursor = db.cursor() #使用 execute() 方法执行 SQL 查询 cursor.execute("SELECT VERSION()") #使用 fetchone() 方法获取单条数据. data = cursor

doraemon的python 数据库2和pymysql

匿名 (未验证) 提交于 2019-12-02 22:06:11
##### 10.3.2.2 约束 约束: - not null 某一个字段不能为空 - default 给某个字段设置默认值 - unique 设置一个字段不能重复 - auto_increment 设置某一个int类型的字段 自动增加 - primary key 设置一个字段非空且不能重复 - 外键关联的那张表中的字段必须unique - 级联操作:on update cascade on delete cascade - foreign key 外键 - unsigned 只能是正数 not null和default: ```python create table t1( id int not null, name char(8) not null, gender enum('male','female') not null default 'male' ); ``` unique: ```python create table t2( id int unique, username char(4) unique, password char(8) not null ); ``` 联合唯一 ```python create table t3( in int, ip char(15), server char(15), port int, unique(ip,port) );

Pycharm添加Mysql数据库的坑

匿名 (未验证) 提交于 2019-12-02 22:02:20
1.Did you install mysqlclient? 解决方法: Django连接MySQL时默认使用MySQLdb驱动,但MySQLdb不支持Python3,因此这里将MySQL驱动设置为pymysql,使用 pip install pymysql 进行安装,然后在工程文件__init__.py添加以下代码即可。 #安装pymysql pip install pymysql #__init__.py import pymysql pymysql.install_as_MySQLdb() 第一种: django降到2.1.4版本就OK了 第二种(仍使用django 2.2版本): #找到Python环境下 django包,并进入到backends下的mysql文件夹 cd /opt/anaconda3/envs/envAGC_Mini/lib/python3.6/site-packages/django/db/backends/mysql #文件列表如下 # 找到base.py文件,注释掉 base.py 中如下部分(35/36行) if version < (1, 3, 3): raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database._

python pymysql 基本使用

匿名 (未验证) 提交于 2019-12-02 22:02:20
1 from pymysql import * 2 3 # 1.创建连接数据库 4 conn = connect(host="localhost", port=3306, user="root", password="root", database="jiang_test", charset="utf8") 5 # 2.获取游标 6 cur = conn.cursor() 7 # 数据库操作 8 count = cur.execute("select *from students") 9 res = cur.fetchall() # fetchone()获取一条数据 fetchmany(n)获取指定n条数据量 10 print(type(res)) # tulpe 11 print(count) # 打印出查询数据数量 12 print(res) # 打印出查询数据 13 # 3.关闭游标 14 cur.close() 15 # 4.关闭连接 16 conn.close() 步骤 1.创建连接数据库2.获取游标 3.数据库操作(增删改查)4.关闭游标5.关闭连接

Flask使用pymysql操作数据库错误汇总

匿名 (未验证) 提交于 2019-12-02 22:02:20
F:\book\lib\site-packages\pymysql\cursors.py:170: Warning: (1366, "Incorrect string value: '\xD6\xD0\xB9\xFA\xB1\xEA...' for column 'VARIABLE_VALUE' at row 481") 解决办法: # 之前的配置是这样的 import pymysql app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://账号:密码@localhost/appname" 现在使用 mysql.connector 来连接数据库 # 安装 mysql-connector-python pip install mysql-connector-python # 使用 import mysql.connector app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+mysqlconnector://账号:密码@localhost/appname" 这是msyql5.7的编码bug,用上面的方法可以妥善解决 解决办法: 检查创建数据库时使用的编码是 utf8 还是 utf8mb4 ,字段设置有没有按照对应的编码设置

pymysql模块

匿名 (未验证) 提交于 2019-12-02 22:02:20
conn = pymysql.connect(host="127.0.0.1",#默认是本机 port=3306, #默认3306 user="root",#必填 password='密码',#必填 db="库名")#必填 #如果没有库会报pymysql.err.InternalError: (1049, "Unknown database '库名'") 所有我们编辑可以这样 try: conn = pymysql.connect(host="127.0.0.1",#默认是本机 port=3306, #默认3306 user="root",#必填 password='16745',#必填 db="asds",)#必填 except pymysql.err.InternalError: print('没有库') cursor = conn.cursor(pymysql.cursors.DictCursor) #自定义游标类型为字典 cursor = conn.cursor()#默认是元祖 普通提交 count = cursor.execute('show tables') #返回值为受到影响的数据条数 防注入提交 table_name = input('table name :') count = cursor.execute('select table %s',(name,))

在python中操作MySql

匿名 (未验证) 提交于 2019-12-02 22:02:20
Python中操作MySql 文章目录 Python中操作MySql 创建数据库表 **数据库插入操作** **数据库查询操作** **数据库更新操作** **数据库删除操作** 创建数据库表 【示例】创建表student import pymysql db = pymysql . connect ( "localhost" , "root" , "root" , "python_db" ) # 创建游标对象 cursor = db . cursor ( ) try : # 使用execute()方法执行sql,如果表存在则删除 cursor . execute ( "drop table if exists student" ) # 创建表 sql sql = ''' create table student( sno int(8) primary key auto_increment, sname char(30) not null, sex varchar(5), age int(2), score float(3,1) ) ''' cursor . execute ( sql ) print ( '创表成功' ) except Exception as e : print ( e ) print ( "创表失败" ) finally : db . close ( )