我有一个数据库
raw_data(
Field, Type, Null, Key, Default, Extra, id
'tweet_no', 'int(11)', 'NO', 'PRI', NULL, 'auto_increment'
'user_id', 'varchar(50)', 'YES', '', NULL, ''
'text', 'varchar(200)', 'YES', '', NULL, ''
'senti_score', 'int(11)', 'YES', '', NULL, ''
)
tweet_no是主键 我想要做的是检索所有行的文本和tweet_no。分析文本并将如此计算的分数插回数据库。请参阅下面的代码:
import mysql.connector
cnx=mysql.connector.connect(host='localhost',user='root',password='root' ,database='db')
cursor = cnx.cursor()
cu_insert = cnx.cursor()
query = ("SELECT text, tweet_no FROM raw_data")
cursor.execute(query)
for (text,tweet_no) in cursor:
tweet = str(text)
no = int(tweet_no)
print(no)
print(':::')
print(tweet)
print('\n')
print(preprocess(tweet))
到此为止,我的代码正常工作,因为我能够获取数据并对其进行处理。
score=0
for term in terms_stop:
if term in scores.keys(): #Look up each token in the scores dict
print(term)
print(scores[term])
score = score + int(scores[term]) #If the term matches, assign the term score and calculate the total score of each tweet
else:
score = score
print('..............')
print(score)
print('..............')
addto_raw_data=("INSERT INTO raw_data(senti_score) WHERE tweet_no=no VALUES(%s)",(score))
cu_insert.execute(addto_raw_data)
print('inserted')
cnx.commit()
cursor.close()
cu_insert.close()
cnx.close()
在尝试执行此部分代码以将分数插入上述数据库的senti_score列时,我收到错误。
引发errors.InternalError("找到未读结果。") mysql.connector.errors.InternalError:找到未读结果。
请建议我如何使用我能够检索的tweet_no将计算得分插入数据库的senti_score列。是否还有其他方法可以插入分数,我可以在这里使用,例如将分数附加到列表然后插入它们。
答案 0 :(得分:0)
try:
update_raw_data=("UPDATE raw_data SET senti_score=%s WHERE tweet_no=%s ",(score,no))
cu_insert.execute(*update_raw_data)
print('inserted the score')
cnx.commit()
except mysql.connector.Error as err:
print(err.msg)