[Flask] Read data from existing database
Andreas Dorfner
andreas.dorfner at stud.hs-regensburg.de
Sun Dec 4 15:17:30 EST 2016
Hello everyone,
I've created a data-logger using Flask-SQLAlchemy with the following
schematic/models:
class Phase1 with id, Urms, Irms, Prms, Qrms, cosphi
class Phase2 with id, Urms, Irms, Prms, Qrms, cosphi
class Phase3 with id, Urms, Irms, Prms, Qrms, cosphi
class PhaseTotal with id, Prms, Qrms
Attached you can find the resultant database created by the file
logger_sqlalchemy.py.
In the second part of the project I would like to set up a webserver to
display the newest database-data on a website.
Because there is an infinite loop in the logger file, another python
file (webserver.py, also attached) is used to do so.
Like described on the website
"http://flask-sqlalchemy.pocoo.org/dev/queries/#querying-records", the
following lines should display the Urms value with id 5 on the screen
(logger_sqlalchemy.py doesn't run at this time):
URMS_L1 = Phase1.query.filter_by(id=5).first()
print(URMS_L1.Urms)
But there is always an error "Phase1 is not defined"!
Does anybody know what's going wrong?
To link the file to the corresponding database, the following lines are
used:
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] =
'sqlite:////root/project/logger_sqlalchemy.db'
db = SQLAlchemy(app)
Maybe the link to the database is not correct? The database is in the
same folder than the webserver file.
Many thanks,
Andreas
---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20161204/652be174/attachment.html>
-------------- next part --------------
#all the imports
import time
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////root/project/logger_sqlalchemy.db'
db = SQLAlchemy(app)
#---------------------------------------------------------------------------
#connects to the specific database
#def connect_db():
# return sqlite3.connect(app.config['DATABASE'])
#---------------------------------------------------------------------------
URMS_L1 = Phase1.query.filter_by(id=5).first()
print(URMS_L1.Urms)
#---------------------------------------------------------------------------
#That function pass the entries as dicts to the show_entries.html template
#and return the broadcasted one.
#@app.route('/')
#@app.route('/user/<username>')
#def show_user(username):
# user = User.query.filter_by(username=username).first_or_404()
# return render_template('show_user.html', user=user)
#---------------------------------------------------------------------------
#Run the file as a standalone application on IP address 192.168.7.2 (BBB)
#at Port 5000
#if __name__ == '__main__':
# app.run(host = '192.168.7.2')
#---------------------------------------------------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: logger_sqlalchemy.db
Type: application/octet-stream
Size: 5120 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/flask/attachments/20161204/652be174/attachment.obj>
More information about the Flask
mailing list