以下函数从数据库中获取数据:
def findid(name,parent):
conn = MySQLdb.connect (host = "localhost",
user = "arunkamaly",
passwd = "code",
db = "se")
cursor=conn.cursor()
cursor.execute(" select xbrl_id from se_xbrl_concepts where xbrl_name=%s;",name)
name=cursor.fetchone()
cursor.execute(" select xbrl_id from se_xbrl_concepts where xbrl_name=%s;",parent)
pname=cursor.fetchone()
cursor.close()
if pname==None:
return name[0],0
return name[0],pname[0]
而这里用到了上面的函数,还介绍了insert方法:
def prsentparse():
conn = MySQLdb.connect (host = "localhost",\
user = "arunkamaly",\
passwd = "code",\
db = "se")
cursor=conn.cursor()
f = open(csvfile, 'rb')
spamReader = csv.reader(f, delimiter=',', quotechar='"')
for clist in spamReader:
if clist[0]=='LinkRole' or clist[0] =='' or clist[0]=='prefix':
continue
name=clist[0]+':'+clist[1]
parent=clist[6].strip()
xid,pid=findid(name,parent)
prio=0 if clist[5].strip()=='' else clist[5]
order=0 if clist[4].strip()=='' else clist[4]
depth=0 if clist[3].strip()=='' else clist[3]
#print clist
#cursor.execute("INSERT INTO se_xbrl_presentation (xbrl_id,xbrl_parent_id,priority,order,depth) VALUES (%s,%s,%s,%s,%s);",(xid,pid,prio,order,depth) )
#sql = "insert into se_xbrl_presentation (xbrl_id, xbrl_parent_id, priority, order, depth) values (" + xid + ", " + pid + ", " + prio + ", " + order + ", " + depth + ");"
try:
cursor.execute("INSERT INTO `se_xbrl_presentation`(`xbrl_id`, `xbrl_parent_id`, `priority`, `order`, `depth`) VALUES (%s,%s,%s,%s,%s);",(xid,pid,prio,order,depth) )
except MySQLdb.Error,e:
print "mysql Error %d:%s"%(e.args[0],e.args[1])
conn.commit
cursor.close()
这种方法似乎太慢了。您能否建议任何提高性能的改进?