2016-12-05 65 views
1

我有一个基本的问题,其中多个插入到mysql表不能通过python代码工作。多个插入到mysql不工作在python

for pair in zip(documentcount,publicationpaper,title,byline,section,length,url,graphic,language,subject,company,organization,ticker,industry,person,city,state,country,loaddate,copyrightinfo,MainText): 
      mainlist.append(pair) 

cur.executemany("""INSERT INTO financedatasetsample (DOCCOUNT,PUBLICATION,TITLE,BYLINE,SECTION,LENGTH,URL,GRAPHIC,LANGUAG,SUBJEC,COMPANY,ORGANIZATION,TICKER,INDUSTRY,PERSON,CITY,STATE,COUNTRY,LOADDATE,COPYRIGHT) 
     VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", (mainlist)) 

其中zip中的所有元素都是单独的列表,我试图追加到成功完成的主列表。但问题出现了,当我尝试执行以下executemany查询它是不是插入并抛出我下面的错误“AttributeError的:‘MySQLConverter’对象有没有属性‘_list_to_mysql’”

我尝试另一种方法在那里仅插入第一行而不插入后续行。

for p in mainlist: 
      format_str = """INSERT INTO financedatasetsample (DOCCOUNT,PUBLICATION,TITLE,BYLINE,SECTION,LENGTH,URL,GRAPHIC,LANGUAG,SUBJEC,COMPANY,ORGANIZATION,TICKER,INDUSTRY,PERSON,CITY,STATE,COUNTRY,LOADDATE,COPYRIGHT,MAINTEXTBODY) 
      VALUES ('{DocumentCount}', '{Publication_Type}', '{Title}', '{Byline}','{Section}', '{Length}', '{Url}', '{Graphic}','{Language}', '{Subject}', '{Company}', '{Organization}','{Tickersymbol}', '{Industry}', '{Person}', '{City}','{State}','{Country}', '{Load_Date}', '{Copyrightinfo}','{MainText}');""" 
      sql_command1 = format_str.format(DocumentCount=p[0], Publication_Type=p[1], Title=p[2], Byline=p[3], Section=p[4],Length=p[5], Url=p[6], Graphic=p[7], Language=p[8],Subject=p[9], Company=p[10], Organization=p[11],Tickersymbol=p[12], Industry=p[13], Person=p[14], City=p[15],State=p[16], Country=p[17],Load_Date=p[18], Copyrightinfo=p[19], MainText=p[20]) 
      print(sql_command1) 
      cur.execute(sql_command1) 

请帮我解决问题。

+0

运行'在(documentcount,publicationpaper,标题,署名,部分列元组, print(set(map(type,column)))''所以我们可以验证它是否有效所有的价值都是明智的类型。 –

回答

1

正确的方法是:

c.executemany(
     """INSERT INTO breakfast (name, spam, eggs, sausage, price) 
     VALUES (%s, %s, %s, %s, %s)""", 
     [ 
     ("value1", "value2", "value3", "value4", "value5"), 
     ("value11", "value22", "value33", "value44", "value55"), 
     ]) 

确保您mainlist包含有相同数量的值作为你的发言列