0

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.

Jnl
  • 194
  • 1
  • 11
  • The code that you've written is just counting names, what have you tried to add `fakeNames` to 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? – Aleksandar Varicak Jun 16 '22 at 10:55
  • I am not sure how I can add a whole list and replace the existing column values. Not sure how to formula a query with the correct syntax. That's the question...order doesn't matter. @PerunSS – Jnl Jun 16 '22 at 11:32
  • You need to have conditions for the update, or all values will be updated to the "same value". Let's say you have `person_name = "John"` and you want to replace that name with "Mark", you would need to write something like `update 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. – Aleksandar Varicak Jun 16 '22 at 13:53
  • Yes, I want to update all names to random names (from the list ```fakeNames``` but not sure how to do that since there are hundreds of rows). Another option could be to create a new column ```newCol``` and insert all values from ```fakeNames``` there. Then I could delete the original names column. However, I am not sure how to do either of them syntax wise @PerunSS – Jnl Jun 16 '22 at 14:13
  • Maybe this can help: https://stackoverflow.com/questions/41885936/get-random-array-value-in-mysql – Aleksandar Varicak Jun 17 '22 at 06:46

0 Answers0