I'd like to fetch this custom query on Flask-Peewee
SELECT A.* , haversine('34.0160',' -118.4925', A.lat, A.long, 'MILES') AS dist FROM merchant_details A HAVING haversine('34.0160', '-118.4925', A.lat, A.long, 'MILES') <6000
I tried the following piece of code code but didn't work and I'm getting 'long' object has no attribute 'fetchall':
@app.route('/api/results/')
def results():
db = connect_db()
cur = db.execute("SELECT A.* , haversine('34.0160',' -118.4925', A.lat, A.long, 'MILES') AS dist FROM merchant_details A HAVING haversine('34.0160', '-118.4925', A.lat, A.long, 'MILES') <6000 LIMIT 1")
entries = [dict(id=row[0], merchant_id=row[1], merchant_name=row[2], first_name=row[3]) for row in cur.fetchall()]
return repr(entries)
Any help would be greatfully appreciated.
EDITED>
Here is connect_db function:
from torndb import Connection
LOCALHOST = "localhost"
DBNAME = "XXXX"
DBUSER = "XXXX"
DBPASSWORD = "XXXX"
#connect with DB
def connect_db():
db = Connection(LOCALHOST,DBNAME, user=DBUSER, password=DBPASSWORD)
return db
#close the connection from DB
def close_db(db):
db.close()
With flask-peewee if you want to run a SQL query you can do:
If
MerchantDetails
is a model, you could try:To get: