python数据分析师日常postgreSQL

PYTHON定时重跑获取数据

2017-03-14  本文已影响280人  异想派

参考链接:
PYTHON定时重跑获取数据


代码实现

import psycopg2
from time import sleep
class GetData(object):
    def __init__(self):
        self.conn=''
        self.host='127.0.0.1'
        self.port=5432
        self.user='enniu'
        self.passwd='postgres'
        self.db='test'
        self.cnum=5   #set retry number

    def init_connect(self):
        self.conn= psycopg2.connect(host=self.host, user=self.user, password=self.passwd, database=self.db, port=self.port)

    def get_data(self):
        self.init_connect()
        cur=self.conn.cursor()
        cur.execute("select * from testauto")
        rs=cur.fetchall()
        cur.close()
        self.conn.close()
        return rs

    def run(self):
        count=1 
        while (count<=self.cnum):
            rs=self.get_data()
            if len(rs)>0:   
                print len(rs)
                for i in rs:
                    print i
                break   #when data load success,end the loop

            print count
            sleep(10)
            count+=1

if __name__ == '__main__':
    gd = GetData()
    gd.run()
上一篇下一篇

猜你喜欢

热点阅读