【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 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: ...
随机推荐
- 《逆袭团队》第九次团队作业【Beta】Scrum meeting 3
项目 内容 软件工程 任课教师博客主页链接 作业链接地址 团队作业9:Beta冲刺与团队项目验收 团队名称 逆袭团队 具体目标 (1)掌握软件黑盒测试技术:(2)学会编制软件项目总结PPT.项目验收报 ...
- SQL操作Spark SQL--BasicSQLTestt
object BasicSQLTest { def main(args: Array[String]): Unit = { val spark = SparkSession .builder() .a ...
- 使用mybatis框架实现带条件查询-多条件(传入Map集合)
我们发现我们可以通过传入javaBean的方式实现我们的需求,但是就两个条件,思考:现在就给他传入一个实体类,对系统性能的开销是不是有点大了. 现在改用传入Map集合的方式: 奥!对了,在创建map集 ...
- python的numpy.array
为什么要用numpy Python中提供了list容器,可以当作数组使用.但列表中的元素可以是任何对象,因此列表中保存的是对象的指针,这样一来,为了保存一个简单的列表[1,2,3].就需要三个指针和三 ...
- SpringBoot之使用Druid连接池,SQL监控和spring监控
项目结构 1.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot< ...
- VS2010中使用boost正则表达式库
1.下载boost库.http://www.boost.org/ 我下载的是boost_1_51_0版本.放在D:\opensource\boost_1_51_0. 2.编译boost库. 执 ...
- 【批处理】set命令
原文地址:https://www.cnblogs.com/Braveliu/p/5081084.html [1]set命令简介 set,设置. [2]set命令使用 1. 打印系统环境变量.set命令 ...
- mysql linux上安装使用
安装启动 安装之前可以看下系统中有没有已经安装. 查看所有软件:dpkg -l 1.查看mysql安装的版本 mysql --version 2.mysql状态 service mysql statu ...
- 2-ESP8266 SDK开发基础入门篇--非RTOS版与RTOS版
https://www.cnblogs.com/yangfengwu/p/11071580.html 所有的源码 https://gitee.com/yang456/Learn8266SDKDevel ...
- 洛谷P2744 量取牛奶
题目 DP或者迭代加深搜索,比较考验递归的搜索. 题目第一问可以用迭代加深搜索限制层数. 第二问需要满足字典序最小,所以我们可以在搜索的时候把比当前答案字典序大的情况剪枝掉. 然后考虑怎么搜索,对于每 ...