通过脚本,将MySQL数据转换成单条insert语句
通过命令,导出数据:
SELECT * FROM users INTO OUTFILE '/tmp/users_data.sql' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
通过脚本,转成insert语句:
#!/bin/env python
# -*- encoding: utf-8 -*-
#数据导出命令,注意修改表名
#SELECT * FROM users INTO OUTFILE '/tmp/users_data.sql' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
import re
import sys
import time
import subprocess
import os
with open('/tmp/users_data.sql', 'r') as f:
lines = f.readlines()
with open('/tmp/insert_data.sql', 'w') as f:
for line in lines:
data = line.strip().split(',')
insert_statement = "INSERT INTO users VALUES ({})".format(','.join(["'{}'".format(d) for d in data]))
f.write(insert_statement + ';\n')