pymysql
2018-06-18 00:40:29来源:未知 阅读 ()
一、pymysql
pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同,我们今天来说下pymsql用法。MySQLdb的只有在python2 里面使用的,在python3里面需要用pymysql。
二、安装pymysql
pip3 install pymysql
三、使用
3.1 连接mysql
# -*- coding: UTF-8 -*-
import pymysql
# 创建连接
conn = pymysql.connect(host='172.16.200.49', port=3306,
user='bigberg', password='111111',
db='study')
# 创建游标
cursor = conn.cursor()
# 执行SQL,并返回受影响行数
effect_row = cursor.execute("select * from student")
print(effect_row)
# 执行SQL,并返回受影响行数
# data = [
# ('S1','2018-04-21','M'),
# ('S2','2018-05-11','F'),
# ('S3','2018-04-25','M')
# ]
# sql = "insert into student (name,register_date,gender) values (%s,%s,%s)"
# effect_row = cursor.executemany(sql,data)
# 提交,不然无法保存
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
3.2 获取查询的数据
# -*- coding: UTF-8 -*-
import pymysql
# 创建连接
conn = pymysql.connect(host='172.16.200.49', port=3306,
user='bigberg', password='111111',
db='study')
cursor = conn.cursor()
sql = "select * from student where stu_id>3"
cursor.execute(sql)
# 获取第一行数据
# result = cursor.fetchone()
# 获取前n条数据
# result = cursor.fetchmany(3)
# 获取所有数据
result = cursor.fetchall()
print(result)
conn.commit()
cursor.close()
conn.close()
3.3 fetch数据类型
import pymysql
# 创建连接
conn = pymysql.connect(host='172.16.200.49', port=3306,
user='bigberg', password='111111',
db='study',)
# 游标设置为字典类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:python socket模块
- pymysql 的简单使用 2019-07-24
- django_mysql_配置 2019-05-23
- MySQL 之Navicat Premium 12安装使用、pymysql模块使用、sql 2019-05-17
- 调用pymysql模块操作数据库 2019-04-26
- python基础学习24----使用pymysql连接mysql 2018-10-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
