• pymysql 单独获取表的栏位名称


    有时需要获取表的栏位+数值,请参考

    https://www.cnblogs.com/xibuhaohao/p/9996571.html

    有时只需要栏位名称,那么可以使用

    col=self.cursor.description

    脚本如下:

    [dba@wanliu-jx-db-218 monitor]$ cat mysql_ccpay.py
    #coding=utf-8
    import sys
    import pymysql
    import os
    #from prometheus_client import Gauge,start_http_server
    import time
    #v_host=os.popen('echo $HOSTNAME')
    #hostname=v_host.read()
    #hstname="".join(hostname)
    #print(hostname.strip())
    class MySQL_Status_Output:
        def __init__(self,host,port,user,password):
            try:
                self.db = pymysql.connect(host=host,port=port,user=user,password=password)
                self.cursor = self.db.cursor()
            except Exception as e:
                print('Wrong')
                print(e)
        def mysql_select_sql(self,sql):
            try:
                self.cursor.execute(sql)
                col=self.cursor.description
                v_result=self.cursor.fetchall()
                return v_result,col
            except Exception as e:
                print(e)
        def close(self):
            self.db.close()
    if __name__ == "__main__":
        #start_http_server(9500)
        #ccpayGauge = Gauge('ccpayGauge','Description of gauge', ['mylabelname'])
        while True:
            time.sleep(10)
            try:
                pro_db = MySQL_Status_Output('127.0.0.1',3306,'dbadmin','dbadmin')
                ccpay_machine_enable,col_name = pro_db.mysql_select_sql(" select id '序号',name '姓名',age '年龄' from test.test ")
                pro_db.close()
                col_name_list=[]
                ccpay_machine_enable_name=[]
                for i in range(len(col_name)):
                    col_name_list.append(col_name[i][0])
                print(tuple(col_name_list))
                col_name_tuple=tuple(col_name_list)
                for j in range(len(ccpay_machine_enable)):
                    ccpay_machine_enable_name=ccpay_machine_enable[j]
                    print(ccpay_machine_enable_name)
                    me = dict(zip(col_name_tuple,ccpay_machine_enable_name))
                    print(me)
            except Exception as e:
                print('Is Wrong')
                print(e)
    输出结果如下:
  • 相关阅读:
    [转]web串口调试助手,浏览器控制串口设备
    [转]WEB页获取串口数据
    [转]js串口通信 调用MSCOMM32控件 链接电子秤
    [转]C# serialPort 串口接收中this.Invoke的使用
    [转]C#串口通信 SerialPort类
    [转]How to display the data read in DataReceived event handler of serialport
    [转]c# System.IO.Ports SerialPort Class
    [转]用C#在windows上操控电脑自带蓝牙(入道指南)
    Springboot Actuator之四:重写与注册服务中心的健康检查逻辑(判断依据是tcp连接是否正常)
    二维码支付原理分析及安全性的探究
  • 原文地址:https://www.cnblogs.com/xibuhaohao/p/10003547.html
Copyright © 2020-2023  润新知