1 shell脚本

例: 实现批量插入数据
image.png
另开一个终端, 写shell脚本

vi test.sh

  1. for ((i=1;i<=5;i++)); do
  2. mysql -uroot -p123456 -e "insert mytest.bbb(name) values('ws$i');"
  3. done

2 sql文件

例: 实现创建一个数据表
image.png
例: 导入sql的第二种方法

mysql -h192.168.3.67 -uws -p123456 meiduo < goods_data.sql

3 py脚本

例: 自动插入10000行记录

  1. import pymysql
  2. db = pymysql.connect(
  3. host='localhost',
  4. port=3306,
  5. user='ws',
  6. password='123456',
  7. database='mytest',
  8. )
  9. cursor = db.cursor()
  10. for i in range(10000):
  11. key1 = 'key1_' + str(i)
  12. key2 = i
  13. key3 = 'key3_' + str(i)
  14. keyp1 = 'keyp1_' + str(i)
  15. keyp2 = 'keyp2_' + str(i)
  16. keyp3 = 'keyp3_' + str(i)
  17. comn = 'comn_' + str(i)
  18. sql = 'insert tb_sgle(key1, key2, key3, key_part1, key_part2, key_part3, common_field) ' \
  19. 'values(%s, %s, %s, %s, %s, %s, %s);'
  20. try:
  21. cursor.execute(sql, (key1, key2, key3, keyp1, keyp2, keyp3, comn))
  22. db.commit()
  23. print(i, 'done')
  24. except:
  25. db.rollback()
  26. print(i, 'failed')
  27. print('all done')