【问题标题】:PYODBC MS Access Insert Error - Too Few ParametersPYODBC MS 访问插入错误 - 参数太少
【发布时间】:2016-02-19 13:38:06
【问题描述】:

我目前正在从事一个项目,该项目需要推文并进行快速情绪分析,并将推文 ID、推文日期、推文文本和推文的情绪极性加载到 MS Access 表中(Office 2013 )。我已将输出转换为字符串,除了情感极性以匹配表(测试)数据类型。这是我的代码:

from pattern.web import Twitter
import time
import pyodbc
from textblob import TextBlob

cnxn = pyodbc.connect(r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:...SentimentAnalysis.accdb;')
cursor = cnxn.cursor()

s = Twitter().stream('#testhash')
for i in range(25):
    time.sleep(1)
    s.update(bytes=1024)
    if s:
        Ident = str(s[-1].id)
        TweetDate = str(s[-1].date)
        TweetText = str(s[-1].text.encode('utf8'))
        x = s[-1].text
        blob = TextBlob(x)
        sent = blob.sentiment.polarity
        cursor.execute('insert into Test([Ident],[TweetDate],[TweetText],[TweetSentiment]) values (Ident,TweetDate,TweetText,sent);')
        cnxn.commit()
        print 'Inserted: ', Ident
    else: ''
    s.clear()

问题是我收到以下错误:

pyodbc.Error: ('07002', '[07002] [Microsoft][ODBC Microsoft Access 驱动程序] 参数太少。预期 4. (-3010) (SQLExecDirectW)')

我已经看到其他有关此错误的帖子,但大多数只是由于 Access 中的双引号问题。由于我只使用了单引号并且仍然遇到问题,我很难过。这让我发疯!任何帮助将不胜感激!

【问题讨论】:

    标签: python twitter ms-access-2013 pyodbc


    【解决方案1】:

    您的 SQL 语句包含标识符 IdentTweetDate 等作为文字文本,并且 Access 数据库引擎将它们视为参数占位符。您需要做的是在命令文本中创建 actual 参数占位符,然后传递参数 values。例如:

    sql = """\
    INSERT INTO [Test] ([Ident], [TweetDate], [TweetText], [TweetSentiment]) 
    VALUES (?,?,?,?)
    """
    params = (Ident, TweetDate, TweetText, sent)
    cursor.execute(sql, params)
    cnxn.commit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多