Guest User

Untitled

a guest
Apr 18th, 2018
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. import mysql.connector
  2. from mysql.connector import errorcode
  3.  
  4. def addUser(connection, user):
  5. cursor = connection.cursor()
  6.  
  7. customerInsert = (
  8. "INSERT INTO customer (first_name, last_name, email, "
  9. "DOB, annual_spend) VALUES "
  10. "(%(first)s, %(last)s, %(email)s, %(dob)s, %(spend)s)")
  11.  
  12. customerData = {
  13. 'first': user['name']['first'],
  14. 'last': user['name']['second'],
  15. 'email': user['email'],
  16. 'dob': user['dob'],
  17. 'spend': user['annualSpend']
  18. }
  19.  
  20. cursor.execute(customerInsert, customerData)
  21. customerId = cursor.lastrowid
  22.  
  23. cityQuery = ("SELECT city_id FROM city WHERE city = %(city)s")
  24. for address in user['address']:
  25. cursor.execute(cityQuery, {'city': address['city']})
  26. city_id = cursor.fetchone()[0]
  27.  
  28. addressInsert = (
  29. "INSERT INTO address (address, address2, district, "
  30. "city_id, postal_code, customer_id, location) "
  31. "VALUES (%(add)s, %(add2)s, %(dist)s, %(city)s, %(post)s, %(cust)s, %(loc)s)")
  32.  
  33. addressData = {
  34. 'add': address['number'],
  35. 'add2': address['street'],
  36. 'dist': address['state'],
  37. 'city': city_id,
  38. 'post': address['postalCode'],
  39. 'cust': customerId,
  40. 'loc': address['location']
  41. }
  42.  
  43. cursor.execute(addressInsert, addressData)
  44.  
  45. topicQuery = ("SELECT topics_id FROM topics WHERE subject = %(subj)s")
  46. interestInsert = (
  47. "INSERT into interests (topic_id, customer_id) "
  48. "VALUES (%(topic)s, %(cust)s)")
  49.  
  50. # For each interest, see if it already exists in the topic table; if not then
  51. # add it. In either case, record the id from the topics table and use that to add
  52. # an entry to the interests table for the customer.
  53.  
  54. for interest in user['interests']:
  55. topicId = 0
  56. topicData = {
  57. 'subj': interest['interest']
  58. }
  59.  
  60. cursor.execute(topicQuery, topicData)
  61. row = cursor.fetchone()
  62. if row is None:
  63. topicInsert = ("INSERT INTO topics (subject) VALUES (%(subj)s)")
  64. cursor.execute(topicInsert, topicData)
  65. topicId = cursor.lastrowid
  66. else:
  67. topicId = row[0]
  68.  
  69. interestData = {
  70. 'topic': topicId,
  71. 'cust': customerId
  72. }
  73. cursor.execute(interestInsert, interestData)
  74.  
  75. phoneInsert = (
  76. "INSERT INTO `phone numbers` (customer_id, phone_number, `Phone number_type`) "
  77. "VALUES (%(cust)s, %(num)s, %(type)s)")
  78. for phoneNumber in user['phone']:
  79. phoneData = {
  80. 'cust': customerId,
  81. 'num': phoneNumber['number'],
  82. 'type': phoneNumber['location']
  83. }
  84. cursor.execute(phoneInsert, phoneData)
  85.  
  86. connection.commit()
  87. cursor.close()
  88. return customerId
  89.  
  90.  
  91. def addSimpleUser(connection, user):
  92. cursor = connection.cursor()
  93. customerInsert = (
  94. "INSERT INTO customer (first_name, last_name, email, "
  95. "home_address_id, work_address_id, DOB, annual_spend) VALUES "
  96. "(%(first)s, %(last)s, %(email)s, %(dob)s, %(spend)s)")
  97. customerData = {
  98. 'first': user['name']['first'],
  99. 'last': user['name']['second'],
  100. 'email': user['email'],
  101. 'dob': user['dob'],
  102. 'spend': user['annualSpend']
  103. }
  104. cursor.execute(customerInsert, customerData)
  105. customerId = cursor.lastrowid
  106. connection.commit()
  107. cursor.close()
  108. return customerId
  109.  
  110.  
  111. def addStillSimpleUser(connection, user):
  112. homeId = 0;
  113. workId = 0;
  114. cursor = connection.cursor()
  115. cityQuery = ("SELECT city_id FROM city WHERE city = %(city)s")
  116. for address in user['address']:
  117. cursor.execute(cityQuery, {'city': address['city']})
  118. city_id = cursor.fetchone()[0]
  119. addressInsert = (
  120. "INSERT INTO address (address, address2, district, "
  121. "city_id, postal_code) "
  122. "VALUES (%(add)s, %(add2)s, %(dist)s, %(city)s, %(post)s)")
  123. addressData = {
  124. 'add': address['number'],
  125. 'add2': address['street'],
  126. 'dist': address['state'],
  127. 'city': city_id,
  128. 'post': address['postalCode']
  129. }
  130. cursor.execute(addressInsert, addressData)
  131. addressID = cursor.lastrowid
  132. if address['location'] == "home":
  133. homeId = addressID
  134. elif address['location'] == "work":
  135. workId = addressID
  136.  
  137. customerInsert = (
  138. "INSERT INTO customer (first_name, last_name, email, "
  139. "home_address_id, work_address_id, DOB, annual_spend) VALUES "
  140. "(%(first)s, %(last)s, %(email)s, %(home)s, %(work)s, %(dob)s, %(spend)s)")
  141. customerData = {
  142. 'first': user['name']['first'],
  143. 'last': user['name']['second'],
  144. 'email': user['email'],
  145. 'home': homeId,
  146. 'work': workId,
  147. 'dob': user['dob'],
  148. 'spend': user['annualSpend']
  149. }
  150. cursor.execute(customerInsert, customerData)
  151. customerId = cursor.lastrowid
  152.  
  153. connection.commit()
  154. cursor.close()
  155. return customerId
  156.  
  157.  
  158. try:
  159. cnx = mysql.connector.connect(user="root", password="goldie",
  160. host="localhost",
  161. database="store")
  162. except mysql.connector.Error as err:
  163. if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
  164. print("Something is wrong with your user name or password")
  165. elif err.errno == errorcode.ER_BAD_DB_ERROR:
  166. print("Database does not exist")
  167. else:
  168. print(err)
  169. else:
  170. customer = {
  171. "name" : {
  172. "first" : "Eartha",
  173. "second" : "Thompson"
  174. },
  175. "address" : [
  176. {
  177. "location" : "home",
  178. "number" : 23,
  179. "street" : "Twin Pines",
  180. "city" : "New York",
  181. "state" : "New York",
  182. "postalCode" : "O83 1F1"
  183. },
  184. {
  185. "location" : "work",
  186. "number" : 1,
  187. "street" : "Holy Cross",
  188. "city" : "New York",
  189. "state" : "New York",
  190. "postalCode" : "513 8U5"
  191. }
  192. ],
  193. "phone" : [
  194. {
  195. "location" : "mobile",
  196. "number" : "+48-675-560-3029"
  197. },
  198. {
  199. "location" : "work",
  200. "number" : "+48-887-222-1234"
  201. }
  202. ],
  203. "email" : "eandrzejak0@yellowpages.com",
  204. "annualSpend" : 916305.32,
  205. "dob" : "1985-02-28 07:32:58",
  206. "interests" : [
  207. {
  208. "interest" : "XML Schema Design"
  209. },
  210. {
  211. "interest" : "Glazing"
  212. }
  213. ]
  214. }
  215. customer = addUser(cnx, customer)
  216. print "Added customer# ", customer, " to the database"
  217. cnx.close()
Add Comment
Please, Sign In to add comment