笔记-python操作mysql
笔记-python操作mysql
1. 开始
1.1. 环境准备-mysql
create database db_python;
use db_python;
create table `t2`(
`id` int unsigned auto_increment,
`name` varchar(30),
primary key(`id`));
#创建用户并授权
create user 'dev_python' identified by '123456';
grant all on db_python.t2 to dev_python@'%';
alter table t2 add password varchar(30);
alter table t2 add content varchar(500);
1.2. 环境-python
使用pymysql库,PyMySQL是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中是使用mysqldb。
pip install pymysql
2. API
2.1. connect
连接类,有一些参数可以调整,列出最常用的部分:
# connect to database, parameter: address, user name, password, database name,charset
db = pymysql.connect('localhost','dev_python','123456','db_python',cursorclass=pymysql.cursors.DictCursor)
2.2.
cursor
执行操作的接口,有
execute(query, args=None)
Execute a query
Parameters: |
query (str) – Query to execute. args (tuple, list or dict) – parameters used with query. (optional) |
Returns: |
Number of affected rows |
Return type: |
If args is a list or tuple, %s can be used
as a placeholder in the query. If args is a dict, %(name)s can be used as a placeholder in the query.
fetchall()
Fetch all the rows
fetchmany(size=None)
Fetch several rows
fetchone()
Fetch the next row
classpymysql.cursors.DictCursor(connection)
A cursor which returns results as a
dictionary
返回的是一个字典,使用时需要注意返回的字典的健值。
3.
使用
import pymysql
# connect to database, parameter: address, user name, password,
database name,charset
db = pymysql.connect('localhost','dev_python','123456','db_python',cursorclass=pymysql.cursors.DictCursor)
'''
# 得到一个可以执行sql语句的cursor object
# 注意cursor支持上下文,
# with db.cursor() as cur:
cursor = db.cursor()
#定义要执行的sql语句
# 本处为查看表结构
cmd_text = 'desc t2;'
# 执行
print(cursor.execute(cmd_text))
# 关闭相关对象
cursor.close()
db.close()
'''
# execute statement
#create table
sql_create = r"""create table if not
exists `t3` (`id` int)"""
with db.cursor() as cur:
cur.execute(sql_create)
print(cur.execute('show
tables;'))
print(cur.fetchall())
#CURD
# 插入
cmd_insert = """insert into t2
values('7','first_name','pass1', 'content1')"""
with db.cursor() as cur:
pass
#print(cur.execute(cmd_insert))
db.commit()
# update
sql_update = """update t2 set name=%s
where id=%s"""
with db.cursor() as cur:
cur.execute(sql_update, ['ppp','7'])
cur.execute('select * from t2')
#print(cur.fetchall())
# retrieve
sql_select = """select * from
t3;"""
with db.cursor() as cur:
if cur.execute(sql_select):
print(cur.fetchall())
else:
print('get 0
rows.')
# delete
sql_delete = """delete from t2 where
id=%s"""
with db.cursor() as cur:
cur.execute(sql_delete, ['7'])
cur.execute('select * from t2')
print(cur.fetchall())
上面列出的仅包括最常用的CURD操作,更复杂的功能需要参考相关接口文档。
4.
参考文档
https://pypi.org/project/PyMySQL/
https://pymysql.readthedocs.io/en/latest/
笔记-python操作mysql的更多相关文章
- mysql数据库----python操作mysql ------pymysql和SQLAchemy
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy 一.pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQ ...
- Python(九) Python 操作 MySQL 之 pysql 与 SQLAchemy
本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...
- 练习:python 操作Mysql 实现登录验证 用户权限管理
python 操作Mysql 实现登录验证 用户权限管理
- Python操作MySQL
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...
- Python操作Mysql之基本操作
pymysql python操作mysql依赖pymysql这个模块 下载安装 pip3 install pymysql 操作mysql python操作mysql的时候,是通过”游标”来进行操作的. ...
- python成长之路【第十三篇】:Python操作MySQL之pymysql
对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎 ...
- python操作mysql数据库的相关操作实例
python操作mysql数据库的相关操作实例 # -*- coding: utf-8 -*- #python operate mysql database import MySQLdb #数据库名称 ...
- Python 操作 MySQL 之 pysql 与 ORM(转载)
本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...
- Python开发【第十九篇】:Python操作MySQL
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...
随机推荐
- CommonJS 的实现原理
CommonJS 使用 Node.js 的四个环境变量moduleexportsrequireglobal 只要能够提供这四个变量,浏览器就能加载 CommonJS 模块. Browserify 是目 ...
- DirectX HLSL 内置函数
Intrinsic Functions (DirectX HLSL) The following table lists the intrinsic functions available in HL ...
- Swift自适应布局(Adaptive Layout)教程(一)
通用的stroyboard文件是通向自适应布局光明大道的第一步.在一个storyboard文件中适配iPad和iPhone的布局在iOS8中已不再是梦想.我们不必再为不同尺寸的Apple移动设备创建不 ...
- 初看Mybatis 源码 (一)
Mybatis 的使用,首先需要构建一个SqlSessionFactory 实例.而该实例可以通过SqlSessionFactoryBuilder来创建. String resource = &quo ...
- Linux目录与文件的权限意义
ls -l和ls -al的区别:第一个不会显示隐藏文件,第二个会显示隐藏文件(以点(.)开头的文件) 一.权限对文件(r.w.x主要针对文件的内容而言)的重要性 r:可读取文件内容 w:可以编辑.新增 ...
- Linux与Windows区别——总结中
一:在Linux系统中,每一个文件都多加了很多的属性进来,尤其是用户组的概念 二:Windows下面一个文件是否具有执行的能力是通过“扩展名”来判断的,如:.exe,.bat,.com等 Linux下 ...
- Centos 5.2下安装多个mysql数据库
一.编译安装第一个MySQL 5.1.33 cd /opt/usr/sbin/groupadd mysql/usr/sbin/useradd -g mysql mysql -s /bin/nologi ...
- 2017.11.14 C语言---指针的学习
第八章 善于利用指针 (1)指针是什么 1.内存区每一个字节都有一个编号,这就是"地址".地址形象化的被称为"指针".它能通过以它为地址的内存单元.地址指向(* ...
- ios相关配置
Deployment Target,它控制着运行应用需要的最低操作系统版本.
- 图像上采样(图像插值)增取样(Upsampling)或内插(Interpolating)下采样(降采样),
缩小图像(或称为下采样(subsampled)或降采样(downsampled))的主要目的有两个:1.使得图像符合显示区域的大小:2.生成对应图像的缩略图.放大图像(或称为上采样(upsamplin ...