Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Snippet 1
- # Get the datasets in the given database
- def get_tables(self):
- engine = ""
- try:
- # Connecting to MySQL database
- """
- con = pymysql.connect(host=HOST,
- user=USER,
- password=PASSWD,
- db=DB,
- cursorclass=pymysql.cursors.DictCursor)
- """
- engine = create_engine("mysql+pymysql://"+USER+":"+PASSWD+"@"+HOST+":3306/"+DB+"?")
- except Exception as e:
- print("[ERR] Unable to connect to database")
- print(e)
- messagebox.showerror("Error", "Unable to complete connection to database")
- return
- try:
- # Returning the table names in the database
- return engine.table_names()
- except:
- return []
- # Snippet 2
- """
- # Method that uploads the pandas dataframe as a table in mysql server
- """
- def upload_to_sql(self):
- engine = ""
- try:
- # Connecting to MySQL database
- """
- con = pymysql.connect(host=HOST,
- user=USER,
- password=PASSWD,
- db=DB,
- cursorclass=pymysql.cursors.DictCursor)
- """
- engine = create_engine("mysql+pymysql://"+USER+":"+PASSWD+"@"+HOST+":3306/"+DB+"?")
- except Exception as e:
- print("[ERR] Unable to connect to database")
- print(e)
- messagebox.showerror("Error", "Unable to complete connection to database")
- return
- # Get te last table number added
- _f = open("last.txt", "r")
- f = (int)(_f.readline()) + 1
- _f.close()
- try:
- # Add dataframe to mysql as a table
- self.df.to_sql(name="databaseobject"+str(f), con=engine)
- except Exception as e:
- print(e)
- messagebox.showerror("Error", e)
- return
- # Updating table number
- _f = open("last.txt", "w")
- _f.write(str(f))
- _f.close()
- messagebox.showinfo("Complete","Succesfully added dataset!")
Add Comment
Please, Sign In to add comment