Python Sql數(shù)據(jù)庫(kù)增刪改查操作簡(jiǎn)單封裝
本文實(shí)例為大家分享了如何利用Python對(duì)數(shù)據(jù)庫(kù)的增刪改查進(jìn)行簡(jiǎn)單的封裝,供大家參考,具體內(nèi)容如下
1.insert
import mysql.connector
import os
import codecs
#設(shè)置數(shù)據(jù)庫(kù)用戶(hù)名和密碼
user='root';#用戶(hù)名
pwd='root';#密碼
host='localhost';#ip地址
db='mysql';#所要操作數(shù)據(jù)庫(kù)名字
charset='UTF-8'
cnx = mysql.connector.connect(user=user,password=pwd, host=host, database=db)
#設(shè)置游標(biāo)
cursor = cnx.cursor(dictionary=True)
#插入數(shù)據(jù)
#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
#取得數(shù)據(jù)庫(kù)信息
# 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
#顯示建表語(yǔ)句
#table string 表名
#return string 建表語(yǔ)句
def showCreateTable(table):
sql='show create table %s'%(table)
cursor.execute(sql)
result=cursor.fetchall()[0]
return result['Create Table']
#print(showCreateTable('gelixi_admin'))
#顯示表結(jié)構(gòu)語(yǔ)句
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數(shù)據(jù)庫(kù)增刪改查操作的相關(guān)操作,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Python安裝第三方庫(kù)及常見(jiàn)問(wèn)題處理方法匯總
本文給大家匯總介紹了Python安裝第三方庫(kù)及常見(jiàn)問(wèn)題處理方法,非常的簡(jiǎn)單使用,有需要的小伙伴可以參考下2016-09-09
Python實(shí)現(xiàn)人臉識(shí)別的詳細(xì)圖文教程
人臉識(shí)別是人工智能的一個(gè)重要應(yīng)用,下面這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)人臉識(shí)別的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解
這篇文章主要介紹了python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
python爬蟲(chóng)今日熱榜數(shù)據(jù)到txt文件的源碼
這篇文章主要介紹了python爬蟲(chóng)今日熱榜數(shù)據(jù)到txt文件的源碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Python實(shí)現(xiàn)為圖片添加水印的示例詳解
這篇文章主要介紹了如何通過(guò)Python3實(shí)現(xiàn)添加水印,這樣發(fā)朋友圈,圖片再也不怕被盜了?。?!文中的示例代碼簡(jiǎn)潔易懂,需要的可以參考一下2022-02-02
python中urllib.unquote亂碼的原因與解決方法
這篇文章主要給大家介紹了python中urllib.unquote亂碼的原因與解決方法,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友可以參考學(xué)習(xí),下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-04-04
Python3的urllib.parse常用函數(shù)小結(jié)(urlencode,quote,quote_plus,unquot
這篇文章主要介紹了Python3的urllib.parse常用函數(shù),結(jié)合實(shí)例形式分析了urlencode,quote,quote_plus,unquote,unquote_plus等函數(shù)的相關(guān)使用技巧,需要的朋友可以參考下2016-09-09
Python中的反射知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于Python中的反射知識(shí)點(diǎn)總結(jié)內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)參考下。2021-11-11

