Replace Table Name With Variable. Using Python And Mysql Connector
I would like to dynamically change the variable name of the table I insert data into. This currently works, def dataEntry(subreddit, _title, _post_url, _imageURL): cnx = mysql
Solution 1:
The exception that is showing mysql connector is telling you that the table doesn't exist in your database.
In addition, you're trying to use 'MachinePorn' as the argument but you didn't define that in the query, it's hardcoded 'subredditName'.
I think you should define database as another parameter in the query and it will run fine:
def dataEntry(subreddit, _title, _post_url, _imageURL):
cnx = mysql.connector.connect(**config)
c = cnx.cursor()
insert = cnx.escape_string("INSERT INTO MachinePorn (subreddit, title, post_url, imageURL) VALUES (%s, %s, %s, %s)")
data_value = (subreddit, _title, _post_url, _imageURL)
c.execute(insert, data_value)
cnx.commit()
c.close()
cnx.close()
dataEntry("fake", "fake", "fake", "fake")
Post a Comment for "Replace Table Name With Variable. Using Python And Mysql Connector"