删除mysql查询python结果的行,但不从数据库中删除

时间:2019-10-04 02:13:40

标签: python mysql geolocation

我正在开发一个应用程序,该应用程序需要使用两个邮政编码之间的距离来确定该行是否显示给用户。如果距离不大于100英里,则结果将保留在sql查询返回的结果集中,但是如果距离大于100,我想将其从结果集中删除,但将其保留在sql表中

def zipcode(offerzip, techzip):
    dist = pgeocode.GeoDistance('us')
    distance = dist.query_postal_code(offerzip, techzip)
    distance = distance/1.609
    return round(distance)

def techOffer()
    cur = mysql.connection.cursor()
    result = cur.execute("SELECT * FROM orders WHERE status='offer'",)
    offerData = cur.fetchall()
    result_2 = cur.execute("SELECT * FROM users WHERE username=%s", [session['username']])
    techData = cur.fetchall()
    techZip = techData['zip']
    for row in offerData:
         offerZip = offerData['zip']
         if zipcode(offerZip, techZip) !< 100:

输出print(techData):

({'id': 20, 'name': 'Sage Hopkins', 'email': 'Removed', 'username': 'sagefhopkins', 'password': '$5$rounds=535000$KywbJzy7Oo6lNfrJ$iz9BPUcLF.2HUttL8zRZVEIcwJ8/O3RxOkWZH0lsdO1', 'street': 'Removed ', 'city': 'Removed', 'state': 'Removed', 'zip': 97537, 'phone': 'Removed', 'cell': 'Removed', 'permission': None, 'register_date': datetime.datetime(2019, 8, 27, 20, 29, 26)},)

打印输出(offerData):

({'id': 7, 'userid': 20, 'assignedid': 20, 'name': 'Sage Hopkins', 'email': 'Removed', 'phone': 'Removed', 'street': 'Removed', 'city': 'REmoved', 'state': 'Removed', 'country': 'United States', 'zip': '80906', 'issue': 'Very major computer issue, Very major computer issue, Very major computer issue, Very major computer issue, Very major computer issue, ', 'support_Type': 'Home', 'computer': 'Dell T700', 'operating_System': 'Windows', 'status': 'offer', 'timestamp': datetime.datetime(2019, 8, 27, 20, 30, 43)},)

1 个答案:

答案 0 :(得分:1)

您可以使用del关键字从词典中删除键。在您的情况下,您需要遍历结果,因为它是字典的元组

if zipcode(offerZip, techZip) <= 100:
    for obj in techData:
        del obj['zip']

    for obj in offerData:
        del obj['zip']

请注意,这对数据库中的数据没有任何影响,只会更改techDataofferData的实例。

我也不认为!<是有效的语法。将<=用于小于或等于,将{{11}}用于小于