Skip to content

Instantly share code, notes, and snippets.

@lazybios
Last active August 29, 2015 14:12
Show Gist options
  • Save lazybios/005f55ff80fe84369470 to your computer and use it in GitHub Desktop.
Save lazybios/005f55ff80fe84369470 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''导入csv格式数据到mysql数据库'''
import csv
import MySQLdb as mysqldb
import time
mytime = time.strftime('%Y-%m-%d %H:%M:%S')
try:
con = mysqldb.connect('localhost','root','root','issues_development',charset='utf8')
cur = con.cursor()
with open('issue_info.csv','rb') as fd:
t = csv.reader(fd,delimiter=';',quotechar='"')
for row in t:
row[0] = str(int(row[0]) - 1)
#避免sql中字符转义带来问题
sql = '''INSERT INTO `errors` (`id`, `code`, `info`, `tips`, `type`, `cas e`, `notes`) VALUES (NULL, %s, %s, %s, %s, %s, %s);'''
20 cur.execute(sql,(row[1],row[2],row[3],row[5],row[6],row[4]))
con.commit()
except mysqldb.Error,e:
print e
finally:
if con:
con.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment