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%';

 

 

只需要把相对应的文件放在上面的目录下,即可成功读取,而不会报上面的错误了。

 

 

posted @ 2018-10-24 10:10  &阿岩  Views(1608)  Comments(0Edit  收藏  举报