环境
  虚拟机: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学习之十】操作数据库的更多相关文章

  1. python学习笔记:操作数据库

    1.下载安装模块 第一种:cmd下:执行命令下载安装:pip3 install pymysql 第二种:IDE下pycharm python环境路径下添加模块 2.连接数据库 import pymys ...

  2. python学习笔记(十)完善数据库操作

    1.cur = coon.cursor(cursor=pymysql.cursors.DictCursor)的用法 建立游标,指定cursor类型返回的是字典,如果不指定类型,返回的是元组类型数据 i ...

  3. Python 学习 第十篇 CMDB用户权限管理

    Python 学习 第十篇 CMDB用户权限管理 2016-10-10 16:29:17 标签: python 版权声明:原创作品,谢绝转载!否则将追究法律责任. 不管是什么系统,用户权限都是至关重要 ...

  4. [Python] Python 学习 - 可视化数据操作(一)

    Python 学习 - 可视化数据操作(一) GitHub:https://github.com/liqingwen2015/my_data_view 目录 折线图 散点图 随机漫步 骰子点数概率 文 ...

  5. 使用python简单连接并操作数据库

    python中连接并操作数据库 图示操作流程 一.使用的完整流程 # 1. 导入模块 from pymysql import connect # 2. 创建和数据库服务器的连接,自行设置 服务器地址, ...

  6. python 学习笔记十六 django深入学习一 路由系统,模板,admin,数据库操作

    django 请求流程图 django 路由系统 在django中我们可以通过定义urls,让不同的url路由到不同的处理函数 from . import views urlpatterns = [ ...

  7. python学习笔记(十 二)、操作数据库

    每一种语言都少不了多数据库进行各种操作. python支持多种数据库.有关python支持的数据库清单,请参阅:https://wiki.python.org/moin/DatabaseInterfa ...

  8. [Python] 学习笔记之MySQL数据库操作

    1 Python标准数据库接口DB-API介绍 Python标准数据库接口为 Python DB-API,它为开发人员提供了数据库应用编程接口.Python DB-API支持很多种的数据库,你可以选择 ...

  9. python学习笔记之——操作mysql数据库

    Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: ...

随机推荐

  1. 使用flask搭建微信公众号:实现签到功能

    终于到了实战阶段.用微信公众号实现一个简单的签到功能. 前情提要: 微信公众号token验证失败 使用flask搭建微信公众号:完成token的验证 使用flask搭建微信公众号:接收与回复消息 程序 ...

  2. Linux TTY介绍

    1. TTY介绍 TTY(TeleType)指Linux中的一类终端(Terminal)设备, 是一种字符设备 在Linux中, tty可分为如下几类- 串行端口终端(serial port term ...

  3. discuz x3.4 开启tags聚合标签及伪静态配置方法

    因为SEO的需要,要做tags聚合到一个页面,做到伪静态. 例如: misc.php?mod=tag >>> /tag/ misc.php?mod=tag&id=47 > ...

  4. python 添加Windows权限

    # -*- coding: utf-8 -*- """ Created on Mon Jan 8 09:09:51 2018 @author: coordinate &q ...

  5. LeetCode 1043. Partition Array for Maximum Sum

    原题链接在这里:https://leetcode.com/problems/partition-array-for-maximum-sum/ 题目: Given an integer array A, ...

  6. 按值传递与按值引用详解(java版)

    http://blog.csdn.net/zzp_403184692/article/details/8184751

  7. node安装失败报错

     安装Node有时会报错 提示这段信息 怎么安装都不行 最后通过命令行安装就可以完成 1.首先去Node下载安装包 下载完后放在本地 比如我放在桌面aa这个文件夹里 2.进去aa这个文件 复制里面的路 ...

  8. SQL基础-操纵表及插入、查询

    一.操纵表 1.表的关键信息 2.更新表名 更新表名:使用RENAME TABLE关键字.语法如下: RENAME TABLE 旧表名 TO 新表名; 比如,生产环境投产前备份teacher表,使用如 ...

  9. OpenFOAM——具有压差的平行平板间流动(泊肃叶流动)

    本算例翻译整理自:http://the-foam-house5.webnode.es/products/chapter-1-plane-parallel-plates-case/ 这个算例中两平板间没 ...

  10. AdaptIS: Adaptive Instance Selection Network

    AdaptIS: Adaptive Instance Selection Network 2019-09-19 12:58:07 Paper: https://arxiv.org/pdf/1909.0 ...