I'm trying to develop a web api using flask
and sqlite
. For communication with db I'm using sqlalchemy
.
In the code that I post below I have create a GET method to retrieve all data into a specific table into db:
from flask import Flask, g, Response, request, jsonify, abort
from flask_restful import Resource, Api
from sqlalchemy import create_engine
from flask.ext.restless import APIManager
from flask.ext.sqlalchemy import SQLAlchemy
from json import dumps
import sqlite3
import json
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///climb.db"
db = SQLAlchemy(app)
class falesie(db.Model):
__tablename__ = 'falesie'
id = db.Column(db.Integer, primary_key=True)
regione = db.Column(db.String(20))
citta = db.Column(db.String(20))
n_settori = db.Column(db.Integer)
lat = db.Column(db.Float)
lon = db.Column(db.Float)
def __init__(self, regione, citta, n_settori, lat, lon):
self.regione = regione
self.citta = citta
self.n_settori= n_settori
self.lat = lat
self.lon = lon
@app.route('/dev', methods = ['GET'])
def get_falesie():
Falesie = falesie.query.all()
formatted_falesie = []
for f in Falesie:
formatted_falesie.append({
'id': f.id,
'regione': f.regione,
'citta': f.citta,
'n_settori': f.n_settori,
'lat': f.lat,
'lon': f.lon})
return json.dumps({'Falesie': formatted_falesie}), 200, {'Content- Type': 'application/json'}
if __name__ == "__main__":
db.create_all()
app.run(debug=True)
I would like to create a GET
method to retrieve a specific record with a specific value, as in this example:
@app.route('dev/<string:name>')
def get_data(name):
I don't know how to retrieve a single record. Any help please?