处理python代码中的语法错误

时间:2013-10-23 02:14:27

标签: python sql

import csv, sqlite3

con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("CREATE TABLE TWITTER_HANDLE (twitter_handle TEXT);")

with open('/Users/hyunjincho/Desktop/twitter_handle.csv','r') as fin:
     reader = csv.reader(fin)
     dicts = ({'twitter_handle': line[0]} for line in reader)
     to_db = ((i['twitter_handle']) for i in dicts)
cur.execute('INSERT INTO TWITTER_HANDLE (twitter_handle)', to_db)
con.commit()

python在行

处抛出错误
  cur.execute('INSERT INTO TWITTER_HANDLE (twitter_handle)', to_db)

错误消息是

   near ")": syntax error

我不知道为什么python会抛出此错误,因为我在代码中没有看到任何错误。 但是因为我是python的新手,所以可能会出现一个我无法发现的错误。

编辑:  以下是错误的完整追溯

        OperationalError                          Traceback (most recent call last)
        <ipython-input-15-9039bb2204be> in <module>()
        46     to_db = ((i['twitter_handle']) for i in dicts)
        47 
   ---> 48 cur.execute('INSERT INTO TWITTER_HANDLE (twitter_handle)', to_db)
        49 con.commit()
        50 

   OperationalError: near ")": syntax error

1 个答案:

答案 0 :(得分:0)

我认为语法应该是:

cur.execute('INSERT INTO TWITTER_HANDLE (twitter_handle) VALUES(?)', to_db)

?引用参数to_db

的位置