简体   繁体   English

更新数据库不起作用; Python 3.4,SQL Azure数据库

[英]Updating database not working ; Python 3.4, SQL Azure DB

I have a database table that includes end user comments (Comments column) and sentiment scores (Sentiment column). 我有一个数据库表,其中包含最终用户评论(“评论”列)和情感分数(“情感”列)。 I am running a sentiment analysis using texblob on the comments column. 我正在评论栏中使用texblob运行情绪分析。

By default the Sentiment column was pre-populated with a sentiment score of 0 (float datatype). 默认情况下,“情感”列的情感评分为0(浮点数据类型)。

I am having issues updating the sentiment score based on the outcome of the sentiment analysis. 我在根据情感分析结果更新情感分数时遇到问题。 The pre-populated value of 0 remains in the table column-so the update isn't working. 表格列中保留了预先填充的0值,因此更新无法正常进行。

The various components appear to work (no errors thrown, print statements output the correct sentiments scores that should be updating the Sentiment column with each loop, If I hardcode the update sql statement that also works, albeited without looping through the rows, DB connection isn't an issue as the sentiment is being calculated...). 各个组件似乎都正常工作(没有错误抛出,print语句输出应该在每个循环中更新Sentiment列的正确的情感分数。如果我对更新的sql语句进行了硬编码,尽管没有循环遍历行,则数据库连接无效这不是问题,因为正在计算情绪...)。

Can someone advise what I am doing wrong? 有人可以告诉我我在做什么错吗? New to programming. 编程新手。

Cheers Steve 干杯史蒂夫

import pypyodbc
from textblob import TextBlob

myConnection = pypyodbc.connect('Driver={SQL Server};'
                                'Server=tcp:AZURESERVER;'
                                'Database=DBNAME;'
                                'uid=USERNAME; pwd=PASSWORD')
myCursor = myConnection.cursor()
SQLCommand =("SELECT Comments FROM [dbo].[ADO NET Destination] ") 
myCursor.execute(SQLCommand)

for row in myCursor.fetchall():
    print(row)
    wiki = TextBlob(str(row))
    print(wiki.polarity)
    print(type(wiki.polarity))
    SQLUPDATECommand =("Update [ADO NET Destination] SET [Sentiment] = ?") 
    value = [wiki.polarity]
    myCursor.execute(SQLUPDATECommand,value)
    myConnection.commit()
myConnection.close()

在此处输入图片说明

Issue was with the UPDATE statement. 问题在于UPDATE语句。 It required a WHERE to be included to specify the exact row that needed to be updated based on the specific iteration of the FOR Loop. 它需要包含一个WHERE,以根据FOR循环的特定迭代来指定需要更新的确切行。 Eg 例如

   SQLUPDATECommand =("Update [ADO NET Destination] SET [Sentiment] = ? WHERE RowID = ?") 
value = [wiki.polarity, row[1]]

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

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