Forums

connecting PythonAnywhere database to my project (errors)

hello all , i am new to this environment and i started developing my app when i started to get to the part i needed to use a database i did the next

  1. went to the database tab and created a database and i set a mysql password.
  2. From bash console i checked if i can access the database and all was fine.
  3. in my main python_loader.py i did these codes and all of them result the same problem.

(I am using python 2.7 , and my app is flask project related)

this is first try using sqlalchemy

from sqlalchemy import create_engine
app = Flask(__name__)
engine = create_engine('mysql://Sosolo:password@mysql.server/Sosolo$database-name')
engine.connect()

this is second try using sqlalchemy

from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://Sosolo:password@mysql.server/Sosolo$database-name'
db = SQLAlchemy(app)

and last this is using MySQLdb

import MySQLdb as sql
app = Flask(__name__)
db = sql.connect(host='mysql.server', user='Sosolo', passwd='pass', db='db')
cursor = db.cursor ()

all of them lead me to this Error

return Connection(*args, **kwargs)
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2005, "Unknown MySQL server host 'mysql.server' (0) ")

i really don't understand the connections or how it should work correctly please help me thanks in advance

Are you trying this code in a console or in a web app?

Can you also try changing the mysql.server hostname to:

anywheredb.clt1czfvlzlc.us-east-1.rds.amazonaws.com

?

harry i am trying it on the web app running it with the main file python_loader.py and the page don't load it gives Internal Server Error , when i remove the database codes , the page website load.

so i try to connect it with the console on and it did give me this error up.


and i did try the host name it still gives the same problem

What's the latest error in the error log of your web app?

2015-03-23 16:53:47,701 :Exception on /s [GET]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1361, in full_dispatch_request
response = self.make_response(rv)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1439, in make_response
raise ValueError('View function did not return a response')
ValueError: View function did not return a response

That doesn't look like a database problem?

i will check again

the log error actually helped me a lot thanks , the problem was that the my function was not working and i fix it and it showed up , sorry for trouble.

No problem, thanks for confirming it works now!

Is there any way to tell which function in my code is causing the issue? The traceback only shows the error trail in the flask code.

did you read the full traceback?