Python Sql数据库增删改查操作简单封装


Posted in Python onApril 18, 2016

本文实例为大家分享了如何利用Python对数据库的增删改查进行简单的封装,供大家参考,具体内容如下

1.insert    

import mysql.connector
import os
import codecs
#设置数据库用户名和密码
user='root';#用户名
pwd='root';#密码
host='localhost';#ip地址
db='mysql';#所要操作数据库名字
charset='UTF-8'
cnx = mysql.connector.connect(user=user,password=pwd, host=host, database=db)
#设置游标
cursor = cnx.cursor(dictionary=True)
#插入数据
#print(insert('gelixi_help_type',{'type_name':'\'sddfdsfs\'','type_sort':'283'}))
def insert(table_name,insert_dict):
  param='';
  value='';
  if(isinstance(insert_dict,dict)):
    for key in insert_dict.keys():
      param=param+key+","
      value=value+insert_dict[key]+','
    param=param[:-1]
    value=value[:-1]
  sql="insert into %s (%s) values(%s)"%(table_name,param,value)
  cursor.execute(sql)
  id=cursor.lastrowid
  cnx.commit()
  return id

2.delete    

def delete(table_name,where=''):
  if(where!=''):
    str='where'
    for key_value in where.keys():
      value=where[key_value]
      str=str+' '+key_value+'='+value+' '+'and'
    where=str[:-3]
    sql="delete from %s %s"%(table_name,where)
    cursor.execute(sql)
    cnx.commit()

3.select    

#取得数据库信息
# print(select({'table':'gelixi_help_type','where':{'help_show': '1'}},'type_name,type_id'))
def select(param,fields='*'):
  table=param['table']
  if('where' in param):
    thewhere=param['where']
    if(isinstance (thewhere,dict)):
      keys=thewhere.keys()
      str='where';
      for key_value in keys:
        value=thewhere[key_value]
        str=str+' '+key_value+'='+value+' '+'and'
      where=str[:-3]
  else:
    where=''
  sql="select %s from %s %s"%(fields,table,where)
  cursor.execute(sql)
  result=cursor.fetchall()
  return result

4.showtable,showcolumns    

#显示建表语句
#table string 表名
#return string 建表语句
def showCreateTable(table):
  sql='show create table %s'%(table)
  cursor.execute(sql)
  result=cursor.fetchall()[0]
  return result['Create Table']
#print(showCreateTable('gelixi_admin'))
#显示表结构语句
def showColumns(table):
  sql='show columns from %s '%(table)
  print(sql)
  cursor.execute(sql)
  result=cursor.fetchall()
  dict1={}
  for info in result:
    dict1[info['Field']]=info
  return dict1

以上就是Python Sql数据库增删改查操作的相关操作,希望对大家的学习有所帮助。

Python 相关文章推荐
分享15个最受欢迎的Python开源框架
Jul 13 Python
python通过pil模块将raw图片转换成png图片的方法
Mar 16 Python
Python中利用sorted()函数排序的简单教程
Apr 27 Python
python实现下载指定网址所有图片的方法
Aug 08 Python
浅谈scrapy 的基本命令介绍
Jun 13 Python
python实现将读入的多维list转为一维list的方法
Jun 28 Python
利用python打开摄像头及颜色检测方法
Aug 03 Python
django2.0扩展用户字段示例
Feb 13 Python
python实现跨excel sheet复制代码实例
Mar 03 Python
Python使用plt.boxplot() 参数绘制箱线图
Jun 04 Python
Python 解决相对路径问题:"No such file or directory"
Jun 05 Python
Python编程编写完善的命令行工具
Sep 15 Python
python使用paramiko实现远程拷贝文件的方法
Apr 18 #Python
python UNIX_TIMESTAMP时间处理方法分析
Apr 18 #Python
python动态加载包的方法小结
Apr 18 #Python
python实现按行切分文本文件的方法
Apr 18 #Python
Python获取linux主机ip的简单实现方法
Apr 18 #Python
Python实现递归遍历文件夹并删除文件
Apr 18 #Python
Python简单实现TCP包发送十六进制数据的方法
Apr 16 #Python
You might like
关于laravel框架中的常用目录路径函数
2019/10/23 PHP
Jquery.TreeView结合ASP.Net和数据库生成菜单导航条
2010/08/27 Javascript
深入理解JavaScript系列(6):S.O.L.I.D五大原则之单一职责SRP
2012/01/15 Javascript
简单实用jquery版三级联动select示例
2013/07/04 Javascript
Textarea根据内容自适应高度
2013/10/28 Javascript
使用JQuery快速实现Tab的AJAX动态载入(实例讲解)
2013/12/11 Javascript
css3元素简单的闪烁效果实现(html5 jquery)
2013/12/28 Javascript
回车直接实现点击某按钮的效果即触发单击事件
2014/02/27 Javascript
Javascript学习指南
2014/12/01 Javascript
jQuery模拟原生态App上拉刷新下拉加载更多页面及原理
2015/08/10 Javascript
jQuery调用WebMethod(PageMethod) NET2.0的方法
2016/04/15 Javascript
js select实现省市区联动选择
2020/04/17 Javascript
textarea 在浏览器中固定大小和禁止拖动的实现方法
2016/12/03 Javascript
AngularJS通过ng-route实现基本的路由功能实例详解
2016/12/13 Javascript
JS实现微信里判断页面是否被分享成功的方法
2017/06/06 Javascript
Vue多环境代理配置方法思路详解
2019/06/21 Javascript
python中精确输出JSON浮点数的方法
2014/04/18 Python
python使用marshal模块序列化实例
2014/09/25 Python
python3 图片referer防盗链的实现方法
2018/03/12 Python
python实现txt文件格式转换为arff格式
2018/05/31 Python
使用Python的Dataframe取两列时间值相差一年的所有行方法
2018/07/10 Python
python简单验证码识别的实现方法
2019/05/10 Python
Python hashlib模块加密过程解析
2019/11/05 Python
NumPy统计函数的实现方法
2020/01/21 Python
django-csrf使用和禁用方式
2020/03/13 Python
keras分类之二分类实例(Cat and dog)
2020/07/09 Python
手把手教你从PyCharm安装到激活(最新激活码),亲测有效可激活至2089年
2020/11/25 Python
加拿大鞋子连锁店:Town Shoes
2016/09/26 全球购物
皇家阿尔伯特英国官方商店:Royal Albert骨瓷
2019/03/25 全球购物
美体小铺奥地利官方网站:The Body Shop奥地利
2019/04/11 全球购物
英国家居用品和床上用品零售商:P&B Home
2020/01/16 全球购物
财务科长个人对照检查材料
2014/09/18 职场文书
2015年反腐倡廉工作总结
2015/05/14 职场文书
2019最新企业员工考勤管理制度(通用版)!
2019/07/02 职场文书
python中sys模块的介绍与实例
2021/04/17 Python
Python正则表达式中flags参数的实例详解
2022/04/01 Python