首页 > 数据库技术 > 详细

Python3 if 变量variable SQL where 语句拼接

时间:2019-10-14 16:07:46      阅读:144      评论:0      收藏:0      [点我收藏+]

最近在写python3的项目,在实际中运用到了根据 if 判断变量variable ,然后去拼接where子句。但是在百度、BING搜索中未找到合适的答案,这是自己想出来的典型php写法,这里做一下记录。不知道在python中如何做,如有python大神知道,请指点一下。

  

def getRooms( excludeId = None , limit = None ):
    sql = ‘SELECT * from computer_room WHERE 1=1 ‘
    param = []
    if excludeId :
        sql += ‘AND id <> %s ‘
        param += [excludeId]
    sql += ‘ORDER BY weights ASC , id DESC ‘
    if limit :
        sql += ‘LIMIT %s ‘
        param += [limit]
    config.cursor.execute( sql , param )
    return config.cursor.fetchall()

  

Python3 if 变量variable SQL where 语句拼接

原文:https://www.cnblogs.com/68xi/p/11671387.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!