Python连接MySQL数据库(pymysql的使用)
本文Python版本3.5.3,mysq版本5.7.23
基本使用
# 导入pymysql模块
import pymysql #连接数据库
conn = pymysql.connect(
database="数据库名",
user="用户名",
password="密码",
host="数据的地址,
port=3306, # 端口3306是固定的
charset="utf8") # 只能是utf8,千万不能是utf-8.在数据库中只能是utf8 #得到一个可以执行SQL的游标对象
cursor = conn.cursor() #定义需要执行的SQL语句
sql="""
create table user(
id int auto_increment primary key,
name char(10) not null unique,
age tinyint not null
)engine=innodb default charset=utf8;
"""
#执行语句
cursor.execute(sql)
#关闭游标对象
cursor.close()
#关闭数据库连接
conn.close()
对数据进行增删改查
# 导入pymysql模块
import pymysql #连接数据库
conn = pymysql.connect(
database="db",
user="root",
password="123456",
host="localhost",
port=3306,
charset="utf8") #得到一个可以执行SQL的游标对象
cursor = conn.cursor() #定义需要执行的SQL语句
sql="""
insert into user(name,age) values(%s,%s);
"""
name = "曹操",
age = 34
data = [("aaa", 18), ("bbb", 20), ("cccc", 21)] try:
print(sql)
#插入一条数据SQL语句,只能插入单条
# cursor.execute(sql,(name,age)) # 防止SQL注入 #批量插入多天SQL语句
cursor.executemany(sql,data) #第二个参数可以是列表或数组
# 提交事务)
conn.commit()
except Exception as e:
# 有异常,回滚事务
conn.rollback()
print(e)
#关闭游标对象
cursor.close()
#关闭数据库连接
conn.close()
删
# 导入pymysql模块
import pymysql #连接数据库
conn = pymysql.connect(
database="db",
user="root",
password="123456",
host="localhost",
port=3306,
charset="utf8") #得到一个可以执行SQL的游标对象
cursor = conn.cursor() #定义需要执行的SQL语句
sql="""
delete from user where id=%s
""" try:
cursor.execute(sql,[4])
# 提交事务
conn.commit()
except Exception as e:
# 有异常,回滚事务
conn.rollback()
print(e)
#关闭游标对象
cursor.close()
#关闭数据库连接
conn.close()
改
# 导入pymysql模块
import pymysql #连接数据库
conn = pymysql.connect(
database="db",
user="root",
password="123456",
host="localhost",
port=3306,
charset="utf8") #得到一个可以执行SQL的游标对象
cursor = conn.cursor() #定义需要执行的SQL语句
sql="""
update user set age=%s where name=%s;
"""
name="刘备"
age=18 try:
cursor.execute(sql,(age,name))
# 提交事务
conn.commit()
except Exception as e:
# 有异常,回滚事务
conn.rollback()
print(e)
#关闭游标对象
cursor.close()
#关闭数据库连接
conn.close()
查
# 导入pymysql模块
import pymysql #连接数据库
conn = pymysql.connect(
database="db",
user="root",
password="123456",
host="localhost",
port=3306,
charset="utf8") # 得到一个可以执行SQL的游标对象
# 增加参数后结果返回字典格式的数据
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) #定义需要执行的SQL语句
sql="""
select * from user where id=2;
""" sql1="""
select * from user;
""" # 执行查询数据的SQL语句
cursor.execute(sql)
# 获取单条查询数据
ret = cursor.fetchone() # 获取多条查询数据
cursor.execute(sql1)
ret1 = cursor.fetchall() #关闭游标对象
cursor.close()
#关闭数据库连接
conn.close() # 打印查询结果
print(ret)
print(ret1)
得到的数据为字典类型的数据,在数据的传输过程中比较好
# 可以获取指定数量的数据
cursor.fetchmany(4)
Python连接MySQL数据库(pymysql的使用)的更多相关文章
- 使用python连接mysql数据库——pymysql模块的使用
安装pymysql pip install pymysql 使用pymysql 使用数据查询语句 查询一条数据fetchone() from pymysql import * conn = conne ...
- pymysql模块使用---Python连接MySQL数据库
pymysql模块使用---Python连接MySQL数据库 浏览目录 pymysql介绍 连接数据库 execute( ) 之 sql 注入 增删改查操作 进阶用法 一.pymysql介绍 1.介绍 ...
- Python连接MySQL数据库的多种方式
上篇文章分享了windows下载mysql5.7压缩包配置安装mysql 后续可以选择 ①在本地创建一个数据库,使用navicat工具导出远程测试服务器的数据库至本地,用于学习操作,且不影响测试服务器 ...
- python 连接Mysql数据库
1.下载http://dev.mysql.com/downloads/connector/python/ 由于Python安装的是3.4,所以需要下载下面的mysql-connector-python ...
- Python连接MySQL数据库之pymysql模块使用
安装PyMySQL pip install pymysql PyMySQL介绍 PyMySQL是在python3.x版本中用于连接MySQL服务器的一个库,2中则使用mysqldb. Django中也 ...
- Mysql(九):Python连接MySQL数据库之pymysql模块使用
Python3连接MySQL 本文介绍Python3连接MySQL的第三方库--PyMySQL的基本使用. PyMySQL介绍 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服 ...
- python入门(十七)python连接mysql数据库
mysql 数据库:关系型数据库mysql:互联网公司 sqllite:小型数据库,占用资源少,手机里面使用oracle:银行.保险.以前外企.sybase:银行+通信 互联网公司key:valuem ...
- python连接mysql数据库,并进行添加、查找数据
1.删除MySQL数据表中的记录 DELETE FROM table_name WHERE condition; python操作mysql1数据库 import pymysql # 连接mysql数 ...
- Python连接MySQL数据库
连接MySQL数据库 源码: import MySQLdb #导入MySQLdb模块 print '连接数据库</br>' #连接MySQL数据库 connect the database ...
随机推荐
- IntelliJ 禁用 Search Everywhere
发现自: https://youtrack.jetbrains.com/issue/IDEA-114933#comment=27-603899 Open lib/resources.jar/idea/ ...
- linux下统计目录下所有子目录的大小
du -sh * --exclude=tar |awk '{v=substr($1,length($1),1)}v=="G"{$0="1G "$0}v==&qu ...
- C++编译器符号表有哪些内容?
http://blog.csdn.net/wangbingcsu/article/details/48340479 C++编译器符号表有哪些内容? 很早就想写一篇关于符号表的学习小结,可是迟迟不能下笔 ...
- Laravel中如何将单个routes.php分割成多个子文件
随着业务逻辑越来越复杂,routes.php文件也会变得越来越庞大,为了便于管理,我们可以像管理配置文件那样将其分割成多个子文件,这实现起来很简单: // app/routes.php ... // ...
- Linux MySql5.6.38安装过程
1.下载mysql安装包mysql-5.6.38-linux-glibc2.12-x86_64.tar.gz 2.用xftp工具将其上传到Linux服务器上的soft文件夹,没有的话先创建 [root ...
- C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)
冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C C. Serval and Parenthesis Sequence time limit pe ...
- node.js学习之post文件上传 (multer中间件)
express为了性能考虑,采用按需加载的方式,引入各种中间件来完成需求, 平时解析post上传的数据时,是用body-parser. 但这个中间件有缺点,只能解析post的文本内容,(applica ...
- Python自动化之django model验证(很弱,感觉应用场景不多)
django model的数据验证 使用full_clean进行验证 obj = models.UserInfo(name="alex",email="tiantian& ...
- Java反射学习二
利用反射进行对象拷贝的例子 如下例程ReflectTester类进一步演示了Reflection API的基本使用方法. ReflectTester类有一个copy(Object object)方法, ...
- [图解tensorflow源码] Graph 图构建 (Graph Constructor)