简体   繁体   English

PYODBC MS Access插入错误-参数太少

[英]PYODBC MS Access Insert Error - Too Few Parameters

I am currently working on a project that takes Tweets and conducts a quick sentiment analysis and loads the tweet ID, the date of the tweet, the text of the tweet, and the tweet's sentiment polarity into an MS Access Table (Office 2013). 我目前正在一个项目中,该项目需要使用推文并进行快速的情感分析,并将推文ID,推文的日期,推文的文本以及推文的情感极性加载到MS Access Table(Office 2013)中。 I've converted the outputs to strings, except for the sentiment polarity to match the table (Test) data types. 我已将输出转换为字符串,但情感极性要与表(测试)数据类型匹配。 Here's my code: 这是我的代码:

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()

The problem is that I get the following error: 问题是我得到以下错误:

pyodbc.Error: ('07002', '[07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 4. (-3010) (SQLExecDirectW)') pyodbc.Error :(“ 07002”,“ [07002] [Microsoft] [ODBC Microsoft Access驱动程序]参数太少。预期为4。(-3010)(SQLExecDirectW)”)

I've seen other posts on this error, but most have just been due to issues with double quotes in Access. 我已经看到过有关此错误的其他文章,但大多数只是由于Access中双引号引起的。 As I have only used single quotes and still get the issue, I am stumped. 由于我只用单引号引起了问题,所以我很困惑。 This is driving me nuts! 这真让我发疯! Any help would be greatly appreciated! 任何帮助将不胜感激!

Your SQL statement is including the identifiers Ident , TweetDate , etc., as literal text and the Access Datatabase Engine is considering them to be parameter placeholders. 您的SQL语句包括标识符IdentTweetDate等,作为原义文本,而Access Datatabase Engine则将其视为参数占位符。 What you need to do is create actual parameter placeholders in your command text and then pass the parameter values . 您需要做的是在命令文本中创建实际的参数占位符,然后传递参数 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM