• python re 实例


    #!/usr/bin/env python
    #_*_coding:utf-8_*_

    import re
    import time
    import json
    import MySQLdb
    import contextlib

    hosts = {0:{"host":"192.168.1.140","port":3306,"user":"root","Password":"123456","database":"test"}}

    def who_binlog():
    map_sql = "INSERT INTO `rdstest`.`ty` SET @1=14 @2=5 @3=6 INSERT INTO `rdstest`.`ty` SET @1=12 @2=6 @3=8"
    r1 = re.compile("INSERT INTO")
    print r1.split(map_sql)

    for mp in r1.split(map_sql):
    if mp:
    print(mp.strip())
    vals = re.findall('@*.=(d+)',mp)
    print(vals)
    vals2 = re.findall("@2=(d+)",mp)
    print(vals2)
    db_table = mp.strip().split()[0]
    print(db_table)

    def return_obj():
    ret_dirct = {}
    ret_dirct["io_sql"] = "test"
    ret_dirct["name"] = "oh"
    bb = {}
    bb["case1"]="test"
    bb["case2"] = "test2"
    ret_dirct["data"] = bb;

    print(json.dumps(ret_dirct))

    @contextlib.contextmanager
    def mysql(Host,Port,User,Password,Database):

    conn = MySQLdb.connect(host=Host, port=Port, user=User, passwd=Password, db=Database)
    #cursor = conn.cursor()
    cursor = conn.cursor(MySQLdb.cursors.DictCursor);

    try:
    yield cursor;
    finally:
    conn.commit();
    cursor.close();
    conn.close();


    def execSql(excelSql):
    hosts = {0:{"host":"192.168.1.140","port":3306,"user":"root","Password":"123456","database":"test"}}
    for idx in hosts:
    ip = hosts[idx]["host"]
    port = hosts[idx]["port"]
    user = hosts[idx]["user"]
    password = hosts[idx]["Password"]
    database = hosts[idx]["database"]
    with mysql(ip,port,user,password,database) as cursor:
    sql = excelSql
    cursor.execute(sql)
    rows = cursor.fetchall()
    if len(rows)>0:
    Master_Host = rows[0]["Master_Host"]
    SQL_THREAD = rows[0]["Slave_SQL_Running"]
    IO_THREAD = rows[0]["Slave_IO_Running"]
    LAST_ERROR= rows[0]["Last_Error"]
    Slave_SQL_RunStatus= rows[0]["Slave_SQL_Running_State"]
    Master_InforStatus = rows[0]["Master_Info_File"]
    print("-------DataServer:%s------" % ip)
    print("relp Master Host: %s" % Master_Host)
    print("repl SQL Thread: %s" % SQL_THREAD)
    print("repl IO Thread: %s" % IO_THREAD)
    print("Mrepl Last error: %s" % LAST_ERROR)
    print("Slave SQLRunStatus: %s" % Slave_SQL_RunStatus)
    print("Master_InfoStatus: %s" % Master_InforStatus)
    print(" ")


    def get_table_row(sql):
    for idx in hosts:
    ip = hosts[idx]["host"]
    port = hosts[idx]["port"]
    user = hosts[idx]["user"]
    password = hosts[idx]["Password"]
    database = hosts[idx]["database"]
    with mysql(ip, port, user, password, database) as cursor:
    cursor.execute(sql)
    rows = cursor.fetchall()
    return rows




    if __name__ == "__main__":
    #who_binlog()
    #return_obj()
    #execSql("show slave status;")
    db = "test"
    tb = "t1"
    sql = """select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME COLUMN_KEY,ORDINAL_POSITION
    from information_schema.COLUMNS
    where TABLE_SCHEMA='%s' and TABLE_NAME='%s'
    order by ORDINAL_POSITION asc;
    """ %(db,tb)
    rows = get_table_row(sql)
    print(rows)
  • 相关阅读:
    mysql 内联接、左联接、右联接、完全联接、交叉联接 区别
    JS 时间字符串与时间戳之间的转换
    MySQL性能优化的最佳20条经验
    ++i 与 i++ 的区别
    === 与 == 区别
    SC命令创建和删除windows服务
    杂记
    linux 文件编程
    u-boot 启动过程
    简单冒泡法
  • 原文地址:https://www.cnblogs.com/moss_tan_jun/p/7967750.html
Copyright © 2020-2023  润新知