Python Sql数据库增删改查操作简单封装

 更新时间:2016年04月18日 14:35:05   作者:mrmusic  
这篇文章主要为大家介绍了Python Sql数据库增删改查操作简单封装,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了如何利用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读取.text文件特定行的数据方法

    使用python读取.text文件特定行的数据方法

    今天小编就为大家分享一篇使用python读取.text文件特定行的数据方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • pyqt 多窗口之间的相互调用方法

    pyqt 多窗口之间的相互调用方法

    今天小编就为大家分享一篇pyqt 多窗口之间的相互调用方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • pandas实现datetime64与unix时间戳互转

    pandas实现datetime64与unix时间戳互转

    这篇文章主要介绍了pandas实现datetime64与unix时间戳互转,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • python通过scapy获取局域网所有主机mac地址示例

    python通过scapy获取局域网所有主机mac地址示例

    这篇文章主要介绍了python通过scapy获取局域网所有主机mac地址示例,需要的朋友可以参考下
    2014-05-05
  • pandas combine_first函数处理两个数据集重叠和缺失

    pandas combine_first函数处理两个数据集重叠和缺失

    combine_first是pandas中的一个函数,它可以将两个DataFrame对象按照索引进行合并,用一个对象中的非空值填充另一个对象中的空值,这个函数非常适合处理两个数据集有部分重叠和缺失的情况,可以实现数据的补全和更新,本文介绍combine_first函数的语法及一些案例应用
    2024-01-01
  • Django Rest Framework实现身份认证源码详解

    Django Rest Framework实现身份认证源码详解

    这篇文章主要为大家介绍了Django Rest Framework实现身份认证源码详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • virtualenv实现多个版本Python共存

    virtualenv实现多个版本Python共存

    virtualenv用于创建独立的Python环境,多个Python相互独立,互不影响,它能够:1. 在没有权限的情况下安装新套件 2. 不同应用可以使用不同的套件版本 3. 套件升级不影响其他应用
    2017-08-08
  • 微信跳一跳python代码实现

    微信跳一跳python代码实现

    这篇文章主要为大家详细介绍了微信跳一跳python代码实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Python Cloudinary实现图像和视频上传详解

    Python Cloudinary实现图像和视频上传详解

    这篇文章主要介绍了Python Cloudinary实现图像和视频上传功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-11-11
  • Pytorch中的backward()多个loss函数用法

    Pytorch中的backward()多个loss函数用法

    这篇文章主要介绍了Pytorch中的backward()多个loss函数用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05

最新评论