2016-08-25 58 views
2

我想從ajax查詢我的數據庫中的記錄,例如浮點變量(地圖座標)(查詢float列)。我不知道,如果類似的方法可以查詢不是字符串數據類型,因爲這個沒有工作......SQLalchemy - 用於查詢float列

data = json.loads(request.data) 

     markers = session.query(Markers) \ 
     .filter(Markers.lat.like('%' + data['lat'] + '%')) \ 
     .filter(Markers.lng.like('%' + data['lng'] + '%')) \ 
     .order_by(func.random()).limit(7).all() 

     if len(markers) <= 0: 
      return jsonify(resp = 'no results') 
     else: 
      return jsonify(resp = 'markers', markers = [m.serialize for m in markers]) 

我甚至試圖使其與STR()的字符串,但仍獲得此錯誤:

ProgrammingError: (ProgrammingError) operator does not exist: double precision ~~ unknown 
LINE 3: WHERE markers.lat LIKE '%%' AND markers.lng LIKE '%%' ORDER ... 
         ^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
'SELECT markers.id AS markers_id, markers.type AS markers_type, markers.name AS markers_name, markers.location AS markers_location, markers.lat AS markers_lat, markers.lng AS markers_lng, markers.icon AS markers_icon, markers.message AS markers_message, markers.date AS markers_date, markers.place_id AS markers_place_id, markers.owner AS markers_owner \nFROM markers \nWHERE markers.lat LIKE %(lat_1)s AND markers.lng LIKE %(lng_1)s ORDER BY random() \n LIMIT %(param_1)s' {'lng_1': '%%', 'param_1': 7, 'lat_1': '%%'} 

怎麼回事? sqlalchemy func模塊是否有將查詢float列作爲字符串的方法?我該如何解決?謝謝!

P.S我使用PostgreSQL的

回答

2

你需要投浮到字符串

from sqlalchemy import cast, String 

markers = session.query(Markers) \ 
     .filter(cast(Markers.lat, String()).like('%' + data['lat'] + '%')) 
+0

我會給它一個try.Thanks! – ryanwaite28

+0

謝謝,它工作完美! – ryanwaite28