最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:动视网 责编:小OO 时间:2020-11-27 14:25:59
文档

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

1.insert;,import mysql.connectorimport osimport codecs#设置数据库用户名和密码user=';root';#用户名pwd=';root';#密码host=';localhost';#ip地址db=';mysql';#所要操作数据库名字charset=';UTF-8';cnx = mysql.connector.connect(user=user,{';type_name';:';';sddfdsfs';';,';type_sort';:';283';}))def insert(table_name。
推荐度:
导读1.insert;,import mysql.connectorimport osimport codecs#设置数据库用户名和密码user=';root';#用户名pwd=';root';#密码host=';localhost';#ip地址db=';mysql';#所要操作数据库名字charset=';UTF-8';cnx = mysql.connector.connect(user=user,{';type_name';:';';sddfdsfs';';,';type_sort';:';283';}))def insert(table_name。


本文实例为大家分享了如何利用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数据库增删改查操作简单封装方法

1.insert;,import mysql.connectorimport osimport codecs#设置数据库用户名和密码user=';root';#用户名pwd=';root';#密码host=';localhost';#ip地址db=';mysql';#所要操作数据库名字charset=';UTF-8';cnx = mysql.connector.connect(user=user,{';type_name';:';';sddfdsfs';';,';type_sort';:';283';}))def insert(table_name。
推荐度:
标签: 数据库 封装 sql
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top