【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 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: ...
随机推荐
- SQL中and和or的区别是?
今天有这样得一个需求,如果登陆人是客服的话,会查询订单是’该客服’以及还没有匹配客服的,刚开始想的是直接在sql语句上拼写 or assigned_id is null 的,测试了一下发现这样的 ...
- Centos7-网卡配置
目标计划:熟悉Linux网卡 1.修改网卡名称,替换自动生成的网卡名 2.新建网卡配置文件与新增网卡的关系 3.网卡bond模式配置,team模式 4.NetworkManager-nmcli管理网络 ...
- HDU - 3644:A Chocolate Manufacturer's Problem(模拟退火, 求多边形内最大圆半径)
pro:给定一个N边形,然后给半径为R的圆,问是否可以放进去. 问题转化为多边形的最大内接圆半径.(N<50): sol:乍一看,不就是二分+半平面交验证是否有核的板子题吗. 然而事情并没有那 ...
- kvm创建windows2008虚拟机
virt-install -n win2008-fushi001 -r 16384 --vcpus=4 --os-type=windows --accelerate -c /data/kvm/imag ...
- intellij idea参数提示param hints
https://jingyan.baidu.com/article/5225f26bae80f4e6fa0908b1.html
- 13-Flutter移动电商实战-ADBanner组件的编写
1.AdBanner组件的编写 我们还是把这部分单独出来,需要说明的是,这个Class你也是可以完全独立成一个dart文件的.代码如下: 广告图片class AdBanner extends Stat ...
- Basic concepts of docker/kubernete/kata-container
Kubereters An open-source system for automating deployment, scaling, and management of containerized ...
- asp.net解决大文件断点续传
以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传 ...
- learning java 重定向标准输入输出
output redirectionOut: public class RedirectOut { public static void main(String[] args) throws File ...
- mysql 去除字符串中的空格
mysql> select " ddd dddee "; +--------------+ | ddd dddee | +--------------+ | ddd ddde ...