def read_csv_to_mysql(filename):
with codecs.open(filename=filename, mode='r', encoding='utf-8') as f:
reader = csv.reader(f)
head = next(reader)
conn = get_conn()
cur = conn.cursor()
sql = 'insert into tb_csv values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
for item in reader:
if item[1] is None or item[1] == '': # item[1]作为唯一键,不能为null
continue
args = tuple(item)
print(args)
insert(cur, sql=sql, args=args)
def read_mysql_to_csv(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 tb_csv'
results = query_all(cur=cur, sql=sql, args=None)
for result in results:
print(result)
write.writerow(result)