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图像处理之反色实现方法
May 30 Python
Python实现随机生成有效手机号码及身份证功能示例
Jun 05 Python
Python从Excel中读取日期一列的方法
Nov 28 Python
浅谈Python中函数的定义及其调用方法
Jul 19 Python
python requests更换代理适用于IP频率限制的方法
Aug 21 Python
Pandas时间序列基础详解(转换,索引,切片)
Feb 26 Python
django使用F方法更新一个对象多个对象字段的实现
Mar 28 Python
基于Python制作一副扑克牌过程详解
Oct 19 Python
pycharm2020.1.2永久破解激活教程,实测有效
Oct 29 Python
python实现web邮箱扫描的示例(附源码)
Mar 30 Python
pytorch 如何使用batch训练lstm网络
May 28 Python
Python内置包对JSON文件数据进行编码和解码
Apr 12 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
PHP下一个非常全面获取图象信息的函数
2008/11/20 PHP
详解php的魔术方法__get()和__set()使用介绍
2012/09/19 PHP
支付宝接口开发集成支付环境小结
2015/03/17 PHP
Nigma vs Alliance BO5 第三场2.14
2021/03/10 DOTA
javascript jQuery $.post $.ajax用法
2008/07/09 Javascript
javascript 装载iframe子页面,自适应高度
2009/03/20 Javascript
JQ获取动态加载的图片大小的正确方法分享
2013/11/08 Javascript
javascript中parseInt()函数的定义和用法分析
2014/12/20 Javascript
JS实现从表格中动态删除指定行的方法
2015/03/31 Javascript
js实现带按钮的上下滚动效果
2015/05/12 Javascript
jQuery实现网站添加高亮突出显示效果的方法
2015/06/26 Javascript
jquery之别踩白块游戏的简单实现
2016/07/25 Javascript
JavaScript对象封装的简单实现方法(3种方法)
2017/01/03 Javascript
vue多个元素的样式选择器问题
2019/11/29 Javascript
vue项目中自定义video视频控制条的实现代码
2020/04/26 Javascript
jquery绑定事件 bind和on的用法与区别分析
2020/05/22 jQuery
python Web开发你要理解的WSGI & uwsgi详解
2018/08/01 Python
Django 项目重命名的实现步骤解析
2019/08/14 Python
pyqt5 QlistView列表显示的实现示例
2020/03/24 Python
Python HTMLTestRunner库安装过程解析
2020/05/25 Python
Python unittest discover批量执行代码实例
2020/09/08 Python
世界上最大的街头服饰网站:Karmaloop
2017/02/04 全球购物
美国轮胎网站:Priority Tire
2018/11/28 全球购物
个人教师自我评价范文
2013/12/02 职场文书
一份婚庆公司创业计划书
2014/01/11 职场文书
初三学生评语大全
2014/04/24 职场文书
2014年综治维稳工作总结
2014/11/17 职场文书
防暑降温通知书
2015/04/27 职场文书
怒海潜将观后感
2015/06/11 职场文书
2015年小学教科研工作总结
2015/07/20 职场文书
SQL Server连接查询的实用教程
2021/04/07 SQL Server
web前端之css水平居中代码解析
2021/05/20 HTML / CSS
Django与数据库交互的实现
2021/06/03 Python
nginx安装以及配置的详细过程记录
2021/09/15 Servers
微信小程序APP的事件绑定以及传递参数时的冒泡和捕获
2022/04/19 Javascript