Advertisement
maurobaraldi

Python MySQL Example

Aug 13th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import csv
  3. import MySQLdb
  4.  
  5. db_settings = {
  6.     'hostname': 'migration-tml-learning-aws1-prod.cykwvsam5lbv.sa-east-1.rds.amazonaws.com',
  7.     'username': 'migration',
  8.     'password': '5xmo4gtc',
  9.     'database': 'tlc',
  10. }
  11.  
  12. db = MySQLdb.connect(**db_settings)
  13. cursor = db.cursor()
  14. count_messages_template = """SELECT (MT.totalcount + MO.totalcount ) AS total_%(phonenumber)s
  15. FROM
  16.    (SELECT COUNT(TML_MT.created_date) AS totalcount
  17.     FROM mobile_learning.mobile_terminated AS `TML_MT`
  18.     WHERE TML_MT.phone_number = '%(phonenumber)s'
  19.        AND TML_MT.created_date >= (now()- INTERVAL 90 DAY)) AS `MT`,
  20.    (SELECT COUNT(TML_MO.created_date) AS totalcount
  21.     FROM mobile_learning.mobile_originated AS `TML_MO`
  22.     WHERE TML_MO.phone_number = '%(phonenumber)s'
  23.        AND TML_MO.created_date >= (now()- INTERVAL 90 DAY)) AS `MO` UNION ALL SELECT count(TLC_MSG.created_date) AS total
  24.    FROM tlc.message AS `TLC_MSG`
  25.    WHERE TLC_MSG.msisdn = CONCAT('52', '%(phonenumber)s')
  26.        AND TLC_MSG.created_date >= (now()- INTERVAL 90 DAY);"""
  27.  
  28. query = lambda q: (lambda q: cursor.query(q))()
  29.  
  30. def compare(phone):
  31.     cursor.query(count_messages_template % {'phonenumber': phone})
  32.     tml, tlc = cursor.fetchall()
  33.     print 'MSISDN: 52%s, TLC: %i; TML: %s' % (phone, tml, tlc)
  34.  
  35. if __name__ == '__main__':
  36.     with csv.open('numbers.csv', 'r') as phone:
  37.         for phonenumber in phone:
  38.             compare(phone)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement