Advertisement
naimul64

Thana Summary Script

Feb 16th, 2017
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.80 KB | None | 0 0
  1. import MySQLdb
  2.  
  3.  
  4. def dropThanaSummaryTable(db):
  5.     cursor = db.cursor()
  6.     deleteQuery = 'DELETE FROM `thana_summary` WHERE 1'
  7.  
  8.     try:
  9.         cursor.execute(deleteQuery)
  10.         db.commit()
  11.         print 'Successfully cleared the table'
  12.  
  13.     except Exception as inst:
  14.         print type(inst)  # the exception instance
  15.         print inst.args  # arguments stored in .args
  16.         print inst  # __str__ allows args to be printed directly
  17.         x, y = inst.args
  18.         print 'x =', x
  19.         print 'y =', y
  20.  
  21.  
  22. def insertByBatchQuery(db, upperLimit, lowerLimit):
  23.     batchInsertQuery = """ insert into thana_summary (District_name,
  24.                                         District_id,
  25.                                         Thana_name,
  26.                                         TCode,
  27.                                         Total_school,
  28.                                         Total_Amount,
  29.                                         Total_Amount_Mobile,
  30.                                         Total_parents,
  31.                                         Beneficiary,
  32.                                         Disbursement_through_mobile_banking,
  33.                                         NoOfStd,
  34.                                         Beneficiary_NoOnStd,
  35.                                         Mobile_Beneficiary_Std)
  36.                            SELECT
  37.                                dis.`name` AS 'District_name',
  38.                                dis.id as 'District_id',
  39.                                t.`name` AS 'Thana name',
  40.                                t.`thana_code` AS TCode,
  41.                                COUNT(DISTINCT (d.`school_code`)) AS 'Total_school',
  42.                                SUM(CASE
  43.                                    WHEN d.`amount` <= 1350 AND d.`amount` > 0 THEN d.`amount`
  44.                                END) AS 'Total_Amount',
  45.                                SUM(CASE
  46.                                    WHEN d.`is_valid` = 1 THEN d.`amount`
  47.                                END) AS 'Total_Amount_Mobile',
  48.                                COUNT(d.`id`) AS 'Total_parents',
  49.                                COUNT(CASE
  50.                                    WHEN d.`amount` <= 1350 AND d.`amount` > 0 THEN d.`id`
  51.                                END) AS 'Beneficiary',
  52.                                COUNT(CASE
  53.                                    WHEN d.`is_valid` = 1 THEN 1
  54.                                END) AS 'Disbursement_through_mobile_banking',
  55.                                SUM(d.`no_of_student`) AS NoOfStd,
  56.                                SUM(CASE
  57.                                    WHEN d.`amount` <= 1350 AND d.`amount` > 0 THEN d.`no_of_student`
  58.                                END) AS Beneficiary_NoOnStd,
  59.                                SUM(CASE
  60.                                    WHEN d.`is_valid` = 1 THEN d.`no_of_student`
  61.                                END) AS 'Mobile_Beneficiary_Std'
  62.                            FROM
  63.                                `disbursement_sheet_raw_data_experiment3` d
  64.                                    INNER JOIN
  65.                                `school` s ON d.`school_code` = s.`school_code`
  66.                                    INNER JOIN
  67.                                `thana` t ON t.id = s.`thana_id`
  68.                                    INNER JOIN
  69.                                `district` dis ON t.`district_id` = dis.`id`
  70.                                where t.id >= """ + str(lowerLimit) + """ and t.id <=""" + str(upperLimit) + """
  71.                            GROUP BY t.`name`"""
  72.  
  73.     try:
  74.         cursor.execute(batchInsertQuery)
  75.         db.commit()
  76.         print 'Successfully inserted data into the table thanaId range [' + str(lowerLimit) + ' , ' + str(
  77.             upperLimit) + ']'
  78.  
  79.     except Exception as inst:
  80.         print type(inst)  # the exception instance
  81.         print inst.args  # arguments stored in .args
  82.         print inst  # __str__ allows args to be printed directly
  83.         x, y = inst.args
  84.         print 'x =', x
  85.         print 'y =', y
  86.  
  87.  
  88. def insertDataInSummaryTable(db):
  89.     getMaxIdQuery = 'Select id FROM thana ORDER BY id DESC limit 1 offset 0'
  90.     cursor = db.cursor()
  91.     cursor.execute(getMaxIdQuery);
  92.     maxId = cursor.fetchall()
  93.     maxId = maxId[0][0]
  94.     print "Max thana Id: " + str(maxId)
  95.     upperLimit = 99
  96.     lowerLimit = 0
  97.     while 1:
  98.         insertByBatchQuery(db, upperLimit, lowerLimit)
  99.         upperLimit = upperLimit + 100
  100.         lowerLimit = lowerLimit + 100
  101.         if lowerLimit > maxId:
  102.             break
  103.  
  104.  
  105. hostIp = "192.168.1.56"
  106. userName = "naimul"
  107. password = "naimul#56"
  108. dbName = "pesp_test_db_naimul"
  109. db = MySQLdb.connect(hostIp, userName, password , dbName)
  110. cursor = db.cursor()
  111.  
  112. dropThanaSummaryTable(db)
  113. insertDataInSummaryTable(db)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement