ai_lambda-exec.py 725 B

1234567891011121314151617181920212223242526272829303132
  1. import pymysql
  2. import boto3
  3. def updata_db(m_key,m_value):
  4. db = pymysql.connect(
  5. host='127.0.0.1',
  6. port=3306,
  7. user='root',
  8. passwd='123***123***',
  9. db='cmd',
  10. charset='utf8mb4')
  11. cursor = db.cursor()
  12. sql = f"INSERT INTO kv(mykey,myvalue) VALUES('{m_key}','{m_value}')"
  13. try:
  14. cursor.execute(sql)
  15. db.commit()
  16. except Exception as e:
  17. print('ERROR : '+e)
  18. db.rollback()
  19. db.close()
  20. def lambda_handler(event, context):
  21. # TODO implement
  22. sec = event['Records'][0]['messageAttributes']
  23. m_key = sec['mykey']['stringValue']
  24. m_value = sec['myvalue']['stringValue']
  25. print(m_key,m_value)
  26. updata_db(m_key,m_value)