pymysql in 的写法

2023-03-04 17:43:19 浏览数 (1)

代码语言:javascript复制
    def use_name_get_goods_id(self, goods_name):
        """
        用商品名称(列表)查找商品ID
        :return:
        """
        # Read a single record, if change database you need connection.commit()
        sql = "SELECT `id`,`name`,`ip_name`,`sales_price` FROM `tide_boxes_goods` WHERE `name` in (%s)" % ','.join(
            ['%s'] * len(goods_name))
        self.cursor.execute(sql, goods_name)
        result = self.cursor.fetchall()
        print(result)
        # print(result.get("id"))
        return result
代码语言:javascript复制
['猫咪开餐', '早安']
[{'id': 90, 'name': '早安', 'ip_name': 'Molly', 'sales_price': Decimal('57.00')}, {'id': 94, 'name': '猫咪开餐', 'ip_name': 'Molly', 'sales_price': Decimal('39.00')}]

思路:按位置数量生成 %s,直接传列表进来

0 人点赞