Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. import mysql.connector
  2. from mysql.connector import Error
  3. import csv
  4.  
  5. """ These functions were for INSERTing into a table using a CSV column.
  6.  
  7. However, some of the fields needed for the INSERT had to be queried from another table, so this queries
  8. the other table first and adds the needed info to the CSV, and then INSERTs into the
  9. other table.
  10.  
  11. This has not been totally generalized, so for reuse, you have to fill in your own database config,
  12. the column numbers for your columns in the csv, and your search and insert queries."""
  13.  
  14.  
  15. """ Add column to CSV with the IDs for database table fields that need to be updated. """
  16. def update_csv(filename):
  17. # open csv
  18. with open (filename, 'rb') as readfile:
  19. reader = csv.reader(readfile)
  20.  
  21. new_filename = 'updated_' + filename
  22. with open(new_filename, 'wb') as writefile:
  23.  
  24. writer = csv.writer(writefile)
  25.  
  26. # copy headers from old file
  27. headers = next(reader, None)
  28. writer.writerow(headers)
  29.  
  30. # for each row in the csvfile, query for needed values and update csv
  31. for row in reader:
  32.  
  33. # get cells needed for query from csv
  34. mlid = row[0]
  35.  
  36. # find the data needed for insert using the CSV cells
  37. search_query = """ SELECT resource
  38. FROM resource_data
  39. WHERE resource_type_field = 88 and value = """ + mlid
  40.  
  41. # read database configuration
  42. try:
  43. conn = mysql.connector.connect(host='localhost',
  44. database='DBNAME',
  45. user='USER',
  46. password='PASSWORD')
  47. cursor = conn.cursor(buffered = True)
  48. cursor.execute(search_query)
  49. dbrow = cursor.fetchone()
  50. all_rsids = []
  51.  
  52. while dbrow is not None:
  53. all_rsids.append(dbrow[0])
  54. dbrow = cursor.fetchone()
  55.  
  56. # write the queried data out to a new copy of the csv
  57. row.append(all_rsids)
  58. writer.writerow(row)
  59.  
  60. except Error as error:
  61. print(error)
  62.  
  63. finally:
  64. cursor.close()
  65. conn.close()
  66.  
  67. writefile.close()
  68. readfile.close()
  69.  
  70. """ Update table in MySQL database using CSV """
  71. def update_table(filename):
  72. # open csv
  73. with open (filename, 'rb') as readfile:
  74. reader = csv.reader(readfile)
  75.  
  76. # skip headers from old file
  77. headers = next(reader, None)
  78.  
  79. for row in reader:
  80.  
  81. # get data from csv
  82. rsids = row[107].strip("[]").split(",") # column format is [1, 2, 3]
  83. zipcode = row[5]
  84.  
  85. if zipcode != "":
  86.  
  87. for rsid in rsids:
  88. rsid = rsid.strip()
  89. print(rsid + " - " + zipcode)
  90.  
  91. # insert data for ALL matching rsids
  92. insert_query = """ INSERT INTO resource_data (resource, resource_type_field, value)
  93. VALUES (""" + rsid + ', 3, "' + zipcode + '")'
  94.  
  95. # read database configuration
  96. try:
  97. conn = mysql.connector.connect(host='localhost',
  98. database='DATABASE',
  99. user='USER',
  100. password='PASSWORD')
  101. cursor = conn.cursor(buffered = True)
  102. cursor.execute(insert_query)
  103. conn.commit()
  104.  
  105. except Error as error:
  106. print(error)
  107.  
  108. finally:
  109. cursor.close()
  110. conn.close()
  111.  
  112. readfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement