python將數(shù)據(jù)插入數(shù)據(jù)庫(kù)的代碼分享
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)呢?
實(shí)現(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ù)庫(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ù)連接 #如果沒有這些關(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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 用python實(shí)現(xiàn)操縱mysql數(shù)據(jù)庫(kù)插入
- python數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn)(executemany的使用)
- 在python中使用pymysql往mysql數(shù)據(jù)庫(kù)中插入(insert)數(shù)據(jù)實(shí)例
- python的mysql數(shù)據(jù)庫(kù)建立表與插入數(shù)據(jù)操作示例
- python讀取word文檔,插入mysql數(shù)據(jù)庫(kù)的示例代碼
- 使用python讀取csv文件快速插入數(shù)據(jù)庫(kù)的實(shí)例
- python數(shù)據(jù)庫(kù)操作常用功能使用詳解(創(chuàng)建表/插入數(shù)據(jù)/獲取數(shù)據(jù))
- python向MySQL數(shù)據(jù)庫(kù)插入數(shù)據(jù)的操作方法
相關(guān)文章
django settings.py配置文件的詳細(xì)介紹
本文主要介紹了django settings.py配置文件的詳細(xì)介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04pytorch繪制并顯示loss曲線和acc曲線,LeNet5識(shí)別圖像準(zhǔn)確率
今天小編就為大家分享一篇pytorch繪制并顯示loss曲線和acc曲線,LeNet5識(shí)別圖像準(zhǔn)確率,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01玩轉(zhuǎn)python爬蟲之URLError異常處理
這篇文章主要介紹了python爬蟲的URLError異常處理,詳細(xì)探尋一下URL\HTTP異常處理的相關(guān)內(nèi)容,通過(guò)一些具體的實(shí)例來(lái)分析一下,非常的簡(jiǎn)單,但是卻很實(shí)用,感興趣的小伙伴們可以參考一下2016-02-02Python實(shí)現(xiàn)合并兩個(gè)有序鏈表的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)合并兩個(gè)有序鏈表的方法,涉及Python操作鏈表節(jié)點(diǎn)的遍歷、判斷、添加等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01python使用socket創(chuàng)建tcp服務(wù)器和客戶端
這篇文章主要為大家詳細(xì)介紹了python使用socket創(chuàng)建tcp服務(wù)器和客戶端,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04pytorch __init__、forward與__call__的用法小結(jié)
這篇文章主要介紹了pytorch __init__、forward與__call__的用法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02基于python實(shí)現(xiàn)可視化生成二維碼工具
這篇文章主要介紹了基于python實(shí)現(xiàn)可視化生成二維碼工具,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07