Python Sql数据库增删改查操作简单封装
作者:mrmusic 发布时间:2024-01-22 14:47:37
标签: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
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 什么是可变/不可变对象不可变对象,该对象所指向的内存中的值不能被改变。当改变某个变量时候,由于其所指的值不能被改变,相当于把原来的值复制一份
- 写一个爬虫首先就是学会设置请求头header,这样才可以伪装成浏览器。下面小编我就来给大家简单分析一下python3怎样构建一个爬虫的请求头
- 将list拼接为一个字符串在 python 中如果想将 list 拼接为一个字符串,可使用 join() 方法。join() 方法描述将序列
- 先看效果,实现一个图片左右摇动,在一般的H5宣传页,商家活动页面我们会看到这样的动画,小程序的动画效果不同于css3动画效果,是通过js来完
- 前言本文使用 cpu 的 tensorflow 2.8 来完成 GRU 文本生成任务。如果想要了解文本生成的相关概念,可以参考我之前写的文章
- 心血来潮写了个多线程抓妹子图,虽然代码还是有一些瑕疵,但是还是记录下来,分享给大家。Pic_downloader.py# -*- codin
- 本文实例讲述了Python实现获取邮箱内容并解析的方法。分享给大家供大家参考,具体如下:# -*- coding: utf-8 -*-fro
- 本文实例为大家分享了js实现滑动进度条效果的具体代码,供大家参考,具体内容如下进度条:<!DOCTYPE html><ht
- TensorFlow中tf.batch_matmul()用法如果有两个三阶张量,size分别为a.shape = [100, 3, 4]b.
- python中自定义模块导入路径的方式主要有以下3种:(1)使用sys.path.append()随着程序执行,会动态地添加模块导入的路径,
- math常用方法1.math.ceil()向上取整import mathprint(math.ceil(56.1))572.math.flo
- 框架概念框架和web服务器关系·静态资源:不是经常变化的资源、往往是固定不变的资源·动态资源:经常变化的资源·模板文件:提供了一个显示的模板
- 中间件Django中的中间件是一个轻量级、底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出。中间件的设
- PHP str_split() 函数实例把字符串 "Hello" 分割到数组中:<?php print_r(str
- 好了,下面就是满足你设想的几个主程序,你还可以在实际应用中不断完善和扩充:login.asp' 考生验证<%@&nb
- 一、创建堆heapq有两种方式创建堆, 一种是使用一个空列表,然后使用heapq.heappush()函数把值加入堆中,另外一种就是使用he
- 需求:该接口,含两个参数,一个是file,一个是paperName。其中file为上传的文件。content-type为form-data。
- Django中请求的生命周期HTTP请求及服务端响应中传输的所有数据都是字符串步骤用户在浏览器中输入url时,浏览器会生成请求头和请求体发给
- 在用python进行图像处理时,有时需要遍历numpy数组,下面是遍历数组的方法:[rows, cols] = num.shape for