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使用正则搜索字符串或文件中的浮点数代码实例
Jul 11 Python
Python字符串特性及常用字符串方法的简单笔记
Jan 04 Python
Python 中的Selenium异常处理实例代码
May 03 Python
Flask框架Flask-Principal基本用法实例分析
Jul 23 Python
通过python爬虫赚钱的方法
Jan 29 Python
python scatter散点图用循环分类法加图例
Mar 19 Python
Django框架模板语言实例小结【变量,标签,过滤器,继承,html转义】
May 23 Python
python基于Selenium的web自动化框架
Jul 14 Python
Python依赖包整体迁移方法详解
Aug 15 Python
解决flask接口返回的内容中文乱码的问题
Apr 03 Python
Python pathlib模块使用方法及实例解析
Oct 05 Python
Python如何导出导入所有依赖包详解
Jun 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中对数据库操作的封装
2006/10/09 PHP
global.php
2006/12/09 PHP
用PHP实现 上一篇、下一篇的代码
2012/09/29 PHP
PHP中使用localhost连接Mysql不成功的解决方法
2014/08/20 PHP
Symfony2安装第三方Bundles实例详解
2016/02/04 PHP
PHP小偷程序的设计与实现方法详解
2016/10/15 PHP
动态表单验证的操作方法和TP框架里面的ajax表单验证
2017/07/19 PHP
Laravel实现定时任务的示例代码
2017/08/10 PHP
php 将json格式数据转换成数组的方法
2018/08/21 PHP
PHP获取访问设备信息的方法示例
2019/02/20 PHP
php用户名的密码加密更安全的方法
2019/06/21 PHP
php设计模式之建造器模式分析【星际争霸游戏案例】
2020/01/23 PHP
php使用pthreads v3多线程实现抓取新浪新闻信息操作示例
2020/02/21 PHP
JavaScript利用append添加元素报错的解决方法
2014/07/01 Javascript
jQuery实现鼠标点击弹出渐变层的方法
2015/07/09 Javascript
在WordPress中加入Google搜索功能的简单步骤讲解
2016/01/04 Javascript
JS基于ocanvas插件实现的简单画板效果代码(附demo源码下载)
2016/04/05 Javascript
javascript实现延时显示提示框特效代码
2016/04/27 Javascript
浅谈jQuery 选择器和dom操作
2016/06/07 Javascript
JS实现touch 点击滑动轮播实例代码
2017/01/19 Javascript
jQuery plugin animsition使用小结
2017/09/14 jQuery
webpack css加载和图片加载的方法示例
2018/09/11 Javascript
超简单的微信小程序轮播图
2019/11/22 Javascript
vue自定义插件封装,实现简易的elementUi的Message和MessageBox的示例
2020/11/20 Vue.js
vue表单验证之禁止input输入框输入空格
2020/12/03 Vue.js
Python通过递归遍历出集合中所有元素的方法
2015/02/25 Python
Python用threading实现多线程详解
2017/02/03 Python
python实现微信跳一跳辅助工具步骤详解
2018/01/04 Python
使用Python实现分别输出每个数组
2019/12/06 Python
python实现异常信息堆栈输出到日志文件
2019/12/26 Python
Python基础类继承重写实现原理解析
2020/04/03 Python
收集的22款给力的HTML5和CSS3帮助工具
2012/09/14 HTML / CSS
罚站检讨书
2015/01/29 职场文书
行政撤诉申请书
2015/05/18 职场文书
PostgreSQL聚合函数介绍以及分组和排序
2022/04/12 PostgreSQL
详解Go语言中配置文件使用与日志配置
2022/06/01 Golang