Skip to content

code inserting only one character per row into mysql library #2

@starp41

Description

@starp41

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.

Screenshot: https://ibb.co/k0jNd59

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("CryptoCurrency+BitcoinMarkets+CryptoCurrencies+cryptomarkets+cryptocurrencytrading+cryptotrading+ethrader+Crypto_Currency_News").stream.comments():
    rdtext = str(comment.body)
    db = (rdtext)
    mycursor.executemany(sql, db)
    mydb.commit()
    print(comment.body)`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions