1 shell脚本
例: 实现批量插入数据
另开一个终端, 写shell脚本
vi test.sh
for ((i=1;i<=5;i++)); do
mysql -uroot -p123456 -e "insert mytest.bbb(name) values('ws$i');"
done
2 sql文件
例: 实现创建一个数据表
例: 导入sql的第二种方法
mysql -h192.168.3.67 -uws -p123456 meiduo < goods_data.sql
3 py脚本
例: 自动插入10000行记录
import pymysql
db = pymysql.connect(
host='localhost',
port=3306,
user='ws',
password='123456',
database='mytest',
)
cursor = db.cursor()
for i in range(10000):
key1 = 'key1_' + str(i)
key2 = i
key3 = 'key3_' + str(i)
keyp1 = 'keyp1_' + str(i)
keyp2 = 'keyp2_' + str(i)
keyp3 = 'keyp3_' + str(i)
comn = 'comn_' + str(i)
sql = 'insert tb_sgle(key1, key2, key3, key_part1, key_part2, key_part3, common_field) ' \
'values(%s, %s, %s, %s, %s, %s, %s);'
try:
cursor.execute(sql, (key1, key2, key3, keyp1, keyp2, keyp3, comn))
db.commit()
print(i, 'done')
except:
db.rollback()
print(i, 'failed')
print('all done')