mysql的多重插入在python中不起作用

2024-05-17 15:27:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个基本问题,即mysql表的多个insert不能通过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中的所有元素都是独立的列表,我正在尝试附加到mainlist,这是成功完成的。但是当我尝试执行下面的executemany查询时,问题出现了,它没有插入,并抛出下面的错误“AttributeError:'MySQLConverter'object has no attribute'\u list \u to \u 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)

请帮我解决这个问题。你知道吗


Tags: informatforsqlmysqlzipinsertcur
1条回答
网友
1楼 · 发布于 2024-05-17 15:27:54

正确的方法是:

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包含与语句列具有相同数值的元组

相关问题 更多 >