简体   繁体   English

从 Python 脚本调用 SQL 5.6 例程

[英]CALL a SQL 5.6 routine from a Python script

I'm trying to write a Python script which will anonymise users stored in a production database in bulk.我正在尝试编写一个 Python 脚本,它将批量匿名存储在生产数据库中的用户。 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:运行时返回如下output:

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.我会很感激我在这里可能出错的任何想法。

execute() is not execute multiple statements, you try to multi=False and delete the semicolon. execute() 不是执行多条语句,你尝试 multi=False 并删除分号。

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()
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.确保 csv 中的数据以逗号分隔。 For example, there were only one row with no commas, the code would look like in the above.例如,只有一行没有逗号,代码如上所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM