1. 程式人生 > >python生成資料後,快速匯入資料庫

python生成資料後,快速匯入資料庫

1、使用python生成資料庫檔案內容# coding=utf-8import randomimport timedef 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%';

 

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