简体   繁体   English

flask sqlalchemy 更新 sql 原始命令不提供任何响应

[英]flask sqlalchemy update sql raw command does not provide any response

I'm trying to perform an update using flask-sqlalchemy but when it gets to the update script it does not return anything.我正在尝试使用 flask-sqlalchemy 执行更新,但是当它到达更新脚本时它不会返回任何内容。 it seems the script is hanging or it is not doing anything.似乎脚本挂起或没有做任何事情。

I tried to wrap a try catch on the code that does not complete but there are no errors.我试图在未完成但没有错误的代码上包装 try catch。

I gave it 10 minutes to complete the update statement which only updates 1 record and still, it will not do anything for some reason.我给了它 10 分钟来完成只更新 1 条记录的更新语句,但由于某种原因它仍然不会做任何事情。

When I cancel the script, it provides an error Communication link failure (0) (SQLEndTran) but I don't think this is the root cause of the error because on the same script, I have other sql scripts that works ok so the connection to db is good当我取消脚本时,它提供了一个错误通信链接失败 (0) (SQLEndTran)但我认为这不是错误的根本原因,因为在同一个脚本上,我还有其他 sql 脚本可以正常工作,所以连接到分贝很好

what my script does is get some list of filenames that I need to process (I have no issues with this).我的脚本所做的是获取一些我需要处理的文件名列表(我对此没有任何问题)。 then using the retrieved list of filenames, I will look into the directory to check if the file exists.然后使用检索到的文件名列表,我将查看目录以检查文件是否存在。 if it does not exists, I will update the database to tag the file as it is not found.如果它不存在,我将更新数据库以标记文件,因为它没有找到。 this is where I get the issue, it does not perform the update nor provide an error message of some sort.这是我遇到问题的地方,它不执行更新也不提供某种错误消息。

I even tried to create a new engine just for the update script, but still I get the same behavior.我什至尝试为更新脚本创建一个新引擎,但我仍然得到相同的行为。

I also tried to print out the sql script first in python before executing.我还尝试在执行之前先在 python 中打印出 sql 脚本。 I ran the printed sql command on my sql browser and it worked ok.我在我的 sql 浏览器上运行了打印的 sql 命令,它运行正常。

The code is very simple, I'm not really sure why it's having the issue.代码非常简单,我不确定为什么会出现问题。

#!/usr/bin/env python3

from flask_sqlalchemy import sqlalchemy
import glob


files_directory = "/files_dir/"

sql_string = """
    select * 
    from table
    where status is null
    """

# ommited conn_string
engine1 = sqlalchemy.create_engine(conn_string)
result = engine1.execute(sql_string)

for r in result:
    engine2 = sqlalchemy.create_engine(conn_string)
    filename = r[11]
    match = glob.glob(f"{files_directory}/**/{filename}.wav")

    if not match:
        print('no match')

        script = "update table set status = 'not_found' where filename = '" + filename + "' "
        engine2.execute(script)
        engine2.dispose()

    continue

engine1.dispose()

it appears that if I try to loop through 26k records, the script doesn't work.看来,如果我尝试遍历 26k 记录,则脚本不起作用。 but when I try to do by batches of 2k records per run, then the script will work.但是当我尝试每次运行批量 2k 记录时,脚本将起作用。 so my sql string will become (added top 2000 on the query)所以我的 sql 字符串将变为(在查询中添加前 2000 个)

sql_string = """
    select top 2000 * 
    from table
    where status is null
    """

it's manual yeah, but it works for me since I just need to run this script once.它是手动的,但它对我有用,因为我只需要运行这个脚本一次。 (I mean 13 times) (我的意思是 13 次)

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

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