Guest User

Untitled

a guest
Nov 10th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. # Snippet 1
  2. # Get the datasets in the given database
  3. def get_tables(self):
  4. engine = ""
  5. try:
  6. # Connecting to MySQL database
  7. """
  8. con = pymysql.connect(host=HOST,
  9. user=USER,
  10. password=PASSWD,
  11. db=DB,
  12. cursorclass=pymysql.cursors.DictCursor)
  13. """
  14. engine = create_engine("mysql+pymysql://"+USER+":"+PASSWD+"@"+HOST+":3306/"+DB+"?")
  15. except Exception as e:
  16. print("[ERR] Unable to connect to database")
  17. print(e)
  18. messagebox.showerror("Error", "Unable to complete connection to database")
  19. return
  20.  
  21. try:
  22. # Returning the table names in the database
  23. return engine.table_names()
  24. except:
  25. return []
  26.  
  27. # Snippet 2
  28. """
  29. # Method that uploads the pandas dataframe as a table in mysql server
  30. """
  31. def upload_to_sql(self):
  32. engine = ""
  33. try:
  34. # Connecting to MySQL database
  35. """
  36. con = pymysql.connect(host=HOST,
  37. user=USER,
  38. password=PASSWD,
  39. db=DB,
  40. cursorclass=pymysql.cursors.DictCursor)
  41. """
  42. engine = create_engine("mysql+pymysql://"+USER+":"+PASSWD+"@"+HOST+":3306/"+DB+"?")
  43. except Exception as e:
  44. print("[ERR] Unable to connect to database")
  45. print(e)
  46. messagebox.showerror("Error", "Unable to complete connection to database")
  47. return
  48.  
  49. # Get te last table number added
  50. _f = open("last.txt", "r")
  51. f = (int)(_f.readline()) + 1
  52. _f.close()
  53.  
  54. try:
  55. # Add dataframe to mysql as a table
  56. self.df.to_sql(name="databaseobject"+str(f), con=engine)
  57. except Exception as e:
  58. print(e)
  59. messagebox.showerror("Error", e)
  60. return
  61.  
  62. # Updating table number
  63. _f = open("last.txt", "w")
  64. _f.write(str(f))
  65. _f.close()
  66.  
  67. messagebox.showinfo("Complete","Succesfully added dataset!")
Add Comment
Please, Sign In to add comment