I have this (somewhat) working script that takes comments from a Reddit subreddit and insert them into a MySQL database. The spaghetti code I ended up with, however, pastes one character from each comment to each row of the database instead of one comment per row.
I have searched through but couldn't find any instance like this on previous cases. Below is a screenshot of how MySQL look like + the code snippet.
import praw
import configReddit
import mysql.connector
import configMySQL
# ---------------- REDDIT ---------------- #
reddit = praw.Reddit(
client_id=configReddit.client_id,
client_secret=configReddit.client_secret,
password=configReddit.password,
user_agent=configReddit.user_agent,
username=configReddit.username,
charset='utf8mb4'
)
# ---------------- DATABASE ---------------- #
mydb = mysql.connector.connect(
host=configMySQL.host,
user=configMySQL.user,
password=configMySQL.password,
database="db_blockchainstable"
)
mycursor = mydb.cursor()
sql = "INSERT INTO test (rdtext) VALUES (%s)"
# ---------------- EXE ---------------- #
for comment in reddit.subreddit("news").stream.comments():
mycursor.executemany(sql, comment.body)
mydb.commit()
print(comment.body)
Below what the console is returning based on the print(comment.body).
Also, if I change mycursor.executemany(sql, db) to mycursor.execute(sql, db) I get this error:
If I wrap %s in ' ' --> '%s' the database records %s as value (see below).