1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      python將數(shù)據(jù)插入數(shù)據(jù)庫的方法

      這篇文章主要介紹了python將數(shù)據(jù)插入數(shù)據(jù)庫的方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

      成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比梁河網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式梁河網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋梁河地區(qū)。費用合理售后完善,十載實體公司更值得信賴。

      python將數(shù)據(jù)插入數(shù)據(jù)庫的方法:首先讀入數(shù)據(jù)并建立數(shù)據(jù)庫連接;然后創(chuàng)建數(shù)據(jù)庫;接著執(zhí)行插入數(shù)據(jù)語句,迭代讀取每行數(shù)據(jù);最后關(guān)閉數(shù)據(jù)庫連接即可。

      python將數(shù)據(jù)插入數(shù)據(jù)庫的方法

      比如現(xiàn)在我們要將如下Excel數(shù)據(jù)表格插入到MySQL數(shù)據(jù)庫中,該如何實現(xiàn)呢?

      python將數(shù)據(jù)插入數(shù)據(jù)庫的方法

      實現(xiàn)代碼:

      #導(dǎo)入需要使用到的數(shù)據(jù)模塊
      import pandas as pd
      import pymysql
      
      #讀入數(shù)據(jù)
      filepath = 'E:\_DataSet\catering_sale.xls'
      data = pd.read_excel(filepath)
      
      #建立數(shù)據(jù)庫連接
      db = pymysql.connect('localhost','root','1234','python_analysis')
      #獲取游標(biāo)對象
      cursor = db.cursor()
      #創(chuàng)建數(shù)據(jù)庫,如果數(shù)據(jù)庫已經(jīng)存在,注意主鍵不要重復(fù),否則出錯
      try:
          cursor.execute('create table catering_sale(num int primary key,date datetime, sale float )')
      except:
          print('數(shù)據(jù)庫已存在!')
      
      #插入數(shù)據(jù)語句
      query = """insert into catering_sale (num, date, sale) values (%s,%s,%s)"""
      
      #迭代讀取每行數(shù)據(jù)
      #values中元素有個類型的強制轉(zhuǎn)換,否則會出錯的
      #應(yīng)該會有其他更合適的方式,可以進一步了解
      for r in range(0, len(data)):
          num = data.ix[r,0]
          date = data.ix[r,1]
          sale = data.ix[r,2]
          values = (int(num), str(date), float(sale))
          cursor.execute(query, values)
      
      #關(guān)閉游標(biāo),提交,關(guān)閉數(shù)據(jù)庫連接
      #如果沒有這些關(guān)閉操作,執(zhí)行后在數(shù)據(jù)庫中查看不到數(shù)據(jù)
      cursor.close()
      db.commit()
      db.close()
      
      #重新建立數(shù)據(jù)庫連接
      db = pymysql.connect('localhost','root','1234','python_anylysis')
      cursor = db.cursor()
      #查詢數(shù)據(jù)庫并打印內(nèi)容
      cursor.execute('''select * from catering_sale''')
      results = cursor.fetchall()
      for row in results:
          print(row)
      #關(guān)閉
      cursor.close()
      db.commit()
      db.close()

      感謝你能夠認真閱讀完這篇文章,希望小編分享python將數(shù)據(jù)插入數(shù)據(jù)庫的方法內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細的解決方法等著你來學(xué)習(xí)!


      新聞名稱:python將數(shù)據(jù)插入數(shù)據(jù)庫的方法
      文章位置:http://www.ef60e0e.cn/article/jgeeji.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        郑州市| 肥城市| 双桥区| 边坝县| 正定县| 桓台县| 汉川市| 阜阳市| 荣成市| 五寨县| 呼玛县| 印江| 仁怀市| 宁陕县| 河南省| 云林县| 田林县| 屏东县| 乌鲁木齐县| 扶沟县| 太白县| 朔州市| 延寿县| 黑水县| 东兴市| 云阳县| 旬邑县| 永新县| 肥城市| 扎鲁特旗| 昌邑市| 横峰县| 离岛区| 樟树市| 武邑县| 谢通门县| 鹤壁市| 布拖县| 汕尾市| 巧家县| 宜兰县|