午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python將數(shù)據(jù)插入數(shù)據(jù)庫(kù)的代碼分享

瀏覽:17日期:2022-07-14 08:46:27

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

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

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

python將數(shù)據(jù)插入數(shù)據(jù)庫(kù)的代碼分享

實(shí)現(xiàn)代碼:

#導(dǎo)入需要使用到的數(shù)據(jù)模塊import pandas as pdimport pymysql#讀入數(shù)據(jù)filepath = ’E:_DataSetcatering_sale.xls’data = pd.read_excel(filepath)#建立數(shù)據(jù)庫(kù)連接db = pymysql.connect(’localhost’,’root’,’1234’,’python_analysis’)#獲取游標(biāo)對(duì)象cursor = db.cursor()#創(chuàng)建數(shù)據(jù)庫(kù),如果數(shù)據(jù)庫(kù)已經(jīng)存在,注意主鍵不要重復(fù),否則出錯(cuò)try: cursor.execute(’create table catering_sale(num int primary key,date datetime, sale float )’)except: print(’數(shù)據(jù)庫(kù)已存在!’)#插入數(shù)據(jù)語(yǔ)句query = '''insert into catering_sale (num, date, sale) values (%s,%s,%s)'''#迭代讀取每行數(shù)據(jù)#values中元素有個(gè)類型的強(qiáng)制轉(zhuǎn)換,否則會(huì)出錯(cuò)的#應(yīng)該會(huì)有其他更合適的方式,可以進(jìn)一步了解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ù)庫(kù)連接#如果沒(méi)有這些關(guān)閉操作,執(zhí)行后在數(shù)據(jù)庫(kù)中查看不到數(shù)據(jù)cursor.close()db.commit()db.close()#重新建立數(shù)據(jù)庫(kù)連接db = pymysql.connect(’localhost’,’root’,’1234’,’python_anylysis’)cursor = db.cursor()#查詢數(shù)據(jù)庫(kù)并打印內(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()

知識(shí)點(diǎn)擴(kuò)展:

數(shù)據(jù)庫(kù)連接池

數(shù)據(jù)庫(kù)的連接是昂貴的,一個(gè)連接要經(jīng)過(guò)TCP三次握手,四次揮手,而且一臺(tái)計(jì)算機(jī)的最大線程數(shù)也是有限的

數(shù)據(jù)庫(kù)連接池技術(shù)就是先創(chuàng)建好連接,再直接拿出來(lái)使用

import mysql.connector,mysql.connector.pooling config={ 'host': 'localhost', 'port': '3306', 'user': 'root', 'password': 'password', 'database': 'demo' } try: pool=mysql.connector.pooling.MySQLConnectionPool(**config,pool_size=5) con=pool.get_connection() con.start_transaction() cursor = con.cursor() sql = 'INSERT INTO t_dept(deptno,dname,loc) VALUES(%s,%s,%s);' cursor.execute(sql, (70, 'SALES', 'HUBAI')) con.commit() except Exception as e: if 'con' in dir(): con.rollback() print(e) # do not need to close con

到此這篇關(guān)于python將數(shù)據(jù)插入數(shù)據(jù)庫(kù)的代碼分享的文章就介紹到這了,更多相關(guān)python如何將數(shù)據(jù)插入數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产色悠悠 | 撸av | 黄色片成年人 | 蜜臀久久久| 一级特黄aaa | 国产ts人妖调教重口男 | 国产福利视频在线观看 | 日韩在线一区二区 | 久久久成人精品 | 日韩毛片儿| 国产区一区 | 婷婷四房综合激情五月 | 懂色av成人一区二区三区 | 成人视屏在线 | 亚洲成人精品一区二区三区 | 天堂久久久久久 | 日韩成人区| 色婷av| 高清欧美性猛交 | 新av在线 | 午夜视频网站 | 亚洲成人精品av | 黄色福利在线观看 | 亚洲精品播放 | 亚洲欧美日韩偷拍 | 精品视频亚洲 | 成人国产一区二区 | 精品久久久久国产 | 日韩视频在线观看一区二区 | 国产精品久久视频 | 国产18无套直看片 | 欧美国产日韩综合 | 国产第1页 | 午夜aaaa | 中日韩三级 | 精品一区二区三区四 | 美女久久久久 | 亚洲国产精品第一页 | 午夜免费网站 | 亚洲国产精品久久久 | www.自拍|