Python: MySQL is not updating record despite of using Commit

Viewed 23

I am facing a weird issue. I have the following code. The INSERTS go well but the update query does not work at all. The rowcount is also shown 1 still when I check in Table Plus it does not reflect.

When I directly run the query UPDATE shop_links set product_status = 3 where shop_url = 'https://example.com' in TablePlus it does show record.

The irony is, the update query which set to 1 works just fine and updates instantly

import mysql.connector
def get_connection(host, user, password, db_name):
    connection = None
    try:
        # connection = pymysql.connect(host=host,
        #                              user=user,
        #                              password=password,
        #                              db=db_name,
        #                              charset='utf8',
        #                              max_allowed_packet=1073741824,
        #                              cursorclass=pymysql.cursors.DictCursor)
        connection = mysql.connector.connect(
            host=host,
            user=user,
            use_unicode=True,
            password=password,
            database=db_name

        )
        connection.set_charset_collation('utf8')
        print('Connected')
    except Exception as ex:
        print(str(ex))
    finally:
        return connection
    with connection.cursor() as cursor:
        sql = 'INSERT INTO {} (shop_url,product_url) VALUES (%s, %s)'.format(TABLE_FETCH_PRODUCTS)
        cursor.executemany(sql, records)
        connection.commit()

    with connection.cursor() as cursor:
        # Update the shop URL
        # sql = "UPDATE {} set product_status = 3 where shop_url = '{}' ".format(TABLE_FETCH, shop_url)
        sql = "UPDATE {} set product_status = 3 where shop_url = %s ".format(TABLE_FETCH, shop_url)
        print(sql)
        print('----------------------------------------------------------------')
        cursor.execute(sql, (shop_url,))
        connection.commit()
0 Answers
Related