Skip to content
Advertisement

inserting to database from query in python

I have a table of tweet data my_tweets containing these columns: tweet_id, text, created_at, sentiment

My tweet data is being streamed live and simultaneously inserted into the table. I am writing code using Vader to get the sentiment of the text. At the moment, tweet_id, text, and created_at are all populated.

I would like to use this query: SELECT * FROM my_tweets WHERE sentiment is null in MySQL to get rows where sentiment is null and insert the sentiment from Vader.

Any suggestions on how I can go about this?

My program currently looks like this:

Advertisement

Answer

As you put it this cannot be done in one go, you would have to use several steps.

  1. Save the data in a DataFrame: Return the results, because you are doing a data science related application and AI I recommend you to use pd.read_sql (the documentation) from Pandas to pass the result to a DataFrame.
  1. Perform the operations you make with the data.

  2. Insert the results. I recommend you to do it in a different def so that the quality and readability of your code is better. This documentation explains how to make these insertions.

I hope I have helped you!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement