蝙蝠岛资源网 Design By www.hbtsch.com
本文实例为大家分享了如何利用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数据库增删改查操作的相关操作,希望对大家的学习有所帮助。
蝙蝠岛资源网 Design By www.hbtsch.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
蝙蝠岛资源网 Design By www.hbtsch.com
暂无Python Sql数据库增删改查操作简单封装的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2025年11月08日
2025年11月08日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]