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 相关文章推荐
详解python开发环境搭建
Dec 16 Python
Python Socket传输文件示例
Jan 16 Python
python利用正则表达式排除集合中字符的功能示例
Oct 10 Python
如何优雅地改进Django中的模板碎片缓存详解
Jul 04 Python
caffe binaryproto 与 npy相互转换的实例讲解
Jul 09 Python
浅谈python下tiff图像的读取和保存方法
Dec 04 Python
浅谈PYTHON 关于文件的操作
Mar 19 Python
Python hexstring-list-str之间的转换方法
Jun 12 Python
python 梯度法求解函数极值的实例
Jul 10 Python
python 实现GUI(图形用户界面)编程详解
Jul 17 Python
详解用Python爬虫获取百度企业信用中企业基本信息
Jul 02 Python
如何卸载python插件
Jul 08 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
PHP 文件上传全攻略
2010/04/28 PHP
smarty中英文多编码字符截取乱码问题解决方法
2014/10/28 PHP
php检测图片主要颜色的方法
2015/07/01 PHP
php从数据库读取数据,并以json格式返回数据的方法
2018/08/21 PHP
JS 自动安装exe程序
2008/11/30 Javascript
jQuery插件 tabBox实现代码
2010/02/09 Javascript
Javascript中获取出错代码所在文件及行数的代码
2010/09/23 Javascript
Jquery实现鼠标移上弹出提示框、移出消失思路及代码
2013/05/19 Javascript
JS复制到剪贴板示例代码
2013/10/30 Javascript
js监听鼠标点击和键盘点击事件并自动跳转页面
2014/09/24 Javascript
15个常用的jquery代码片段
2015/12/19 Javascript
JS/jQuery判断DOM节点是否存在的简单方法
2016/11/24 Javascript
Bootstrap CSS组件之面包屑导航(breadcrumb)
2016/12/17 Javascript
Vue-router路由判断页面未登录跳转到登录页面的实例
2017/10/26 Javascript
Webpack 4.x搭建react开发环境的方法步骤
2018/08/15 Javascript
vue展示dicom文件医疗系统的实现代码
2018/08/27 Javascript
Vue.js特性Scoped Slots的浅析
2019/02/20 Javascript
微信小程序实现的canvas合成图片功能示例
2019/05/03 Javascript
在Linux下使用Python的matplotlib绘制数据图的教程
2015/06/11 Python
Python中的descriptor描述器简明使用指南
2016/06/02 Python
详解Python中的文件操作
2016/08/28 Python
Python实现的爬虫刷回复功能示例
2018/06/07 Python
python 给DataFrame增加index行名和columns列名的实现方法
2018/06/08 Python
python实现简单图片物体标注工具
2019/03/18 Python
修改 CentOS 6.x 上默认Python的方法
2019/09/06 Python
pandas分组聚合详解
2020/04/10 Python
python 装饰器的使用示例
2020/10/10 Python
Python实现简单的2048小游戏
2021/03/01 Python
澳大利高级泳装品牌:Bondi Born
2018/05/23 全球购物
应届生船舶驾驶求职信
2013/10/19 职场文书
肯尼迪就职演说稿
2013/12/31 职场文书
党员教师四风问题整改措施思想汇报
2014/10/08 职场文书
信用卡工作证明范本
2015/06/19 职场文书
JS封装cavans多种滤镜组件
2022/02/15 Javascript
SQL Server #{}可以防止SQL注入
2022/05/11 SQL Server
详解Go语言中Get/Post请求测试
2022/06/01 Golang