使用Python将数据导入SQL Server时出现语法错误

2024-09-30 04:39:56 发布

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

语法错误PYODBC

大家好

我已经看这个代码太久了,现在不能发现错误。我正在尝试将从xml文件中提取的数据导入数据库。当我运行程序时,虽然我得到了一个语法错误,try:被突出显示。我找不到错误。谢谢大家。你知道吗

for x in range(len(sentences)):
    slice2 = sentences[max(0,x-1):min(len(sentences),x+2)]
    sentencelist.append([dewiki.from_string(s) for s in slice2])
    if "theory" in sentences[x].lower():
        slice1 = sentences[max(0,x-1):min(len(sentences),x+2)]
        surroundingsentences.append(['[[[&t]]]' + dewiki.from_string(s) for s in slice1])
        slicelinks.append([(links.findall(s)) for s in slice1])
    query1 = unicode("""INSERT INTO SENTENCE (TheoryDocID, SliceWithMarkUp, SliceText) VALUES ({},'{}','{}')""".format(id, slice2.replace("'","''"), slice1.replace("'","''")
    try:                                                                                                                   
        cur.execute(query1)
        cur.commit()
    except:
        print query1
        conn.close()

Tags: infromforstringlensentencesminmax
1条回答
网友
1楼 · 发布于 2024-09-30 04:39:56

你好像少了两个括号

query1 = unicode("""INSERT INTO SENTENCE (TheoryDocID, SliceWithMarkUp, SliceText) VALUES ({},'{}','{}')""".format(id, slice2.replace("'","''"), slice1.replace("'","''")

应该是:

unicode("""INSERT INTO SENTENCE (TheoryDocID, SliceWithMarkUp, SliceText) VALUES ({},'{}','{}')""".format(id, slice2.replace("'","''"), slice1.replace("'","''")))

相关问题 更多 >

    热门问题