Improve your programming skills

MySQL Table lock example usin python connector

Using mysql-connector-python

from db_works import db_connect
import time
import datetime

xxx = str(datetime.datetime.now())
print(xxx)
zzz='a'+str(datetime.datetime.now())+'asd'


# view also need to be locked
def lock_other_workers():
cursor.execute("LOCK TABLES tactics_config WRITE, tactics_config2 WRITE, vw_tactic_config WRITE;")

def insert_into_statement():
cursor.execute("insert into tactics_config (tactics_config_name) values ('"+xxx+"');")

def select_statement():
cursor.execute("select tactics_config_name from vw_tactic_config;")
yyy = cursor.fetchall()
print(yyy)

def update_statement():
cursor.execute("update tactics_config set tactics_config_name = '"+zzz+"';")

def unlock_other_workers():
cursor.execute("UNLOCK TABLES;")


if __name__ == "__main__":
# connect to db
cursor, cnxn = db_connect()

lock_other_workers()
insert_into_statement()
# cnxn.commit()
time.sleep(10)

select_statement()
#cnxn.commit()
update_statement()
#cnxn.commit()

unlock_other_workers()

Leave a comment

Your email address will not be published. Required fields are marked *