Reputation: 1363
I am a newbie to flask and I started with a small app. Now I am trying to connect to mysql db. I have installed mysql workbench,made database added table and data to it. I can query the db using mysql cmd client.
I tried to connect it using MySQLdb, but i am getting error 2005 in cmd and internal server error in browser.
This is what i get when i goto 127.0.0.1:5000/dashboard
(env) C:\Users\415663\Documents\phatomation\project>set FLASK_CONIG=developer
(env) C:\Users\415663\Documents\phatomation\project>set FLASK_APP=run.py
(env) C:\Users\415663\Documents\phatomation\project>flask run
* Serving Flask app "run"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2018-01-17 13:51:51,367] ERROR in app: Exception on /dashboard [GET]
Traceback (most recent call last):
File "c:\users\415663\documents\phatomation\env\lib\site-packages\flask\app.
py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "c:\users\415663\documents\phatomation\env\lib\site-packages\flask\app.
py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:\users\415663\documents\phatomation\env\lib\site-packages\flask\app.
py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "c:\users\415663\documents\phatomation\env\lib\site-packages\flask\_com
pat.py", line 33, in reraise
raise value
File "c:\users\415663\documents\phatomation\env\lib\site-packages\flask\app.
py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "c:\users\415663\documents\phatomation\env\lib\site-packages\flask\app.
py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\415663\Documents\phatomation\project\app\views.py", line 18,
in dashboard
c, conn = connection()
File "C:\Users\415663\Documents\phatomation\project\database.py", line 8, in
connection
db = "pha_std")
File "c:\users\415663\documents\phatomation\env\lib\site-packages\MySQLdb\__
init__.py", line 86, in Connect
return Connection(*args, **kwargs)
File "c:\users\415663\documents\phatomation\env\lib\site-packages\MySQLdb\co
nnections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2005, "Unknown MySQL server host '127.0.0.1
:3306' (0)")
127.0.0.1 - - [17/Jan/2018 13:51:51] "GET /dashboard HTTP/1.1" 500 -
my __init__.py
from flask import render_template
from app import app
@app.route('/')
def index():
return render_template("index.html")
@app.route('/about')
def about():
return render_template("about.html")
from database import connection
@app.route('/dashboard')
def dashboard():
c, conn = connection()
query = "SELECT * from level_1"
c.execute(query)
data = c.fetchall()
conn.close()
return render_template("index.html", data=data)
my database.py
import MySQLdb
def connection():
# Edited out actual values
conn = MySQLdb.connect(host="127.0.0.1:3306",
user="root",
passwd="1234",
db = "pha_std")
c = conn.cursor()
return c, conn
and my index.html
<!-- index.html-->
{% extends "base.html" %}
{% block title %}Home{% endblock %}
{% block body %}
<div class="jumbotron">
<h1>Flask Is Awesome</h1>
<p class="lead">And I'm glad to be learning so much about it!</p>
</div>
<table border="1" cellpadding="5" cellspacing="5">
{% for row in data %}
<tr>
{% for id in row %}
<td>{{ id }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endblock %}
I tried my level best by making small changes in db connection file,views file etc..But i am only getting 500 error
.
Upvotes: 1
Views: 2003
Reputation: 69082
MySQLdb.connect(host="127.0.0.1:3306", ...)
is wrong, it should be be
MySQLdb.connect(host="127.0.0.1", port=3306, ...)
Hostename and port need to be supplied separately.
Also, these are the default values, you can also omit them. Only if you use a different port/host you have to supply them.
Upvotes: 4