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使用os模块的os.walk遍历文件夹示例
Jan 27 Python
Python制作数据导入导出工具
Jul 31 Python
json跨域调用python的方法详解
Jan 11 Python
python虚拟环境virtualenv的使用教程
Oct 20 Python
Python图形绘制操作之正弦曲线实现方法分析
Dec 25 Python
Python中的pack和unpack的使用
Mar 12 Python
详解TensorFlow查看ckpt中变量的几种方法
Jun 19 Python
Python异常的检测和处理方法
Oct 26 Python
在Pandas中DataFrame数据合并,连接(concat,merge,join)的实例
Jan 29 Python
如何更改 pandas dataframe 中两列的位置
Dec 27 Python
pandas求平均数和中位数的方法实例
Aug 04 Python
浅谈Python中对象是如何被调用的
Apr 06 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 页面跳转到另一个页面的多种方法方法总结
2009/07/07 PHP
php实现文件下载功能的几个代码分享
2014/05/10 PHP
thinkPHP中钩子的两种配置调用方法详解
2016/11/11 PHP
Yii框架Session与Cookie使用方法示例
2019/10/14 PHP
js电信网通双线自动选择技巧
2008/11/18 Javascript
Javascript日期对象的dateAdd与dateDiff方法
2008/11/18 Javascript
关于javascript document.createDocumentFragment()
2009/04/04 Javascript
js 实现菜单左右滚动显示示例介绍
2013/11/21 Javascript
JavaScript中读取和保存文件实例
2014/05/08 Javascript
jQuery实现动态添加和删除一个div
2015/08/12 Javascript
jQuery实现宽屏图片轮播实例教程
2015/11/24 Javascript
AngularJS 遇到的小坑与技巧小结
2016/06/07 Javascript
jquery获取table指定行和列的数据方法(当前选中行、列)
2016/11/07 Javascript
如何理解Vue的v-model指令的使用方法
2018/07/19 Javascript
layer实现登录弹框,登录成功后关闭弹框并调用父窗口的例子
2019/09/11 Javascript
小程序接入腾讯位置服务的详细流程
2020/03/03 Javascript
JavaScript设计模式之策略模式实现原理详解
2020/05/29 Javascript
Vue自动构建发布脚本的方法示例
2020/07/24 Javascript
python编码总结(编码类型、格式、转码)
2016/07/01 Python
浅谈Python采集网页时正则表达式匹配换行符的问题
2018/12/20 Python
Python使用pymysql模块操作mysql增删改查实例分析
2019/12/19 Python
Python3.x+迅雷x 自动下载高分电影的实现方法
2020/01/12 Python
在keras下实现多个模型的融合方式
2020/05/23 Python
Python实现http接口自动化测试的示例代码
2020/10/09 Python
python中的列表和元组区别分析
2020/12/30 Python
CSS3 优势以及网页设计师如何使用CSS3技术
2009/07/29 HTML / CSS
英国奢侈品牌时尚购物平台:Farfetch(支持中文)
2020/02/18 全球购物
杭州SQL浙江浙大网新恩普软件有限公司
2013/07/27 面试题
区域销售经理岗位职责
2013/12/10 职场文书
《孔子游春》教学反思
2014/02/25 职场文书
党员岗位承诺口号大全
2014/03/28 职场文书
遗产继承公证书
2014/04/09 职场文书
教师评语大全
2014/04/28 职场文书
2014年社区卫生工作总结
2014/12/18 职场文书
外贸业务员岗位职责
2015/02/13 职场文书
离婚协议书范文2016
2016/03/18 职场文书