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中for循环和while循环的基本使用方法
Aug 21 Python
结合Python的SimpleHTTPServer源码来解析socket通信
Jun 27 Python
Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法
Jan 12 Python
Python冲顶大会 快来答题!
Jan 17 Python
Python测试人员需要掌握的知识
Feb 08 Python
手把手教你如何安装Pycharm(详细图文教程)
Nov 28 Python
Django Channels 实现点对点实时聊天和消息推送功能
Jul 17 Python
django-rest-framework解析请求参数过程详解
Jul 18 Python
基于python的selenium两种文件上传操作实现详解
Sep 19 Python
Python创建临时文件和文件夹
Aug 05 Python
浅谈如何使用python抓取网页中的动态数据实现
Aug 17 Python
python opencv实现直线检测并测出倾斜角度(附源码+注释)
Dec 31 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
世界第一个无线广播电台 KDKA
2021/03/01 无线电
深入了解PHP类Class的概念
2012/06/14 PHP
web站点获取用户IP的安全方法 HTTP_X_FORWARDED_FOR检验
2013/06/01 PHP
Zend Framework动作控制器用法示例
2016/12/09 PHP
Laravel如何同时连接多个数据库详解
2019/08/13 PHP
动态添加js事件实现代码
2009/03/12 Javascript
Javascript实现CheckBox的全选与取消全选的代码
2010/07/20 Javascript
Javascript图像处理—为矩阵添加常用方法
2012/12/27 Javascript
js控制不同的时间段显示不同的css样式的实例代码
2013/11/04 Javascript
jQuery插件pagination实现分页特效
2015/04/12 Javascript
JSON 必知必会 观后记
2016/10/27 Javascript
深入浅出理解JavaScript闭包的功能与用法
2018/08/01 Javascript
微信小程序实现预览图片功能
2020/10/22 Javascript
JS实现头条新闻的经典轮播图效果示例
2019/01/30 Javascript
微信小程序 swiper 组件遇到的问题及解决方法
2019/05/26 Javascript
微信小程序基础教程之worker线程的使用方法
2019/07/15 Javascript
layui添加动态菜单与选项卡 AJAX请求的例子
2019/09/25 Javascript
vue+ts下对axios的封装实现
2020/02/18 Javascript
jquery实现抽奖功能
2020/10/22 jQuery
利用vue3+ts实现管理后台(增删改查)
2020/10/30 Javascript
vue调用微信JSDK 扫一扫,相册等需要注意的事项
2021/01/03 Vue.js
[00:57]辉夜杯战队访谈宣传片—VG
2015/12/25 DOTA
Python查询Mysql时返回字典结构的代码
2012/06/18 Python
Python3简单实例计算同花的概率代码
2017/12/06 Python
python实现nao机器人手臂动作控制
2019/04/29 Python
python+jinja2实现接口数据批量生成工具
2019/08/28 Python
PyQt5 界面显示无响应的实现
2020/03/26 Python
使用python无账号无限制获取企查查信息的实例代码
2020/04/17 Python
利用简洁的图片预加载组件提升html5移动页面的用户体验
2016/03/11 HTML / CSS
html5理解head_动力节点Java学院整理
2017/07/13 HTML / CSS
退休感言
2014/01/28 职场文书
《盘古开天地》教学反思
2014/02/28 职场文书
团支书竞选演讲稿
2014/04/28 职场文书
乔迁之喜答谢词
2015/01/05 职场文书
物业前台接待岗位职责
2015/04/03 职场文书
宣传委员竞选稿
2015/11/19 职场文书