Forums

connection to mysql from local and flask

Hi I was trying to connect to the pythonanywhere database from both local and flask in a project. I am migrating from sqlite to mysql so I would like to do some test locally before implementing it to the flask. I have several question regarding to this issue.

1.I try to use MySQLdb since another post suggesting the this will be very similar to sqlite so I don't need to make much changes. Thus the first question is that will the following code be able to connect to database in Flask?

import MySQLdb
username = 'username'
password = 'pass'
hostname = 'username.mysql.pythonanywhere-services.com'
db = 'username$zipcodes'
conn = MySQLdb.connect(host=hostname, user=username, passwd=password, db=db)
cursor = conn.cursor()

cursor.execute('SELECT * from table)
row = cursor.fetchone()

conn.close()
  1. The second question is that, according to another post, it seems the above code ''could' be used in flask(not sure), but not on the local. If I need to do that, then I will need a paid account and use ssh. I wonder if ssh is required even for flask? And is there another way to connect without ssh? I had another paid account but just wonder this because it looks easier.
  2. I am currently having a sqlite file for my web app and try to convert the data into Mysql in pythonanywhere. I looked for some posts on this but it seems there is not a clear guide. Is there a better way to do this? Thank you!

Take a look at https://blog.pythonanywhere.com/121/