当将twitter值插入mysql数据库时,我得到“execeptionoccured:must be str“错误

nr7wwzry  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(353)

我正在尝试将tweets插入mysql数据库。
我有程序拉推,连接和创建数据库(如果没有创建)成功。
但是,我得到了一个错误:

Exeception occurred: must be str, not int

当我运行它我得到这个错误。我不知道如何找出它所指的是哪个值?知道是什么原因吗?
我认为是我给mysql列分配了错误的数据类型。因此,当我尝试将int插入str列时,它会抛出错误。
但我已经经历过很多次了,这似乎不是为什么。。。


# !/Library/Frameworks/Python.framework/Versions/3.6/bin/python3

from twitter_api import ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET

# import modules

from pymongo import MongoClient
import tweepy
import datetime
import pymysql.cursors

# Connect to Twitter API

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)

# Connect to MYSQL database

dbServerName = "localhost"
dbUser = "root"
dbPassword = "woodycool123"
dbName = "azure_support_tweets"
cusrorType = pymysql.cursors.DictCursor

connectionObject = pymysql.connect(host=dbServerName, user=dbUser, password=dbPassword, db=dbName, charset='utf8mb4', cursorclass=cusrorType)

# Get 1000 tweets

searchQuery = "@azuresupport"
searched_tweets = []
last_id = -1
max_tweets = 1000

while len(searched_tweets) < max_tweets:
    count = max_tweets - len(searched_tweets)
    try:
        new_tweets = api.search(q=searchQuery, count=100, max_id=str(last_id - 1))
        if not new_tweets:
            break
        searched_tweets.extend(new_tweets)
        last_id = new_tweets[-1].id
    except tweepy.TweepError as e:
        # depending on TweepError.code, one may want to retry or wait
        # to keep things simple, we will give up on an error
        break

# Create table for raw tweets if there is none

try:
    # Create a cursor object
    cursorObject = connectionObject.cursor()

    # SQL query string
    sqlQuery = "CREATE TABLE IF NOT EXISTS raw_tweets(id_tweet int, id_str_status varchar(32), text_status varchar(200), created_at_status DATETIME(6), truncated varchar(32), in_reply_to_screen_name varchar(32), retweet_count int, favorite_count int, retweeted varchar(32), lang_status varchar(32), id_user int, id_str_user varchar(32), name_user varchar(32), screen_name varchar(32), location_user varchar(32), description_user varchar(32), url_user varchar(100), followers_count_user_user int, favourites_count_user int, lang_user varchar(32))"

    # Execute the sqlQuery
    cursorObject.execute(sqlQuery)

    # Add a row for each tweet
    for tweet in searched_tweets:
        if tweet.user.screen_name != "azuresupport":
            # Assign values to variables
            id_tweet = tweet.id
            id_str_status = str(tweet.id_str)
            text_status = tweet.text
            created_at_status = tweet.created_at.strftime('%Y-%m-%d %H:%M:%S')
            truncated = tweet.truncated
            in_reply_to_screen_name = tweet.in_reply_to_screen_name
            retweet_count = tweet.retweet_count
            favorite_count = tweet.favorite_count
            retweeted = tweet.retweeted
            lang_status = tweet.lang
            # Fields from the user object
            id_user = tweet.user.id
            id_str_user = tweet.user.id_str
            name_user = tweet.user.name
            screen_name_user = tweet.user.screen_name
            location_user = tweet.user.location
            description_user = tweet.user.description
            url_user = tweet.user.url
            followers_count_user_user = tweet.user.followers_count
            favourites_count_user = tweet.user.favourites_count
            lang_user = tweet.user.lang
            # Insert row
            values = " VALUES (" + id_tweet + id_str_status + text_status + created_at_status + truncated + in_reply_to_screen_name + retweet_count + favorite_count + retweeted + lang_status + id_user + id_str_user + name_user + screen_name + location_user + description_user + url_user, + followers_count_user_user + favourites_count_user + lang_user + ");"
            addrowQuery = "INSERT INTO raw_tweets (id_tweet, id_str_status, text_status, created_at_status, truncated, in_reply_to_screen_name, retweet_count, favorite_count, retweeted, lang_status), id_user, id_str_user, name_user, screen_name, location_user, description_user, url_user, followers_count_user_user, favourites_count_user, lang_user) " + values
            cursorObject.execute(addrowQuery)
            #commit
            connectionObject.commit()

except Exception as e:
    print("Exeception occured:{}".format(e))

finally:
    connectionObject.close()
piah890a

piah890a1#

我使用了错误的数据类型。。python与mysql数据库
我用类型(i)和mysql命令descripe table raw\u tweets检查了所有的数据类型;发现我分配了错误的数据类型。
谢谢!

相关问题