I read a column's length in the code given below. Then, I have a list fakeNames whose length is equal to the column's original length.
How can I replace the values of the original column person_nameswith my list fakeNamesvalues? The changes should be reflected in the database.
import MySQLdb
# Connect
db = MySQLdb.connect(
host="abc.us-east-1.rds.amazonaws.com",
user="abc",
password="password",
)
cursor = db.cursor()
# Execute SQL select statement
cursor.execute("SELECT COUNT(person_names) FROM z3_table")
number_of_names = (cursor.fetchone()[0])
print(number_of_names)
fakeNames = []
# Commit your changes if writing
# In this case, we are only reading data
# db.commit()
# Close the connection
db.close()
This means that each value from the list fakeNamesshould be in a different row.
fakeNamesto the DB? I'm not sure what you want to achieve? Replace all names with fake names? In what order, how do you plan to connect them, etc?person_name = "John"and you want to replace that name with "Mark", you would need to write something likeupdate z3_table set name="Mark" where name="John". So you need to have some kind of "rule" that will help you identify what to change and where. Or do you want to change every name to a random name, but even then you will need a "rule" to update data, or to ensure that all data has been updated.fakeNamesbut not sure how to do that since there are hundreds of rows). Another option could be to create a new columnnewColand insert all values fromfakeNamesthere. Then I could delete the original names column. However, I am not sure how to do either of them syntax wise @PerunSS