python生成數據后,快速導入數據庫


1、使用python生成數據庫文件內容

# coding=utf-8
import random
import time


def create_user():
    start = time.time()
    count = 1000  # 一千萬條數據
    beginId = 200010000
    with open(r"./userInfo.txt", "w") as fp:
        for i in range(1,count+1):
            id = str(i)
            userId = beginId + i
            name = ''.join(random.sample('zyxwvutsrqponmlkjihgfedcba', 4)).replace('', '')
            sex = str(random.choice(['男', '女']))
            weight = str(random.randrange(10, 99))
            address = str(random.choice(['北京', '上海', '深圳', '廣州', '杭州']))
            insert_t_user_weight = (
                            "INSERT INTO t_user_weight VALUES ('%s', '%s', '%s','%s', '%s', '%s', '%s');"
            % (id, userId, name, sex, weight, address, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
                       )
            insert_t_user_weight = insert_t_user_weight + '\n'
            # print(insert_t_user_weight)
            fp.write(insert_t_user_weight)

        print('共創建%d條sql耗時:'% count, time.time() - start)


if __name__ == "__main__":
        create_user()

2、使用命令導入數據庫

load data infile "/tmp/userInfo.txt" into table test_insert fields terminated by ',';

3、MYSQL導入數據出現The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

 

這個原因是因為在安裝MySQL的時候限制了導入與導出的目錄權限,只能在規定的目錄下才能導入,我們需要通過下面命令查看 secure-file-priv 當前的值是什么。

show variables like '%secure%';

 

 

只需要把相對應的文件放在上面的目錄下,即可成功讀取,而不會報上面的錯誤了。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM