python实现远程连接数据库定时获取数据

# --*-- coding:utf-8 --*--
import apscheduler
import time
from apscheduler.schedulers.blocking import BlockingScheduler
import pandas as pd
import pymysql
import subprocess
import os

def connect_mysql():
    db_config = {
        'host':'xx.xxx.xxx.xx',#host
        'port':3306,
        'user':'qys',
        'password':'059807',
        'db':'test',#需要连接的数据库
        'charset':'utf8'#编码
    }
    try:
        cms = pymysql.connect(**db_config)
        cur = cms.cursor()#获取一个游标
        #cur.execute("insert into user values('4','test1','test1');")#在user数据表插入一条数据
        #cur.execute('flush privileges;')#必须实时更新数据
        cur.execute('select * from user;')#查询USER数据表中所有数据
        data = cur.fetchall()
        for d in data:#打印查询的数据
                print('id: '+str(d[0])+'\t'+'name: '+str(d[1])+'\t'+'pwd: '+str(d[2]))
        cur.close()
    except Exception as e:
        print(e)

db = connect_mysql()
sched = BlockingScheduler()
sched.add_job(connect_mysql, 'interval', seconds=5)
sched.start()

你可能感兴趣的:(技术,数据库连接,python,远程连接)