python 封装对象数据_Python数据库封装实现代码示例解析
django中(原生mysql封装)
1.函数封装
import pymysql
# 查 所数据
def get_all(sql):
conn = pymysql.connect(host="localhost", user="root", password="root", database="db6")
cur = conn.cursor(cursor=pymysql.cursors.dictcursor)
cur.execute(sql)
res = cur.fetchall()
cur.close()
conn.close()
return res
# 查 一行数据
def get_one(sql,args):
conn = pymysql.connect(host="localhost", user="root", password="root", database="db6")
cur = conn.cursor(cursor=pymysql.cursors.dictcursor)
cur.execute(sql,args)
res = cur.fetchone()
cur.close()
conn.close()
return res
# 增 删 改 都要提交 commit
def get_mif(sql,args):
conn = pymysql.connect(host="localhost", user="root", password="root", database="db6")
cur = conn.cursor(cursor=pymysql.cursors.dictcursor)
cur.execute(sql,args)
conn.commit()
cur.close()
conn.close()
# 增 删 改 都要提交 commit
# 添加并且带返回值
def get_create(sql,args):
conn = pymysql.connect(host="localhost", user="root", password="root", database="db6")
cur = conn.cursor(cursor=pymysql.cursors.dictcursor)
cur.execute(sql,args)
conn.commit()
cur.close()
conn.close()
return cur.lastrowid
# python插入记录后取得主键id的方法(cursor.lastrowid和conn.insert_id())
# 增 删 改 都要提交 commit
# 批量加入 以元祖的形式传参数 就是添加几次次提交一次
def mul_mode(sql, args):
conn = pymysql.connect(host="localhost", user="root", password="root", database="db6")
cur = conn.cursor(cursor=pymysql.cursors.dictcursor)
# self.cursor.executemany("insert into user (id,name) values (%s,%s)",[(1,"aaa"),(2,"bbb"),(3,"ccc")]) 传参方式
cur.executemany(sql, args)
conn.commit()
cur.close()
conn.close()
2.对象封装
import pymysql
# 注意 args 参数可以传空值[]
class mysqls(object):
def __init__(self):
# 读取配置文件
self.connect()
def connect(self):
self.conn = pymysql.connect(host="localhost", user="root", password="root", database="db6") # 可以把主机连接等写入配置文件 等
self.cursor=self.conn.cursor(cursor=pymysql.cursors.dictcursor)
# 获取所以数据
def get_all(self,sql,args):
self.cursor.execute(sql,args)
res = self.cursor.fetchall()
return res
# 获取一行数据
def get_one(self,sql,args):
self.cursor.execute(sql, args)
res = self.cursor.fetchone()
return res
# 添加 就是添加一次提交多次
def get_mode (self,sql,args):
self.cursor.execute(sql, args)
self.conn.commit()
# 添加并且带返回值
def get_create(self,sql,args):
self.cursor.execute(sql,args)
self.conn.commit()
return self.cursor.lastrowid
# python插入记录后取得主键id的方法(cursor.lastrowid和conn.insert_id())
# 批量加入 以元祖的形式传参数 就是添加一次提交一次
def mul_mode(self, sql, args):
# self.cursor.executemany("insert into user (id,name) values (%s,%s)",[(1,"aaa"),(2,"bbb"),(3,"ccc")]) 传参方式
self.cursor.executemany(sql, args)
self.conn.commit()
def get_close(self):
self.cursor.close()
self.conn.close()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
希望与广大网友互动??
点此进行留言吧!
python 封装对象数据_Python数据库封装实现代码示例解析相关推荐
- python中config命令_Python config.config方法代码示例
本文整理汇总了Python中config.config方法的典型用法代码示例.如果您正苦于以下问题:Python config.config方法的具体用法?Python config.config怎么 ...
- python连接redis哨兵_Python redis.sentinel方法代码示例
本文整理汇总了Python中redis.sentinel方法的典型用法代码示例.如果您正苦于以下问题:Python redis.sentinel方法的具体用法?Python redis.sentine ...
- python程序异常实例_Python werkzeug.exceptions方法代码示例
本文整理汇总了Python中werkzeug.exceptions方法的典型用法代码示例.如果您正苦于以下问题:Python werkzeug.exceptions方法的具体用法?Python wer ...
- python列表get方法_Python json.get方法代码示例
本文整理汇总了Python中json.get方法的典型用法代码示例.如果您正苦于以下问题:Python json.get方法的具体用法?Python json.get怎么用?Python json.g ...
- python modifysetup什么意思_Python pyinotify.IN_MODIFY属性代码示例
本文整理汇总了Python中pyinotify.IN_MODIFY属性的典型用法代码示例.如果您正苦于以下问题:Python pyinotify.IN_MODIFY属性的具体用法?Python pyi ...
- python re 简单实例_Python re.search方法代码示例
本文整理汇总了Python中re.search方法的典型用法代码示例.如果您正苦于以下问题:Python re.search方法的具体用法?Python re.search怎么用?Python re. ...
- python tkinter insert函数_Python tkinter.INSERT属性代码示例
本文整理汇总了Python中tkinter.INSERT属性的典型用法代码示例.如果您正苦于以下问题:Python tkinter.INSERT属性的具体用法?Python tkinter.INSER ...
- python messagebox弹窗退出_Python messagebox.showinfo方法代码示例
本文整理汇总了Python中tkinter.messagebox.showinfo方法的典型用法代码示例.如果您正苦于以下问题:Python messagebox.showinfo方法的具体用法?Py ...
- python中geometry用法_Python geometry.Point方法代码示例
本文整理汇总了Python中shapely.geometry.Point方法的典型用法代码示例.如果您正苦于以下问题:Python geometry.Point方法的具体用法?Python geome ...
最新文章
- 希望增加的BLOG功能(序)
- iptables总结
- 【Selenium】导出成py脚本的基础使用
- Web框架之Django_01初识(三大主流web框架、Django安装、Django项目创建方式及其相关配置、Django基础三件套:HttpResponse、render、redirect)...
- 计组之数据运算:6、原码乘法运算
- Web测试中新增修改功能测试
- 通过JS函数劫持自定义百度云分享提取码
- java 哈希表和向量_Java基础知识笔记(一:修饰词、向量、哈希表)
- 【SICP练习】150 练习4.6
- geotools将shp数据存入postgres
- html中css字体颜色代码大全,css字体颜色的设置方法
- 饥荒如何修改服务器人数上限,《饥荒联机版》房间人数上限修改图文教程
- PHP微信公众号授权登录
- Python数组索引
- 新世纪大学英语(第二版)综合教程第一册 Unit 1 (中英翻译和重点单词)
- ios string转dic_如何在Swift(iOS,Xcode)中将NSDictionary转换为Json String?
- 在socket 中使用域名
- android 百度云语音,手机安卓百度云AI智能之语音合成
- 【5. Redis的高并发高可用】
- java16进制字符串与字符串互相转换