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

作者:mrmusic 时间:2024-01-22 14:47:37 

本文实例为大家分享了如何利用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,数据库,增删改查
0
投稿

猜你喜欢

  • Pandas处理DataFrame稀疏数据及维度不匹配数据分析详解

    2023-08-20 02:44:38
  • 基于python实现数组格式参数加密计算

    2021-08-20 23:29:54
  • Python OpenCV简单的绘图函数使用教程

    2023-08-02 23:22:22
  • SQLServer2019 数据库环境搭建与使用的实现

    2024-01-16 12:45:48
  • 如何用ASP获知机器的网络配置?

    2010-06-11 19:58:00
  • MySQL 数据编码 latin1 转 UTF8

    2010-10-14 14:20:00
  • Oracle学习笔记(五)

    2024-01-25 04:12:09
  • 解决vue单页面多个组件嵌套监听浏览器窗口变化问题

    2024-04-27 15:48:29
  • Pygame实战之实现扎气球游戏

    2023-06-17 10:17:43
  • Python运算符优先级详细整理

    2021-12-17 06:38:14
  • 查看Oracle的执行计划一句话命令

    2010-07-16 13:02:00
  • Python NumPy教程之数据类型对象详解

    2022-03-29 09:05:19
  • Python字典,函数,全局变量代码解析

    2021-02-20 06:58:58
  • ASP字符串大写转换成小写 ASP小写转换成大写 ucase lcase

    2011-03-31 10:59:00
  • python_mask_array的用法

    2022-05-10 06:25:27
  • 如何修改vue-treeSelect的高度

    2024-05-08 09:33:55
  • Python自动化办公之Excel拆分与自动发邮件

    2022-05-03 09:05:40
  • Mysql systemctl start mysqld报错的问题解决

    2024-01-26 03:41:45
  • Python处理excel与txt文件详解

    2021-12-13 20:38:20
  • python学习笔记之调用eval函数出现invalid syntax错误问题

    2023-11-03 01:48:30
  • asp之家 网络编程 m.aspxhome.com