【Python学习之十】操作数据库
环境
虚拟机:VMware 10
Linux版本:CentOS-6.5-x86_64
客户端:Xshell4
FTP:Xftp4
python3.6
操作mysql数据库
1、安装pymysql模块
pip install pymysql
或者
conda install pymysql
2、使用示例
相对java 是非常简单的
'''
Created on 2019年5月8日 @author: Administrator
'''
import pymysql as db
#创建连接
conn=db.connect('134.32.123.101','root','','spark')
#获取游标
cursor=conn.cursor()
#游标执行语句
cursor.execute('select * from person')
#获取一条记录
pn=cursor.fetchone()
print(pn)#结果是一个元组:('1', 'zhangsan', 18)
#获取所有元素
pns=cursor.fetchall()
print(pns)#(('2', 'wangwu', 12), ('3', 'lisi', None))
#关闭连接
conn.close()
3、数据库操作工具类
#coding:utf-8
import pymysql class MysqlHelper(object):
config={
"host":"localhost",
"user":"root",
"password":"",
"db":"demo",
"charset":"utf8"
}
def __init__(self):
self.connection=None
self.cursor=None # 从数据库表中查询一行数据 select count(*) from emp
def getOne(self,sql,*args):
try:
self.connection = pymysql.connect(**MysqlHelper.config)
self.cursor = self.connection.cursor()
self.cursor.execute(sql,args)
return self.cursor.fetchone()
except Exception as ex:
print(ex,ex)
finally:
self.close() # 从数据库表中查询多行数据
def getList(self,sql,*args):
try:
self.connection = pymysql.connect(**MysqlHelper.config)
self.cursor = self.connection.cursor()
self.cursor.execute(sql,args)
return self.cursor.fetchall()
except Exception as ex:
print(ex,ex)
finally:
self.close() # 对数据库进行增,删,改
def executeDML(self,sql,*args):
try:
self.connection = pymysql.connect(**MysqlHelper.config)
self.cursor = self.connection.cursor()
self.cursor.execute(sql,args)# 返回 sql语句执行之后影响的行数
new_id = self.connection.insert_id() # 返回系统刚刚自动生成的id
self.connection.commit();
return new_id
except Exception as ex:
self.connection.rollback()
print(ex,ex)
finally:
self.close() def close(self):
if self.cursor:
self.cursor.close()
if self.connection:
self.connection.close() if __name__ == "__main__":
helper = MysqlHelper()
print(helper.executeDML("delete from dept where deptno=%s",80))
# print(helper.executeDML("insert into dept values(%s,%s,%s)","80","admin","beijing"))
参考:
Python学习笔记
【Python学习之十】操作数据库的更多相关文章
- python学习笔记:操作数据库
1.下载安装模块 第一种:cmd下:执行命令下载安装:pip3 install pymysql 第二种:IDE下pycharm python环境路径下添加模块 2.连接数据库 import pymys ...
- python学习笔记(十)完善数据库操作
1.cur = coon.cursor(cursor=pymysql.cursors.DictCursor)的用法 建立游标,指定cursor类型返回的是字典,如果不指定类型,返回的是元组类型数据 i ...
- Python 学习 第十篇 CMDB用户权限管理
Python 学习 第十篇 CMDB用户权限管理 2016-10-10 16:29:17 标签: python 版权声明:原创作品,谢绝转载!否则将追究法律责任. 不管是什么系统,用户权限都是至关重要 ...
- [Python] Python 学习 - 可视化数据操作(一)
Python 学习 - 可视化数据操作(一) GitHub:https://github.com/liqingwen2015/my_data_view 目录 折线图 散点图 随机漫步 骰子点数概率 文 ...
- 使用python简单连接并操作数据库
python中连接并操作数据库 图示操作流程 一.使用的完整流程 # 1. 导入模块 from pymysql import connect # 2. 创建和数据库服务器的连接,自行设置 服务器地址, ...
- python 学习笔记十六 django深入学习一 路由系统,模板,admin,数据库操作
django 请求流程图 django 路由系统 在django中我们可以通过定义urls,让不同的url路由到不同的处理函数 from . import views urlpatterns = [ ...
- python学习笔记(十 二)、操作数据库
每一种语言都少不了多数据库进行各种操作. python支持多种数据库.有关python支持的数据库清单,请参阅:https://wiki.python.org/moin/DatabaseInterfa ...
- [Python] 学习笔记之MySQL数据库操作
1 Python标准数据库接口DB-API介绍 Python标准数据库接口为 Python DB-API,它为开发人员提供了数据库应用编程接口.Python DB-API支持很多种的数据库,你可以选择 ...
- python学习笔记之——操作mysql数据库
Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: ...
随机推荐
- http消息与webservice
别人的:在一台配置较低的PC上,同时开启服务端与客户端,10000条数据,使用基于http的消息逐条进行传递,从开始传递至全部接收并处理完毕,大概需要465秒的时间:而在同一台机器上,使用WebSer ...
- 走,去出海,一起“Copy to World” | 36氪出海行业报告
http://www.sohu.com/a/200845344_114778 从工具类产品在海外聚集大量流量到新闻.社交游戏等内容类产品在海外取得优异成绩,中国正在完成从Copy to China向C ...
- [NOIp 2018]all
Description 题库链接: Day1 T1 铺设道路 Day1 T2 货币系统 Day1 T3 赛道修建 Day2 T1 旅行 Day2 T2 填数游戏 Day2 T3 保卫王国 Soluti ...
- Cocos2d-x学习小结 配置篇
Cocos2d-x学习小结 配置篇 学习工具:Cocos2d-x用户手册,<Cocos2d-x游戏开发之旅> 首先官网下载cocos2d-x源码,安装vs2019.如果没有安装python ...
- Django rest_fram_work API View序列化
APIview 单表的GET和POST: 视图 查询所有: class PublishView(APIView): # 查询数据 def get(self, request): # first inq ...
- [Debug] How to Debug a NestJs Backend using the Chrome Dev Tools
TO debug NestJS code with Chrome dev tool, we can run: node --inspect-brk dist/rest-api/src/main.js ...
- 集成omnibus-ctl 开发一个专业的软件包管理工具
前边有转发过来自chef 团队的一篇omnibus-ctl 介绍文章,以下尝试进行项目试用 就是简单的集成,没有多少复杂的操作 环境准备 ruby ruby 使用2.6.3 使用 rbenv 安装,可 ...
- 洛谷 P1522 牛的旅行 Cow Tours 题解
P1522 牛的旅行 Cow Tours 题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不 ...
- flume 测试 hive sink
测试flume,将数据送到hive表中,首先建表. create table order_flume( order_id string, user_id string, eval_set string ...
- 坑:jmeter部署AWS云服务器时出现连接超时Non HTTP response code: org.apache.http.conn.HttpHostConnectException
背景: jmeter脚本部署到云服务器(AWS EC2)公网上时,启动jmeter脚本运行了5个小时才运行完毕,后面发现脚本报错timeout(如图),找了很久不知道原因,后面进入脚本发现全部在报错. ...