最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python Sql數(shù)據(jù)庫增刪改查操作簡單封裝

 更新時間:2016年04月18日 14:35:05   作者:mrmusic  
這篇文章主要為大家介紹了Python Sql數(shù)據(jù)庫增刪改查操作簡單封裝,感興趣的小伙伴們可以參考一下

本文實例為大家分享了如何利用Python對數(shù)據(jù)庫的增刪改查進行簡單的封裝,供大家參考,具體內(nèi)容如下

1.insert    

import mysql.connector
import os
import codecs
#設置數(shù)據(jù)庫用戶名和密碼
user='root';#用戶名
pwd='root';#密碼
host='localhost';#ip地址
db='mysql';#所要操作數(shù)據(jù)庫名字
charset='UTF-8'
cnx = mysql.connector.connect(user=user,password=pwd, host=host, database=db)
#設置游標
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ù)庫信息
# 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'))
#顯示表結(jié)構(gòu)語句
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ù)庫增刪改查操作的相關操作,希望對大家的學習有所幫助。

相關文章

  • 使用python讀取.text文件特定行的數(shù)據(jù)方法

    使用python讀取.text文件特定行的數(shù)據(jù)方法

    今天小編就為大家分享一篇使用python讀取.text文件特定行的數(shù)據(jù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • pyqt 多窗口之間的相互調(diào)用方法

    pyqt 多窗口之間的相互調(diào)用方法

    今天小編就為大家分享一篇pyqt 多窗口之間的相互調(diào)用方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • pandas實現(xiàn)datetime64與unix時間戳互轉(zhuǎn)

    pandas實現(xiàn)datetime64與unix時間戳互轉(zhuǎn)

    這篇文章主要介紹了pandas實現(xiàn)datetime64與unix時間戳互轉(zhuǎn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • python通過scapy獲取局域網(wǎng)所有主機mac地址示例

    python通過scapy獲取局域網(wǎng)所有主機mac地址示例

    這篇文章主要介紹了python通過scapy獲取局域網(wǎng)所有主機mac地址示例,需要的朋友可以參考下
    2014-05-05
  • pandas combine_first函數(shù)處理兩個數(shù)據(jù)集重疊和缺失

    pandas combine_first函數(shù)處理兩個數(shù)據(jù)集重疊和缺失

    combine_first是pandas中的一個函數(shù),它可以將兩個DataFrame對象按照索引進行合并,用一個對象中的非空值填充另一個對象中的空值,這個函數(shù)非常適合處理兩個數(shù)據(jù)集有部分重疊和缺失的情況,可以實現(xiàn)數(shù)據(jù)的補全和更新,本文介紹combine_first函數(shù)的語法及一些案例應用
    2024-01-01
  • Django?Rest?Framework實現(xiàn)身份認證源碼詳解

    Django?Rest?Framework實現(xiàn)身份認證源碼詳解

    這篇文章主要為大家介紹了Django?Rest?Framework實現(xiàn)身份認證源碼詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • virtualenv實現(xiàn)多個版本Python共存

    virtualenv實現(xiàn)多個版本Python共存

    virtualenv用于創(chuàng)建獨立的Python環(huán)境,多個Python相互獨立,互不影響,它能夠:1. 在沒有權(quán)限的情況下安裝新套件 2. 不同應用可以使用不同的套件版本 3. 套件升級不影響其他應用
    2017-08-08
  • 微信跳一跳python代碼實現(xiàn)

    微信跳一跳python代碼實現(xiàn)

    這篇文章主要為大家詳細介紹了微信跳一跳python代碼實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python?Cloudinary實現(xiàn)圖像和視頻上傳詳解

    Python?Cloudinary實現(xiàn)圖像和視頻上傳詳解

    這篇文章主要介紹了Python?Cloudinary實現(xiàn)圖像和視頻上傳功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-11-11
  • Pytorch中的backward()多個loss函數(shù)用法

    Pytorch中的backward()多個loss函數(shù)用法

    這篇文章主要介紹了Pytorch中的backward()多個loss函數(shù)用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05

最新評論

阳朔县| 鲜城| 蓬溪县| 陆河县| 北票市| 拉孜县| 北京市| 尤溪县| 新化县| 长葛市| 海兴县| 奉贤区| 临清市| 惠安县| 平陆县| 禄丰县| 惠州市| 浦东新区| 龙海市| 阜新市| 静海县| 古浪县| 普兰县| 景德镇市| 陈巴尔虎旗| 调兵山市| 新宁县| 朔州市| 五莲县| 吐鲁番市| 合作市| 安陆市| 桓仁| 祁阳县| 天全县| 滦平县| 忻州市| 泰兴市| 仪陇县| 米林县| 通海县|