0

I'm trying to write a Python script which will anonymise users stored in a production database in bulk. This is using an existing routine which has been used many times before and works fine.

My code looks like this:

with open('users_to_delete.csv', newline='') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',', quotechar='|')
for row in csv_reader:
    user_to_delete = row[0]
    delete_query = """CALL anonymise_accounts(""" + user_to_delete + ');'
    cursor.execute(delete_query, multi=True)
    db_connection.commit()

When running it, the following output is returned:

CALL anonymise_accounts(5308561);
CALL anonymise_accounts(2558082);

However, the accounts don't get anonymised.

I'd appreciate any ideas where I may be going wrong here.

3
  • try CALL anonymise_accounts(5308561); directly in database to see if it really works. maybe this function doesn't delete accounts. Commented Dec 23, 2020 at 1:14
  • Yes, as mentioned in my post it’s a script that use regularly and works with the print from delete_query. Commented Dec 23, 2020 at 1:24
  • I wouldn't trust it. I would check all again on my own. I would run check code again directly in database and later I would check if code connect to correct database. I don't see any other reason. Commented Dec 23, 2020 at 1:36

2 Answers 2

1

execute() is not execute multiple statements, you try to multi=False and delete the semicolon.

with open('users_to_delete.csv', newline='') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',', quotechar='|')
for row in csv_reader:
    user_to_delete = row[0]
    delete_query = """CALL anonymise_accounts(""" + user_to_delete + ')'
    cursor.execute(delete_query, multi=False)
    db_connection.commit()
Sign up to request clarification or add additional context in comments.

1 Comment

When I do that I get the following: Traceback (most recent call last): File "./bulk_del.py", line 64, in <module> cursor.execute(delete_query, multi=False) File "/usr/local/lib/python3.8/site-packages/mysql/connector/cursor.py", line 554, in execute raise errors.InterfaceError( mysql.connector.errors.InterfaceError: Use multi=True when executing multiple statements
0
with open('users_to_delete.csv', newline='') as csv_file:
csv_reader = csv.reader(csv_file, delimiter='\n', quotechar='|')
for row in csv_reader:
    user_to_delete = row[0]
    delete_query = """CALL rp_anonymise_ho(""" + user_to_delete + ')'
    cursor.execute(delete_query)
    print(cursor.fetchall())

Make sure your data in the csv is comma delimited. For example, there were only one row with no commas, the code would look like in the above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.