Python Sql数据库增删改查操作简单封装
admin
2023-08-02 15:31:57
0

本文实例为大家分享了如何利用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数据库增删改查操作的相关操作,希望对大家的学习有所帮助。

相关内容

热门资讯

500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
python查找阿姆斯特朗数 题目解释 如果一个n位正整数等于其各位数字的n次方之和,则称该数为阿姆斯特朗数。 例如1^3 + 5...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...