gpt4 book ai didi

mysql 整数数据插入错误

转载 作者:行者123 更新时间:2023-11-29 21:20:04 25 4
gpt4 key购买 nike

我有一个数据库

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 列时,我收到错误。

raise errors.InternalError("Unread result found.") mysql.connector.errors.InternalError: Unread result found.

请建议我如何使用我能够检索的tweet_no 将计算出的分数插入数据库的senti_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 整数数据插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35759535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com