Pymysql

Pymysql 查询

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/14 10:52
import pymysql  
def monitor_db():
    db = pymysql.connect(host="192.1xxx", user="aaa",
                         password="xxxxx.", db="cxxxxxe", port=3306)
    cur = db.cursor()
    l1 = []
    sql = "SELECT * FROM `Nodes` WHERE `Name` LIKE '%redis%'"
    try:
        cur.execute(sql)
        results = cur.fetchall()
        # print("id", "name")
        # 遍历结果
        for row in results:
            # id = row[0]
            name = row[2]

            l1.append(name)
            print(l1)
        if l1 != []:
            return "ok"
        else:
            return "error"
    except Exception as e:
        raise e

    finally:
cur.close() db.close()
print(monitor_db())

 

posted @ 2021-10-14 15:05  NeilyoX  阅读(34)  评论(0编辑  收藏  举报