欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn)(executemany的使用)

 更新時(shí)間:2021年04月30日 14:02:59   作者:芝麻芋圓  
這篇文章主要介紹了python數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn)(executemany的使用),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

正常情況下往數(shù)據(jù)庫(kù)多張表中批量插入1000條數(shù)據(jù),若一條一條insert插入,則調(diào)用sql語(yǔ)句查詢(xún)插入需要執(zhí)行幾千次,花費(fèi)時(shí)間長(zhǎng)

現(xiàn)使用cursor.executemany(sql,args) ,可對(duì)數(shù)據(jù)進(jìn)行批量插入,
其中args是一個(gè)包含多個(gè)元組的list列表,每個(gè)元組對(duì)應(yīng)mysql當(dāng)中的一條數(shù)據(jù)

以下是實(shí)例:
往數(shù)據(jù)庫(kù)中的order表、order_detail表和pay表中插入1000條訂單數(shù)據(jù),訂單詳情數(shù)據(jù)以及支付數(shù)據(jù)
1.pay表中的id字段是order表中的pay_id字段
2.order表中的id字段是order_detail表中的order_id字段

1.初始化屬性(包括host、port、user、password和database)

def __init__(self):
        self.__db_host=XXX
        self.__db_port=XXX
        self.__db_user=XXX
        self.__db_password=XXX
        self.__db_database=XXX

2.連接數(shù)據(jù)庫(kù)

def isConnection(self):
        self.__db=pymysql.connect(
            host=self.__db_host,
            port=self.__db_port,
            user=self.__db_user,
            password=self.__db_password,
            database=self.__db_database,
            charset='utf8'
        )

3.批量往pay表中插入1000條數(shù)據(jù)

# 插入數(shù)據(jù)進(jìn)pay表
    def pay_insert(self,pay_value):
        try:
            # 連接數(shù)據(jù)庫(kù)
            self.isConnection()
            # 創(chuàng)建游標(biāo)
            global cursor
            cursor=self.__db.cursor()
            # 執(zhí)行
            cursor.executemany('insert into `pay表`(type,pay_money,pay_time,pay_no,STATUS,create_by,create_time,update_by,update_time) value (%s,%s,%s,%s,%s,%s,%s,%s,%s)',pay_value)
        except Exception as e:
            print e
        finally:
            cursor.close()
            self.__db.commit()
            self.__db.close()

    # 生成pay表所需字段,并調(diào)用sql
    def pay_data(self):
        pay_value=list()
        for i in range(1,1000):
            pay_value.append((0,8800,time.localtime(),str(random.randint(712300000000,712399999999)),3,49338,time.localtime(),49338,time.localtime()))
        now_time=time.localtime()
        self.pay_insert(pay_value)
        return now_time

4.pay表中生成的1000條數(shù)據(jù),依次取出id

 # 獲取pay_id
    def get_pay_id(self,now_time):
        try:
            self.isConnection()
            global cursor
            cursor=self.__db.cursor()
            cursor.execute('select id from `pay表` where create_time >= %s',now_time)
            id_value=list()
            for i in range(1,1000):
                pay_id=cursor.fetchone()
                id_value.append(pay_id)
            return id_value
        except Exception as e:
            print e
        finally:
            cursor.close()
            self.__db.commit()
            self.__db.close()

 以下是完整代碼:

# #!/usr/bin/python
# # -*- coding: UTF-8 -*-


import pymysql          # 先pip install pymysql
import random
import time

class DatabaseAcess:
    # 初始化屬性(包括host、port、user、password和database)
    def __init__(self):
        self.__db_host=XXX
        self.__db_port=XXX
        self.__db_user=XXX
        self.__db_password=XXX
        self.__db_database=XXX

    # 連接數(shù)據(jù)庫(kù)
    def isConnection(self):
        self.__db=pymysql.connect(
            host=self.__db_host,
            port=self.__db_port,
            user=self.__db_user,
            password=self.__db_password,
            database=self.__db_database,
            charset='utf8'
        )


    # 插入數(shù)據(jù)進(jìn)pay表
    def pay_insert(self,pay_value):
        try:
            # 連接數(shù)據(jù)庫(kù)
            self.isConnection()
            # 創(chuàng)建游標(biāo)
            global cursor
            cursor=self.__db.cursor()
            # 執(zhí)行
            cursor.executemany('insert into `pay表`(type,pay_money,pay_time,pay_no,STATUS,create_by,create_time,update_by,update_time) value (%s,%s,%s,%s,%s,%s,%s,%s,%s)',pay_value)

        except Exception as e:
            print e
        finally:
            cursor.close()
            self.__db.commit()
            self.__db.close()


    # 生成pay表所需字段,并調(diào)用sql
    def pay_data(self,data_number):
        pay_value=list()
        for i in range(1,data_number):
            pay_value.append((0,8800,time.localtime(),str(random.randint(712300000000,712399999999)),3,49338,time.localtime(),49338,time.localtime()))
        now_time=time.localtime()
        self.pay_insert(pay_value)
        return now_time


    # 獲取pay_id
    def get_pay_id(self,now_time,data_number):
        try:
            self.isConnection()
            global cursor
            cursor=self.__db.cursor()
            cursor.execute('select id from `pay表` where create_time >= %s',now_time)
            id_value=list()
            for i in range(1,data_number):
                pay_id=cursor.fetchone()
                id_value.append(pay_id)
            return id_value
        except Exception as e:
            print e
        finally:
            cursor.close()
            self.__db.commit()
            self.__db.close()


    # 插入數(shù)據(jù)進(jìn)order表
    def order_insert(self,order_value):
        try:
            self.isConnection()
            global cursor
            cursor=self.__db.cursor()
            cursor.executemany('insert into `order表` (student_name,student_id,school_id,school_name,tel,height,sex,pay_id,order_no,status,original_price,payment_price,order_type,create_by,create_time,update_by,update_time,purchase_id,dept_id,sub_order_mid,class_name,shoe_size,student_no,weight) value (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',order_value)
        except Exception as e:
            print e
        finally:
            cursor.close()
            self.__db.commit()
            self.__db.close()


    # 生成order表所需字段,并調(diào)用sql
    def order_data(self,id_value,data_number):
        order_value=list()
        for i in range(1,data_number):
            pay_id=str(id_value[i-1]).replace("L,)","").replace("(","")
            order_value.append(("周瑜",35999,346,"A城小學(xué)","13322222222",130,1,pay_id,str(random.randint(7100000000,7999999999)),2,8800,8800,1,49338,time.localtime(),49338,time.localtime(),405,121,564123698745632,"三年級(jí) 3班",30,30,30))
        sys_time=time.localtime()
        self.order_insert(order_value)
        return sys_time


    # 獲取order_id
    def get_order_id(self,sys_time,data_number):
        try:
            self.isConnection()
            global cursor
            cursor=self.__db.cursor()
            cursor.execute('select id from `order表` where create_time >= %s',sys_time)
            order_id_list=list()
            for i in range(1,data_number):
                order_id_list.append(cursor.fetchone())
            return order_id_list
        except Exception as e:
            print e
        finally:
            cursor.close()
            self.__db.commit()
            self.__db.close()


    # 插入數(shù)據(jù)進(jìn)order_detail表
    def order_detail_insert(self,detail_value):
        try:
            self.isConnection()
            global cursor
            cursor=self.__db.cursor()
            cursor.executemany('insert into `order_details表` (order_id,commodity_name,commodity_id,original_price,payment_price,img,number,status,create_by,create_time,update_by,update_time) value (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',detail_value)
        except Exception as e:
            print e
        finally:
            cursor.close()
            self.__db.commit()
            self.__db.close()


    # 生成order_detail表所需字段,并調(diào)用sql
    def order_detail_data(self,order_id_list,data_number):
        detail_value=list()
        for i in range(1,data_number):
            order_id=str(order_id_list[i-1]).replace("L,)","").replace("(","")
            detail_value.append((order_id,"A城小學(xué)春季校服","1382932636506902530",8800,8800,"https://ygxf-dev2.obs.cn-north-1.myhuaweicloud.com:443/image%2F1618551784845-589.jpg",1,2,49338,time.localtime(),49338,time.localtime()))
        self.order_detail_insert(detail_value)


if __name__ == '__main__':
    db=DatabaseAcess()
    data_number=3
    db.order_detail_data(order_id_list=db.get_order_id(sys_time=db.order_data(id_value=db.get_pay_id(now_time=db.pay_data(data_number=data_number),data_number=data_number),data_number=data_number),data_number=data_number),data_number=data_number)
    print ("{0}條數(shù)據(jù)插入完成".format(data_number-1))

到此這篇關(guān)于python數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn)(executemany的使用)的文章就介紹到這了,更多相關(guān)python數(shù)據(jù)庫(kù)批量插入 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python+mysql實(shí)現(xiàn)個(gè)人論文管理系統(tǒng)

    python+mysql實(shí)現(xiàn)個(gè)人論文管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了python+mysql實(shí)現(xiàn)個(gè)人論文管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Numpy一維線性插值函數(shù)的用法

    Numpy一維線性插值函數(shù)的用法

    這篇文章主要介紹了Numpy一維線性插值函數(shù)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • 利用Python?Matplotlib繪圖并輸出圖像到文件中的方式

    利用Python?Matplotlib繪圖并輸出圖像到文件中的方式

    這篇文章主要介紹了利用Python?Matplotlib繪圖并輸出圖像到文件中的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 關(guān)于數(shù)據(jù)分析Pandas的Series用法總結(jié)

    關(guān)于數(shù)據(jù)分析Pandas的Series用法總結(jié)

    這篇文章主要介紹了關(guān)于數(shù)據(jù)分析Pandas的Series用法總結(jié),Series序列,是一種一維的結(jié)構(gòu),類(lèi)似于一維列表和ndarray中的一維數(shù)組,但是功能比他們要更為強(qiáng)大,Series由兩部分組成:索引index和數(shù)值values,本篇對(duì)其用法做出總結(jié)
    2023-07-07
  • Python Django切換MySQL數(shù)據(jù)庫(kù)實(shí)例詳解

    Python Django切換MySQL數(shù)據(jù)庫(kù)實(shí)例詳解

    這篇文章主要介紹了Python Django切換MySQL數(shù)據(jù)庫(kù)實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • OpenCV?imread讀取圖片失敗的問(wèn)題及解決

    OpenCV?imread讀取圖片失敗的問(wèn)題及解決

    這篇文章主要介紹了OpenCV?imread讀取圖片失敗的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 解析Python中while true的使用

    解析Python中while true的使用

    這篇文章主要介紹了解析Python中while true的使用,while true即用來(lái)制造一個(gè)無(wú)限循環(huán),需要的朋友可以參考下
    2015-10-10
  • 利用Python連接Oracle數(shù)據(jù)庫(kù)的基本操作指南

    利用Python連接Oracle數(shù)據(jù)庫(kù)的基本操作指南

    由于之前的在職的公司沒(méi)有機(jī)會(huì)接觸到Oralce數(shù)據(jù)庫(kù),所以就沒(méi)有用python連接過(guò)Oralce,之前大多集中在連接mysql和sql server,最近在做一下web自動(dòng)化的工作,所以簡(jiǎn)單的記錄一下,下面這篇文章主要給大家介紹了關(guān)于利用Python連接Oracle數(shù)據(jù)庫(kù)的基本操作,需要的朋友可以參考下
    2022-06-06
  • 基于Python和C++實(shí)現(xiàn)刪除鏈表的節(jié)點(diǎn)

    基于Python和C++實(shí)現(xiàn)刪除鏈表的節(jié)點(diǎn)

    這篇文章主要介紹了基于Python和C++實(shí)現(xiàn)刪除鏈表的節(jié)點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • python 定時(shí)器,實(shí)現(xiàn)每天凌晨3點(diǎn)執(zhí)行的方法

    python 定時(shí)器,實(shí)現(xiàn)每天凌晨3點(diǎn)執(zhí)行的方法

    今天小編就為大家分享一篇python 定時(shí)器,實(shí)現(xiàn)每天凌晨3點(diǎn)執(zhí)行的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02

最新評(píng)論