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

时间:2014-04-15 20:16:04

标签: python python-2.7 syntax-error pyodbc

语法错误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()

1 个答案:

答案 0 :(得分:2)

您似乎错过了该行的两个右括号

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("'","''")))