我有一个数据库
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是主键
我想做的是检索所有行的text和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列时,出现错误。
引发错误。InternalError(“找到未读的结果。”)
mysql.connector.errors.InternalError:发现未读结果。
请建议我如何使用tweet_no将计算出的分数插入数据库的sendi_score列中,我可以将其获取。还有其他插入乐谱的方法吗,我可以在这里使用,例如将乐谱附加到列表中然后插入。
最佳答案
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)
关于mysql - 整数数据的mysql插入错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35759535/