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中使用Tkinter模块创建GUI程序实例
Jan 14 Python
python3实现抓取网页资源的 N 种方法
May 02 Python
Python3实现的简单验证码识别功能示例
May 02 Python
对Python 窗体(tkinter)文本编辑器(Text)详解
Oct 11 Python
django orm 通过related_name反向查询的方法
Dec 15 Python
对Python闭包与延迟绑定的方法详解
Jan 07 Python
Python File(文件) 方法整理
Feb 18 Python
Python3利用print输出带颜色的彩色字体示例代码
Apr 08 Python
Python Pandas中根据列的值选取多行数据
Jul 08 Python
微信公众号token验证失败解决方案
Jul 22 Python
python elasticsearch环境搭建详解
Sep 02 Python
容易被忽略的Python内置类型
Sep 03 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
星际争霸兵种名称对照表
2020/03/04 星际争霸
外媒评选出10支2020年最受欢迎的Dota2战队
2021/03/05 DOTA
PHP设计模式之责任链模式的深入解析
2013/06/13 PHP
解决nginx不支持thinkphp中pathinfo的问题
2015/07/21 PHP
如何修改yii2.0自带的user表为其它的表
2017/08/01 PHP
BOOM vs RR BO3 第一场2.13
2021/03/10 DOTA
利用jQuery简单实现产品展示图片左右滚动功能(示例代码)
2014/01/02 Javascript
JS实现点击文字对应DIV层不停闪动效果的方法
2015/03/02 Javascript
jQuery满屏焦点图左右滚动特效代码分享
2015/09/07 Javascript
jquery拖拽排序简单实现方法(效果增强版)
2016/02/16 Javascript
JS获取html元素的标记名实现方法
2016/10/08 Javascript
浅谈jQuery before和insertBefore的区别
2016/12/04 Javascript
JavaScript编写一个贪吃蛇游戏
2017/03/09 Javascript
jQuery Form插件使用详解_动力节点Java学院整理
2017/07/17 jQuery
js中的闭包学习心得
2018/02/06 Javascript
使用ng-packagr打包Angular的方法示例
2018/09/21 Javascript
微信小程序获取用户信息并保存登录状态详解
2019/05/10 Javascript
微信小程序常用的3种提示弹窗实现详解
2019/09/19 Javascript
在Vue项目中,防止页面被缩放和放大示例
2019/10/28 Javascript
JavaScript实现简单进度条效果
2020/03/25 Javascript
python下载文件记录黑名单的实现代码
2017/10/24 Python
python with提前退出遇到的坑与解决方案
2018/01/05 Python
python命令行解析之parse_known_args()函数和parse_args()使用区别介绍
2018/01/24 Python
Windows下Python3.6安装第三方模块的方法
2018/11/22 Python
在Python中通过getattr获取对象引用的方法
2019/01/21 Python
Python语法分析之字符串格式化
2019/06/13 Python
Python单元测试模块doctest的具体使用
2020/02/10 Python
Python连接Hadoop数据中遇到的各种坑(汇总)
2020/04/14 Python
使用tensorflow进行音乐类型的分类
2020/08/14 Python
意大利值得信赖的在线超级药房:PillolaStore
2020/02/05 全球购物
叙述DBMS对数据控制功能有哪些
2016/06/12 面试题
货代行业个人求职简历的自我评价
2013/10/22 职场文书
《识字五》教学反思
2014/03/01 职场文书
2015试用期转正工作总结
2014/12/12 职场文书
公司禁烟通知
2015/04/23 职场文书
解决goland 导入项目后import里的包报红问题
2021/05/06 Golang