zoukankan      html  css  js  c++  java
  • 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 + ' '
                # 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%';

     

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

     

  • 相关阅读:
    查看lwjgl常用状态的值
    微信公众号开发java框架:wx4j(MenuUtils篇)
    微信公众号开发java框架:wx4j(KefuUtils篇)
    微信公众号开发java框架:wx4j(MaterialUtils篇)
    微信公众号开发java框架:wx4j(入门篇)
    hashcode和equals方法小记
    https单向认证和双向认证区别
    java开发中获取路径的一些方式
    iOS使用sqlite3原生语法进行增删改查以及FMDB的使用
    IOS自动布局
  • 原文地址:https://www.cnblogs.com/yanpan/p/9841970.html
Copyright © 2011-2022 走看看