Guest User

Untitled

a guest
Nov 15th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. # Imports and connection string setup
  2. # ...
  3.  
  4. # You create a connection object and a cursor here, which are prerequisites to
  5. # executing queries.
  6. conn = psycopg2.connect(conn_string)
  7. cur = conn.cursor()
  8.  
  9. # This is your query, however this is static and you would have to manipulate
  10. # this query every time you want something different.
  11. # If you want to dynamically change stuff look at the links
  12. # posted below the code.
  13.  
  14. query = """
  15. SELECT
  16. users.id,
  17. users.company_id,
  18. users.state,
  19. users.phase,
  20. transactions.amount
  21. FROM
  22. cases
  23. INNER JOIN transactions ON user.id = transactions.user_id
  24. WHERE users.company_id IN (9, 15)
  25. AND transactions.date < '2018-01-01'
  26. """
  27.  
  28. # The following line is unnecessary, although it may help understand what
  29. # these columns are named
  30. # To get all column names from the DB you can use
  31. # colnames = [desc[0] for desc in curs.description]
  32. # After you executed your query below
  33. fields = ['id', 'company_id', 'state', 'phase', 'amount']
  34.  
  35. cur.execute(query)
  36.  
  37. # Here you fetch all data, which can be a lot (you may want to check
  38. # memory consumption)
  39. data = cur.fetchall()
  40.  
  41. # Setting up a result list and iterate over the results of the query
  42. internal = []
  43. for d in data:
  44. # The next line is not needed, we do not need to append an empty list
  45. # to the results as we can just append the final result for this row
  46. # further += is overloading the operator, it is cleaner (IMHO) to
  47. # use .append() here
  48. internal += [[''] * 4]
  49. # Now the remainder in the loop is interesting, we add an empty list
  50. # of values to the end of the results (internal) and then use the last
  51. # index to update the values
  52. # Apart from printing the full results in the last step this will probably
  53. # incur some overhead compared to creating the single row's result first
  54. # and then appending/printing it
  55.  
  56. # This simply assigns the value of the current rows index 1 to the last
  57. # item in internal (the empty list added before)
  58. internal[-1][0] = d[1]
  59.  
  60. # Here we obtain the index of 'phase' from fields to know which column
  61. # the value is
  62.  
  63. # If you never change the fields variable above, your can just simply index
  64. # it directly.
  65.  
  66. # After knowing the index (fields.index('phase')) which would be 3 we do a
  67. # comparison if it is 'activated' as a value
  68. # So the result will be True or False for everything that is
  69. # not 'activated'
  70. internal[-1][1] = d[fields.index('phase')] == 'activated'
  71. # Here we simply find the column idx for state (which is 2) and add the
  72. # value.
  73. internal[-1][2] = d[fields.index('state')]
  74. # Here we check if the amount is > 0 and assign 'success' if so, otherwise
  75. # 'fail'.
  76. internal[-1][3] = 'success' if d[4] > 0 else 'fail'
  77.  
  78. # You are printing ALL results here, for simple check you may want to print
  79. # only the first or last result as such:
  80.  
  81. # print(internal[0])
  82. # or
  83. # print(internal[-1])
  84.  
  85. print(internal)
  86.  
  87. conn = psycopg2.connect(conn_string)
  88. cur = conn.cursor()
  89.  
  90. query = """
  91. SELECT
  92. users.id,
  93. users.company_id,
  94. users.state,
  95. users.phase,
  96. transactions.amount
  97. FROM
  98. cases
  99. INNER JOIN transactions ON user.id = transactions.user_id
  100. WHERE users.company_id IN (9, 15)
  101. AND transactions.date < '2018-01-01'
  102. """
  103.  
  104. fields = ['id', 'company_id', 'state', 'phase', 'amount']
  105.  
  106. cur.execute(query)
  107. data = cur.fetchall()
  108.  
  109. internal = []
  110. for d in data:
  111. # We simply create a list with all the same results and append it
  112. row = [
  113. d[1],
  114. d[fields.index('phase')] == 'activated',
  115. d[fields.index('state')],
  116. 'success' if d[4] > 0 else 'fail'
  117. ]
  118. internal.append(row)
  119.  
  120. # print(internal)
  121. print(internal[0])
Add Comment
Please, Sign In to add comment