简体   繁体   中英

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.

By default the Sentiment column was pre-populated with a sentiment score of 0 (float datatype).

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.

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

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. 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. Eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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