简体   繁体   English

Python:从mysql中选择合适的类型

[英]Python: Selecting the appropriate type from mysql

How to manage the following: 如何管理以下内容:

listing = db.query("SELECT list_id FROM results limit 10")

returns a list containing values converted to long type : ({'list_id': 175L},...) 返回包含转换为long类型的值的列表:({'list_id':175L},...)

element = db.query("SELECT * FROM list LIMIT 1")

returns returns a single element which has a standard int id : element['id'] = 175 return返回一个具有标准int id的元素:element ['id'] = 175

Now i would like to do something like this: 现在我想做这样的事情:

if element['id'] not in listing:
   print "ok"

But the loop is never entered. 但是从未输入循环。 I think it is because of the different types in the list (long) and element (int). 我认为这是因为列表中的不同类型(长)和元素(int)。

Is there a quick and simple solution to solve this problem? 有没有快速简单的解决方案来解决这个问题?

Thanks 谢谢

Your first query returns list of dictionaries. 您的第一个查询返回字典列表。 You are trying to find a value in dictionary. 您正试图在字典中查找值。 Here is correct way how to iterate through keys in a dictionary: 以下是如何遍历字典中的键的正确方法:

for name, age in list.iteritems():
    if age == search_age:
        print name

Taken from here: Get key by value in dictionary 取自这里: 在字典中按值获取密钥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM