当从触发器在 MySQL 表中添加记录时,Python 脚本中的 While 循环不起作用

问题描述 投票:0回答:1

我有以下MySQL触发脚本

CREATE TRIGGER table_1_insert
AFTER INSERT ON Table1
FOR EACH ROW
BEGIN
    INSERT INTO Logs (time, table_name)
    VALUES (NOW(), 'Table1');
END;

我还有以下Python脚本

def listen_to_triggers():
    try:
        conn = mysql.connector.connect(
            host="localhost",
            port=<port>,
            database=<db_name>,
            user=<user>,
            password=<password>,
        )

        if conn.is_connected():
            print("Connected to MySQL database")

        while True:
            cursor = conn.cursor()

            # Query for new trigger events
            cursor.execute("SELECT * FROM logs)
            rows = cursor.fetchall()

            for row in rows:
 
                print("Time:", row[1])
                print("Table Name:", row[2])

            cursor.close()
            time.sleep(1)

    except Error as e:
        print(e)

    finally:
        if conn.is_connected():
            conn.close()
            print("Connection closed")

if __name__ == "__main__":
    listen_to_triggers()

我面临的问题是当我运行Python脚本并在Table1中插入一条记录时,打印不显示。当我停止脚本并重新运行它时,我得到了数据的打印。 Python while 循环在第一次运行时仍在运行,但没有打印出来。

非常感谢您对此的支持。谢谢。

while-loop triggers
1个回答
0
投票

如何运行Python脚本?

© www.soinside.com 2019 - 2024. All rights reserved.