V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
p8p8
V2EX  ›  Python

关于数据库表,多条查询的问题。

  •  
  •   p8p8 · 2014-12-03 18:52:10 +08:00 · 2826 次点击
    这是一个创建于 3433 天前的主题,其中的信息可能已经有所发展或是发生改变。
    user表里有N条记录,表里有3个字段,uid, username, avatar,这个时候,客户端post给服务端一个用户uid的字典,例如{123,332,190,201},那么我如何根据这些传过来的uid,查询到数据后,返回给客户端这4个uid数据库表字段中的username和avatar啊?
    4 条回复    2014-12-03 23:58:56 +08:00
    saximoer
        1
    saximoer  
       2014-12-03 18:58:20 +08:00
    返回几个User 对象的LIST 或者转成JSON 呗~
    不晓得有没有帮助
    maroon
        2
    maroon  
       2014-12-03 21:21:27 +08:00   ❤️ 1
    select * from user where id in (1,2,3,4);
    p8p8
        3
    p8p8  
    OP
       2014-12-03 22:22:44 +08:00
    @tornado.gen.coroutine
    def get_myfriens(self,friends):
    query = 'SELECT username, avatar_url FROM users WHERE mid IN (759428,480672);'
    cursor = yield self.execute(query)


    这样会报错:

    psycopg2.ProgrammingError: operator does not exist: text = integer
    LINE 1: SELECT username, avatar_url FROM users WHERE mid in (759428,...
    ^
    HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
    p8p8
        4
    p8p8  
    OP
       2014-12-03 23:58:56 +08:00
    是我自己字段的问题,设置好了,谢谢大家!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   868 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 21:09 · PVG 05:09 · LAX 14:09 · JFK 17:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.