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进程类subprocess的一些操作方法例子
Nov 22 Python
Python3.2中的字符串函数学习总结
Apr 23 Python
python数据结构之图的实现方法
Jul 08 Python
linux环境下python中MySQLdb模块的安装方法
Jun 16 Python
基于tensorflow加载部分层的方法
Jul 26 Python
python正向最大匹配分词和逆向最大匹配分词的实例
Nov 14 Python
Python openpyxl 遍历所有sheet 查找特定字符串的方法
Dec 10 Python
Python实现定时自动关闭的tkinter窗口方法
Feb 16 Python
python数据挖掘需要学的内容
Jun 23 Python
Python类反射机制使用实例解析
Dec 30 Python
pytorch加载自定义网络权重的实现
Jan 07 Python
Python使用MapReduce进行简单的销售统计
Apr 22 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/04/10 PHP
php 8小时时间差的解决方法小结
2009/12/22 PHP
如何突破PHP程序员的技术瓶颈分析
2011/07/17 PHP
php实现的Captcha验证码类实例
2014/09/22 PHP
Symfony的安装和配置方法
2016/03/17 PHP
php5.3后静态绑定用法详解
2016/11/11 PHP
thinkphp 抓取网站的内容并且保存到本地的实例详解
2017/08/25 PHP
解决FLASH需要点击激活的代码
2006/12/20 Javascript
Js实现当前点击a标签变色突出显示其他a标签回复原色
2013/11/27 Javascript
jQuery内容折叠效果插件用法实例分析(附demo源码)
2016/04/28 Javascript
浅析Javascript中bind()方法的使用与实现
2016/04/29 Javascript
jQuery电话号码验证实例
2017/01/05 Javascript
JS实现加载和读取XML文件的方法详解
2017/04/24 Javascript
ExtJs的Ext.Ajax.request实现waitMsg等待提示效果
2017/06/14 Javascript
json2.js 入门教程之使用方法与实例分析
2017/09/14 Javascript
使用vue中的v-for遍历二维数组的方法
2018/03/07 Javascript
Angular4.x通过路由守卫进行路由重定向实现根据条件跳转到相应的页面(推荐)
2018/05/10 Javascript
node中modules.exports与exports导出的区别
2018/06/08 Javascript
在 vue-cli v3.0 中使用 SCSS/SASS的方法
2018/06/14 Javascript
vue框架下部署上线后刷新报404问题的解决方案(推荐)
2019/04/03 Javascript
jquery实现购物车基本功能
2019/10/25 jQuery
使用pdb模块调试Python程序实例
2015/06/02 Python
Python3.4 tkinter,PIL图片转换
2018/06/21 Python
python+mysql实现教务管理系统
2019/02/20 Python
pandas中的series数据类型详解
2019/07/06 Python
Python 正则表达式 re.match/re.search/re.sub的使用解析
2019/07/22 Python
OpenCV3.0+Python3.6实现特定颜色的物体追踪
2019/07/23 Python
Python 列表推导式需要注意的地方
2020/10/23 Python
在C++ 程序中调用被C 编译器编译后的函数,为什么要加extern "C"
2014/08/09 面试题
计算机专业毕业生求职信分享
2013/12/24 职场文书
奶茶专卖店创业计划书
2014/01/18 职场文书
幼儿园中班开学寄语
2014/04/03 职场文书
企业文化口号
2014/06/12 职场文书
党支部群众路线整改措施思想汇报
2014/10/10 职场文书
这样写python注释让代码更加的优雅
2021/06/02 Python
python APScheduler执行定时任务介绍
2022/04/19 Python