python实时查询数据的时候,无法感知数据库变化如何操作。

在做一个otp验证的时候,因为每次都要点击发送验证码,然后去数据库查询这条验证码

先来公共函数 

# coding=utf-8
import MySQLdb



class MySql():
def __init__(self,db):
self.mysql = MySQLdb.Connect(
host="10",
port=3306,
user="jackie",
passwd="jackie@123",
db=db,
charset="utf8")
self.cursor = self.mysql.cursor()

def select(self, sql):
"""
查询
:param sql:
:return:
"""
self.cursor.execute(sql)
result = self.cursor.fetchall()
return result


def update(self, sql):
"""
修改
:param sql:
:return:
"""
self.cursor.execute(sql)
self.mysql.commit()

def delete(self, sql):
"""
删除
:param sql:
:return:
"""

self.cursor.execute(sql)
self.mysql.commit()

def insert(self, sql):
"""
插入
:param sql:
:return:
"""
self.cursor.execute(sql)
self.mysql.commit()
return "success"

def close(self):
self.mysql.close()





实际调用的时候 加上等待时间
time.sleep(15)
MySql.mysql.commit()
OTP=MySql.select(sql="SELECT * from common_otp co order by last_updated DESC")
getotp=OTP[0][6]

这样就可以实时获得到 验证码了


猜你喜欢

转载自www.cnblogs.com/testling/p/12146017.html