I am using mysql connect in the python script. Python script needs to work for long time It works fine for a few hours but it has issues to connect mysql after some hours Here is my codes.
Code to connect mysql
host = "localhost"
user = "root"
def get_connection():
connection = mysql.connector.connect(
host=host,
user=user,
password=password,
database=database
)
return connection
Script repeats below codes to insert the results
connection = get_connection()
cursor = connection.cursor()
cursor.executemany(insert_query, records_to_insert)
connection.commit()
posts = cursor.rowcount + 1
cursor.execute("""UPDATE topics set posts=%s WHERE tid=%s""", (posts, topic_id, ))
connection.commit()
cursor.close()
connection.close()
Could you give me a solution to resolve my issues? Thanks.