赞
踩
附:mysql数据导出为csv文件的脚本,不过好像对中文支持有点问题
import traceback import csv import codecs import sys def get_conn(): conn = MySQLdb.connect(host='10.xxxx', port=xxxx, user='xxxx', passwd='xxxx', db='xxxx', charset='utf8') return conn def query_all(cur, sql, args): cur.execute(sql, args) return cur.fetchall() def read_mysql_to_csv(table, filename): with codecs.open(filename=filename, mode='w', encoding='utf-8') as f: write = csv.writer(f, dialect='excel') conn = get_conn() cur = conn.cursor() sql = 'select * from %s' % table results = query_all(cur=cur, sql=sql, args=None) for result in results: print(",".join('%s' %var for var in result)) write.writerow(result) if __name__ == '__main__': table = sys.argv[1] outfile = sys.argv[2] read_mysql_to_csv(table, outfile)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。