https://prefect.io logo
f

Florian Guily

05/12/2022, 4:10 PM
hey, i'm trying to insert data in mysql using the
MySqlExecute
task. I have no error (i had some but i fixed them) but the row i try to insert isn't showing in the table. I can do it with the same user from mysql workbench and the
MySqlExecute
task return 1 as expected. I don't understand why, maybe i'm doing something wrong. Here is a code example of my task:
Copy code
@task
def set_new_date():
    logger = prefect.context.get("logger")
    new_date = datetime.date.today().strftime("%Y-%m-%d")
    query = f"""
        INSERT INTO `name`.`table_name` (`name`, `value`) VALUES ('name', '{new_date}');
    """
    result = MySQLExecute(
        db_name="name",
        user="user",
        password="pwd",
        host="host",
        port=3306,
        query=query).run()
    <http://logger.info|logger.info>("New date is {}".format(new_date))
    <http://logger.info|logger.info>("Query results is {}".format(result))
k

Kevin Kho

05/12/2022, 4:14 PM
It’s hard to tell. This looks right. When happens when you use
MySQLExecute
outside of a task?
f

Florian Guily

05/12/2022, 4:26 PM
I tried directly inside the flow like this:
Copy code
query = f"""
        INSERT INTO `name`.`table_name` (`name`, `value`) VALUES ('name', '2022-05-12');
    """
    flow.add_task(MySQLExecute(
        db_name="name",
        user="user",
        password="pwd",
        host="host",
        port=3306,
        query=query))
And same thing. The task is marked as successful in the logs but the row is not showing up
k

Kevin Kho

05/12/2022, 5:05 PM
Could you try the Python code under the hood and seeing if it works.
a

Anna Geller

05/12/2022, 7:23 PM
this is sort of a duplicated question - I answered here
f

Florian Guily

05/13/2022, 10:04 AM
oh ok thanks didn't see your reply ! i'll try that
Jeez the prob was that the commit arg had to be set on True for the query to be executed...
Problem solved thanks !
a

Anna Geller

05/13/2022, 11:08 AM
Nice work and thanks for the update!
5 Views