使用Python實現(xiàn)插入100萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫
Python插入100萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫
步驟一:導入所需模塊和庫
首先,我們需要導入 MySQL 連接器模塊和 Faker 模塊。MySQL 連接器模塊用于連接到 MySQL 數(shù)據(jù)庫,而 Faker 模塊用于生成虛假數(shù)據(jù)。
import mysql.connector # 導入 MySQL 連接器模塊 from faker import Faker # 導入 Faker 模塊,用于生成虛假數(shù)據(jù)
步驟二:創(chuàng)建 Faker 實例
然后,我們創(chuàng)建一個 Faker 實例,以便使用其功能生成虛假數(shù)據(jù)。
faker = Faker() # 創(chuàng)建 Faker 實例
步驟三:連接到 MySQL 數(shù)據(jù)庫
接下來,我們使用 MySQL 連接器模塊連接到 MySQL 數(shù)據(jù)庫。需要提供主機地址、用戶名、密碼和數(shù)據(jù)庫名稱。
conn = mysql.connector.connect( host='localhost', # 數(shù)據(jù)庫主機地址 user='root', # 數(shù)據(jù)庫用戶名 password='123456', # 數(shù)據(jù)庫密碼 database='test2' # 數(shù)據(jù)庫名稱 )
步驟四:創(chuàng)建游標對象
然后,我們創(chuàng)建一個游標對象,用于執(zhí)行 SQL 語句。
cursor = conn.cursor() # 創(chuàng)建游標對象,用于執(zhí)行 SQL 語句
步驟五:插入虛假數(shù)據(jù)
現(xiàn)在,我們準備開始插入虛假數(shù)據(jù)到數(shù)據(jù)庫中。我們使用循環(huán)生成多條數(shù)據(jù),并將其插入到數(shù)據(jù)庫表中。
for _ in range(1000000): # 循環(huán)100萬次,插入100萬條數(shù)據(jù) # 使用 Faker 實例生成虛假數(shù)據(jù) name = faker.name() # 姓名 address = faker.address() # 地址 email = faker.email() # 電子郵件 phone_number = faker.phone_number() # 電話號碼 job_title = faker.job() # 職位 company = faker.company() # 公司 date_of_birth = faker.date_of_birth() # 出生日期 credit_card_number = faker.credit_card_number() # 信用卡號 # 定義 SQL 插入語句 sql = "INSERT INTO fake_data (name, address, email, phone_number, job_title, company, date_of_birth, credit_card_number) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)" # 設置參數(shù)值 val = (name, address, email, phone_number, job_title, company, date_of_birth, credit_card_number) # 執(zhí)行 SQL 插入語句 cursor.execute(sql, val)
步驟六:提交事務和關閉連接
最后,我們提交事務以保存更改,并關閉游標和數(shù)據(jù)庫連接。
conn.commit() # 提交事務,保存更改 cursor.close() # 關閉游標 conn.close() # 關閉數(shù)據(jù)庫連接
使用 Python 將 MySQL 數(shù)據(jù)庫中的數(shù)據(jù)逐步查詢并寫入多個 Excel 文件
步驟一:導入所需模塊和庫
首先,我們需要導入 os 模塊用于文件和目錄操作,pandas 庫用于數(shù)據(jù)處理,以及 mysql.connector 模塊用于連接 MySQL 數(shù)據(jù)庫。
import os # 導入 os 模塊,用于文件和目錄操作 import pandas as pd # 導入 pandas 庫并使用 pd 別名,用于數(shù)據(jù)處理 import mysql.connector # 導入 mysql.connector 模塊,用于連接 MySQL 數(shù)據(jù)庫
步驟二:連接到 MySQL 數(shù)據(jù)庫
conn = mysql.connector.connect( host='localhost', # 數(shù)據(jù)庫主機地址 user='root', # 數(shù)據(jù)庫用戶名 password='123456', # 數(shù)據(jù)庫密碼 database='test2' # 數(shù)據(jù)庫名稱 )
步驟三:設置每個 Excel 文件的行數(shù)限制和輸出文件夾
chunk_size = 50000 # 每個 Excel 文件的行數(shù)限制 output_folder = "output_data" # 輸出文件夾名稱 if not os.path.exists(output_folder): # 如果文件夾不存在,則創(chuàng)建 os.makedirs(output_folder)
步驟四:逐步查詢數(shù)據(jù)庫并寫入 Excel 文件
offset = 0 # 查詢偏移量初始值為0 while True: # 使用循環(huán)查詢數(shù)據(jù)庫,直到數(shù)據(jù)查詢完畢 query = f"SELECT * FROM fake_data LIMIT {offset}, {chunk_size}" # 構造 SQL 查詢語句 df = pd.read_sql(query, conn) # 使用 pandas 讀取 SQL 查詢結果為 DataFrame if df.empty: # 如果查詢結果為空,則退出循環(huán) break output_file = os.path.join(output_folder, f"output_{offset // chunk_size + 1}.xlsx") # 構造輸出文件路徑 df.to_excel(output_file, index=False) # 將 DataFrame 寫入 Excel 文件,不寫入索引列 offset += chunk_size # 更新查詢偏移量,準備下一次查詢
步驟五:關閉數(shù)據(jù)庫連接
conn.close() # 關閉數(shù)據(jù)庫連接
最后,我們關閉數(shù)據(jù)庫連接,釋放資源。
到此這篇關于使用Python實現(xiàn)插入100萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫的文章就介紹到這了,更多相關Python插入數(shù)據(jù)到MySQL數(shù)據(jù)庫內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
numpy 中l(wèi)inspace函數(shù)的使用
本文主要介紹了numpy 中l(wèi)inspace函數(shù)的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03Pandas實現(xiàn)復制dataframe中的每一行
這篇文章主要介紹了Pandas實現(xiàn)復制dataframe中的每一行方式,2024-02-02Python利用多線程優(yōu)化for循環(huán)的技巧分享
多線程可以讓程序同時執(zhí)行多個任務,從而提高整體運行效率,這篇文章將詳細介紹如何在Python中使用多線程來優(yōu)化for循環(huán),感興趣的可以了解下2025-02-02python3+PyQt5 數(shù)據(jù)庫編程--增刪改實例
今天小編就為大家分享一篇python3+PyQt5 數(shù)據(jù)庫編程--增刪改實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06