TypeError:行索引必须是整数,而不是python insert Query中的str



这是我在一个表中插入数据的代码从两个表中选择数据。请帮助识别,我的插入查询中的错误是什么

import pyodbc
import pandas as pd

cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=LAPTOP-6HKL6V6K;"
"Database=DemoTest;"
"Trusted_Connection=yes;")
Anxiety=0
Hostility=0
Depression=0
Confidence=0
Compassion=0
Happiness=0
cursor = cnxn.cursor()
cursor2 = cnxn.cursor()
cursor3 = cnxn.cursor()
data2=('select top(1) ArticleURL,Name,Date,CreationTs,Category,Year,Location,Organization,Person from ClassificationAnalysis ')
cursor.execute(data2)
for row1 in cursor.fetchall():
data=cursor2.execute("Select * From SentimentAnalysis where ArticleURL='%s'"%row1.ArticleURL)
#data=cursor2.execute('select SentimentAnalysis.ArticleURL,ClassificationAnalysis.Year,SentimentAnalysis.Entity,ClassificationAnalysis.Name,ClassificationAnalysis.Date,ClassificationAnalysis.CreationTs,ClassificationAnalysis.Category,ClassificationAnalysis.Location,ClassificationAnalysis.Organization,ClassificationAnalysis.Person ,SentimentAnalysis.Score,SentimentAnalysis.Aspect,SentimentAnalysis.Emotion from ClassificationAnalysis join SentimentAnalysis on ClassificationAnalysis.ArticleURL=SentimentAnalysis.ArticleURL where ClassificationAnalysis.Date=SentimentAnalysis.Date')
for  row in cursor2.fetchall():
#  print(data)
if(row.Emotion=='Anxiety'):
Anxiety=row.Score
# print(Anxiety)
if(row.Emotion=='Hostility'):
Hostility=row.Score
# print(Hostility)
if(row.Emotion=='Depression'):
Depression=row.Score
#   print(Depression)
if(row.Emotion=='Confidence'):
Confidence=row.Score
#   print(Confidence)
if(row.Emotion=='Compassion'):
Compassion=row.Score
# print(Compassion)
if(row.Emotion=='Happiness'):
Happiness=row.Score
#     print(Happiness)
data1=cursor3.execute("INSERT INTO SentimentClassification (ArticleURL, Name, CreationTs, Date, Year, Location, Person, Category, Organization, Aspect, Entity, Score, Anxiety, Hostility, Depression, Confidence, Compassion,Happiness) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"  ,row1['ArticleURL'], row1['Name'], row1['CreationTs'], row1['Date'], row1['Year'], row1['Location'], row1['Person'], row1['Category'], row1['Organization'], row['Aspect'], row['Entity'], row['Score'], Anxiety, Hostility, Depression, Confidence, Compassion,Happiness)
print(data1)

插入查询错误:

TypeError:行索引必须是整数,而不是python插入中的str查询

将倒数第二行替换为

data1 = cursor3.execute("INSERT INTO SentimentClassification (ArticleURL, Name, CreationTs, Date, Year, Location, Person, Category, Organization, Aspect, Entity, Score, Anxiety, Hostility, Depression, Confidence, Compassion,Happiness) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"  ,row1.ArticleURL, row1.Name, row1.CreationTs, row1.Date, row1.Year, row1.Location, row1.Person, row1.Category, row1.Organization, row.Aspect, row.Entity, row.Score, Anxiety, Hostility, Depression, Confidence, Compassion,Happiness)

相关内容

  • 没有找到相关文章

最新更新