10 class封装 ORM
1.版本1:初始化
# -*- coding:utf-8 -*-
from MySQLdb import * class MysqlHelper:
def __init__(self,host,port,user,passwd,db,charset='utf8'):
self.host=host
self.port=port
self.user=user
self.passwd=passwd
self.db=db
self.charset=charset mysql1 = MysqlHelper("localhost",3306,"root","mysql","py31""utf8")
2.版本2:打开关闭方法
# -*- coding:utf-8 -*-
from MySQLdb import * class MysqlHelper:
"""封装"""
def __init__(self,host,port,user,passwd,db,charset='utf8'):
"""初始化"""
self.host=host
self.port=port
self.user=user
self.passwd=passwd
self.db=db
self.charset=charset def open(self):
"""连接数据库"""
self.conn = connect(host=self.host, port=self.port, user=self.user, passwd=self.passwd, db=self.db, charset=self.charset)
self.cursor1 = self.conn.cursor() def close(self):
"""关闭连接"""
self.cursor1.close()
self.conn.close() mysql1 = MysqlHelper("localhost",3306,"root","mysql","py31""utf8")
3.版本3:增加修改删除
# -*- coding:utf-8 -*-
from MySQLdb import * class MysqlHelper:
"""封装"""
def __init__(self,host,port,user,passwd,db,charset='utf8'):
"""初始化"""
self.host=host
self.port=port
self.user=user
self.passwd=passwd
self.db=db
self.charset=charset def open(self):
"""连接数据库"""
self.conn = connect(host=self.host, port=self.port, user=self.user, passwd=self.passwd, db=self.db, charset=self.charset)
self.cursor1 = self.conn.cursor() def close(self):
"""关闭连接"""
self.cursor1.close()
self.conn.close() def iud(self,sql_content):
"""增删改"""
self.open() #调用open方法 self.sql = '%s'[sql_content]
self.cursor1.execute(self.sql)
self.conn.commit() self.close() #调用close方法 mysql1 = MysqlHelper("localhost",3306,"root","mysql","py31""utf8") sql_content = raw_input('请输入sql语句:')
mysql1.iud(sql_content)

4.版本4:抛出异常,参数化
# -*- coding:utf-8 -*-
from MySQLdb import * class MysqlHelper:
"""封装"""
def __init__(self,host,port,user,passwd,db,charset='utf8'):
"""初始化"""
self.host=host
self.port=port
self.user=user
self.passwd=passwd
self.db=db
self.charset=charset def open(self):
"""连接数据库"""
self.conn = connect(host=self.host, port=self.port, user=self.user, passwd=self.passwd, db=self.db, charset=self.charset)
self.cursor1 = self.conn.cursor() def close(self):
"""关闭连接"""
self.cursor1.close()
self.conn.close() def iud(self,sql,params):
"""增删改"""
try:
self.open() #调用open方法 self.cursor1.execute(sql,params)
self.conn.commit() self.close() #调用close方法
print('ok')
except Exception as e:
print(e.message) mysql1 = MysqlHelper("localhost",3306,"root","mysql","py31","utf8") name = "jack"
id = 1
sql = 'update students set name=%s where id=%s'
params=[name,id]
mysql1.iud(sql,params)
/usr/bin/python2.7 /home/python/code/03-class封装.py
ok +----+------------+--------+---------------------+----------+
| 1 | jack | | 1999-09-09 00:00:00 | |
5.版本5:查询
# -*- coding:utf-8 -*-
from MySQLdb import * class MysqlHelper:
"""封装"""
def __init__(self,host,port,user,passwd,db,charset='utf8'):
"""初始化"""
self.host=host
self.port=port
self.user=user
self.passwd=passwd
self.db=db
self.charset=charset def open(self):
"""连接数据库"""
self.conn = connect(host=self.host, port=self.port, user=self.user, passwd=self.passwd, db=self.db, charset=self.charset)
self.cursor1 = self.conn.cursor() def close(self):
"""关闭连接"""
self.cursor1.close()
self.conn.close() def iud(self,sql,params):
"""增删改"""
try:
self.open() #调用open方法 self.cursor1.execute(sql,params)
self.conn.commit() self.close() #调用close方法
print('ok')
except Exception as e:
print(e.message) def all(self,sql,params=()): #默认参数
try:
self.open()
print ""
self.cursor1.execute(sql,params)
result = self.cursor1.fetchall() #fetchall()获取多条数据
print "" #fetchone()获取1条数据
print(result)
self.close() return result #返回result except Exception as e:
print(e.message) mysql1 = MysqlHelper("localhost",3306,"root","mysql","py31","utf8") sql = 'select * from students where id<5'
result = mysql1.all(sql)
print(result)
1
2
((1L, u'jack', '\x01', datetime.datetime(1999, 9, 9, 0, 0), '\x00'), (2L, u'\u817e\u65ed', '\x01', datetime.datetime(1990, 2, 2, 0, 0), '\x00'), (3L, u'\u7f51\u6613', '\x01', None, '\x00'), (4L, u'\u5c0f\u7c73', '\x01', None, '\x00'))
((1L, u'jack', '\x01', datetime.datetime(1999, 9, 9, 0, 0), '\x00'), (2L, u'\u817e\u65ed', '\x01', datetime.datetime(1990, 2, 2, 0, 0), '\x00'), (3L, u'\u7f51\u6613', '\x01', None, '\x00'), (4L, u'\u5c0f\u7c73', '\x01', None, '\x00'))
6.版本6:获取单条数据
# -*- coding:utf-8 -*-
from MySQLdb import * class MysqlHelper:
"""封装"""
def __init__(self,host,port,user,passwd,db,charset='utf8'):
"""初始化"""
self.host=host
self.port=port
self.user=user
self.passwd=passwd
self.db=db
self.charset=charset def open(self):
"""连接数据库"""
self.conn = connect(host=self.host, port=self.port, user=self.user, passwd=self.passwd, db=self.db, charset=self.charset)
self.cursor1 = self.conn.cursor() def close(self):
"""关闭连接"""
self.cursor1.close()
self.conn.close() def iud(self,sql,params):
"""增删改"""
try:
self.open() #调用open方法 self.cursor1.execute(sql,params)
self.conn.commit() self.close() #调用close方法
print('ok')
except Exception as e:
print(e.message) def all(self,sql,params=()):
"""获取多条数据"""
try:
self.open() self.cursor1.execute(sql,params)
result = self.cursor1.fetchall()
print(result) self.close() return result except Exception as e:
print(e.message) def one(self,sql,params=()):
"""获取单挑数据"""
try:
self.open() self.cursor1.execute(sql,params)
result = self.cursor1.fetchone()
print(result) self.close() return result except Exception as e:
print(e.message)
7.版本7:封装完成
#encoding=utf8
import MySQLdb class MysqlHelper():
def __init__(self,host,port,db,user,passwd,charset='utf8'):
self.host=host
self.port=port
self.db=db
self.user=user
self.passwd=passwd
self.charset=charset def connect(self):
self.conn=MySQLdb.connect(host=self.host,port=self.port,db=self.db,user=self.user,passwd=self.passwd,charset=self.charset)
self.cursor=self.conn.cursor() def close(self):
self.cursor.close()
self.conn.close() def get_one(self,sql,params=()):
result=None
try:
self.connect()
self.cursor.execute(sql, params)
result = self.cursor.fetchone()
self.close()
except Exception, e:
print e.message
return result def get_all(self,sql,params=()):
list=()
try:
self.connect()
self.cursor.execute(sql,params)
list=self.cursor.fetchall()
self.close()
except Exception,e:
print e.message
return list def insert(self,sql,params=()):
return self.__edit(sql,params) def update(self, sql, params=()):
return self.__edit(sql, params) def delete(self, sql, params=()):
return self.__edit(sql, params) def __edit(self,sql,params):
count=0
try:
self.connect()
count=self.cursor.execute(sql,params)
self.conn.commit()
self.close()
except Exception,e:
print e.message
return count
8 当做第三方模块导入

10 class封装 ORM的更多相关文章
- Windows XP/Windows 7/Windows 8/Windows 10系统封装的另类教程和思路
如果是早些年,XP时代的Ghost封装,各种的封装工具和驱动只能安装工具满天飞,比如龙帝国,还有很早用C++写的忘了什么名字了,自由天空的,非常的多: 当时为什么要用Ghost和用这些驱动安装工具以及 ...
- 10 python 封装----@property的用法
1.基本概念 在python中用双下划线开头的方式将属性隐藏起来(设置成私有的) #其实这仅仅这是一种变形操作 #类中所有双下划线开头的名称如__x都会自动变形成:_类名__x的形式: class A ...
- 封装ORM.py与mysql_client.py代码
ORM.py ''' ORM: 对象关系映射 ---> 映射到数据库MySQL中的数据表 类名 ---> 表名 对象 ---> 一条记录 对象.属性 ---> 字段 模拟Dja ...
- 我的第一个python web开发框架(30)——定制ORM(六)
在开发中,查询操作是使用最多的,而查询列表是其中之一,查询列表可分为分页查询和不分页查询(它们之间多了一次总记录数查询),还可以分为单表查询和多表关联查询,返回的结构体根据前端使用的表单框架不同而有所 ...
- Django中ORM介绍和字段及字段参数
Object Relational Mapping(ORM) 1 ORM介绍 1.1 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对 ...
- .Net Core3.1 + EF Core + LayUI 封装的MVC版后台管理系统
项目名称:学生信息管理系统1.0 后台框架:.Net Core 3.1 + EF Core yrjw.ORM.Chimp 前端框架:ASP.NET Core MVC + LayUI + Bo ...
- Django基础四之测试环境和ORM查询
Django基础四之测试环境和ORM查询 目录 Django基础四之测试环境和ORM查询 1. 搭建测试环境 1.1 测试环境搭建方法: 1.2 使用测试环境对数据库进行CURD 1.3 返回Quer ...
- 【ASP.NET程序员福利】打造一款人见人爱的ORM(二)
上一篇我已经给大家介绍AntORM的框架[ASP.NET程序员福利]打造一款人见人爱的ORM(一),今天就来着重介绍一下如何使用这套框架 1>AntORM 所有成员 如果你只想操作一种数据库,可 ...
- Moon.Orm 5.0 (MQL版)
Moon.Orm 5.0 (MQL版) 实战实例Moon.Orm 5.0 革命性的设计 打造最便捷的异步分页技术(提供下载) 摘要: 一.建一个项目(以WebForm为例)配置文件配置(注意您自己的路 ...
随机推荐
- Spring Boot:内置tomcat启动和外部tomcat部署总结
springboot的web项目的启动主要分为: 一.使用内置tomcat启动 启动方式: 1.IDEA中main函数启动 2.mvn springboot-run 命令 3.java -jar XX ...
- Office加载项安装
出自我的个人主页 Alvin Blog 前言 Excel加载项离不开安装,Excel加载项本身安装及其简单,但这是在申请下来Office开发者账户之后,再次之前都得自行安装 线上安装 微软申请开发者账 ...
- php中的curl_multi的应用(php多进程)
相信许多人对PHP手册中语焉不详的curl_multi一族的函数头疼不已,它们文档少,给的例子 更是简单的让你无从借鉴,我也曾经找了许多网页,都没见一个完整的应用例子. curl_multi_add_ ...
- 双网卡(一外一内)都启用,将内网卡默认网关去除即可正常连接Internet
- 【js基础修炼之路】--创建文档碎片document.createDocumentFragment()
讲这个方法之前,我们应该先了解下插入节点时浏览器会做什么. 在浏览器中,我们一旦把节点添加到document.body(或者其他节点)中,页面就会更新并反映出这个变化,对于 ...
- 1929. Teddybears are not for Everyone (Timus) (combination+reading questions)
http://acm.timus.ru/problem.aspx?space=1&num=1929 combination problems. 排列组合问题. According to the ...
- android+nutz后台如何上传和下载图片
android+nutz后台如何上传和下载图片 发布于 588天前 作者 yummy222 428 次浏览 复制 上一个帖子 下一个帖子 标签: 无 最近在做一个基于android的ap ...
- hdu-3015 Disharmony Trees---离散化+两个树状数组
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3015 题目大意: 有一些树,这些树的高度和位置给出.现在高度和位置都按从小到大排序,对应一个新的ra ...
- 【[SCOI2015]小凸玩矩阵】
题目 第\(k\)大显然没有什么办法直接求,于是多一个\(log\)来二分一波 现在的问题变成了判断一个\(mid\)是否能成为第\(k\)大 这还是一个非常经典的棋盘模型,于是经典的做法就是转化成二 ...
- 2017.10.24 Java 详解 JVM 工作原理和流程
JVM工作原理和特点主要是指操作系统装入JVM是通过jdk中Java.exe来完成,通过下面4步来完成JVM环境. 1.创建JVM装载环境和配置 2.装载JVM.dll 3.初始化JVM.dll并挂界 ...