代码如下:

def write_sql():
    path = r'C:\Users\Administrator\Desktop\sql.txt'

    for i in range(1, 1001):
        str_i = str(i)
        real_name = 'test' + str_i
        phone = 1366666666 + i
        email = 'test' + str_i + '@mail.com'
        sql = "insert into sign_guest values ('{}','{}','{}','{}');".format(i, real_name, phone, email)
        with open(path,'a') as f :
            f.write(sql)
            f.write('\n')

write_sql()

分析思路:

明确需要写入数据的表结构,

将测试数据加入循环中

然后将测试数据与insert 代码合并写入text文件中

写入完成后,更改文件后缀,再进行导入

导入操作步骤:

登录sql:

Python 批量写入sql测试数据

 切换db:

Python 批量写入sql测试数据

 运行命令

 Python 批量写入sql测试数据

Python 批量写入sql测试数据

 导入成功,

Navicat导入

数据库右键点击运行sql文件

Python 批量写入sql测试数据

 选择文件后点击运行

Python 批量写入sql测试数据

相关文章:

  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
  • 2022-12-23
  • 2021-12-11
  • 2022-12-23
  • 2021-06-17
猜你喜欢
  • 2021-12-16
  • 2021-07-03
  • 2022-12-23
  • 2022-12-23
  • 2021-06-03
  • 2022-01-24
  • 2021-12-01
相关资源
相似解决方案