跟踪MySQL中的最后读取记录

时间:2019-06-04 17:36:11

标签: mysql mysql-workbench mysql-python

我在MySQL中有2个数据库:

1)输入Latitude-Longitude_dB(此后称为“ latlong_db”):它具有从GPS跟踪设备读取的每个读数的纬度和经度。

input_latlong dB

2)Weather_db:我从dB1读取输入的latlong,并为每对latlong计算“当前”天气数据(例如:湿度,cloud_coverage)。将此天气数据写入Weather_db。

weather_dB

问题是:我需要跟踪最后读取的记录(哪个“输入latlong”)。这样一来,我就不会为已经讲过的latlong重新计算weather_data。如何跟踪最近读取的input_latlong?

非常感谢您。


编辑: 1)对于那些一直在问“数据库v / s表”问题的人,答案是我正在从1个数据库中读取并写入第2个数据库中。连接到两个数据库的“ config.json”如下:

{
"Tracker_ds_locallatlongdb": {
        "database": "ds_testdb1",
        "host": "XXXXXXXXXXX",
        "port": XXXX,
        "user": "XXXX",
        "password": "XXXXX"
    },
    "Tracker_ds_localweatherdb": {
    "database": "ds_testdb2",
        "host": "XXXXXXX",
        "port": XXXX,
        "user": "XXXX",
        "password": "XXXXX"
    }
}

2)我的Python脚本从input_latlong_db读取并写入weather_db概述如下。我正在使用OpenWeatherMap API计算给定纬度和经度的天气数据:

from pyowm import OWM
import json
import time
import pprint
import pandas as pd

import mysql.connector
from mysql.connector import Error



api_key = 'your api key'

def get_weather_data(my_lat, my_long):
    owm= OWM(api_key)
    obs= owm.weather_at_coords(my_lat.item() , my_long.item() )   #Use: <numpy.ndarray>.item:
    w= obs.get_weather()
    l= obs.get_location()

    city= l.get_name()
    cloud_coverage =w.get_clouds()
    .
    .
    .
    w_datatoinsert= [my_lat, my_long, w_latitude, w_longitude, city, weather_time_gmt,call_time_torontotime,
    short_status, detailed_status,
    temp_celsius, cloud_coverage, humidity, wind_deg, wind_speed,
    snow, rain, atm_pressure, sea_level_pressure,sunset_time_gmt ] #15 + act_latitude + act_longitude
    return w_datatoinsert

# ------------------------------------------------------------------------------------------------------------------------------------
spec_creds_1= {}
spec_creds_2= {}
def operation():
    with open('C:/Users/config.json') as config_file:
        creds_dict= json.load(config_file)
        spec_creds_1= creds_dict['Tracker_ds_locallatlongdb'] 
        spec_creds_2= creds_dict['Tracker_ds_localweatherdb']
        try:
            my_conn_1= mysql.connector.connect(**spec_creds_1 )
            if (my_conn_1.is_connected()):
                info_1= my_conn_1.get_server_info()
                print("Connected ..now reading the local input_latlong_db: ", info_1)
                try:
                    my_conn_2= mysql.connector.connect(**spec_creds_2)
                    if (my_conn_2.is_connected()):
                        info_2= my_conn_2.get_server_info()
                        print('Connected to write into the local weather_db: ', info_2)
                        cursor_2= my_conn_2.cursor()
                        readings_df= pd.read_sql("SELECT latitude, longitude FROM readings_table_19cols;", con= my_conn_1)
                        for index, row in readings_df.iterrows():
                            gwd= get_weather_data(row['latitude'], row['longitude'])
                            q= "INSERT INTO weather_table_19cols VALUES(" +        ",".join(["%s"]*len(gwd))     +    " ); "
                            cursor_2.execute(q, gwd)
                            my_conn_2.commit()
                except Error as e:
                    print("Error while connecting to write into the local weather_db: ", e)
                finally:
                    if (my_conn_2.is_connected()):
                        cursor_2.close()
                        my_conn_2.close()
                        print("Wrote 1 record to the local weather_db.")
        except Error as e:
            print("Error connecting to the local input latlong_db: ", e)
        finally:
            if (my_conn_1.is_connected()):
                my_conn_1.close() # no cursor present for 'my_conn_1'
                print("Finished reading all the input latlongs ...and finished inserting ALL the weather data.")


#-------------------------------------------------------------------------------



if __name__=="__main__":
    operation()

1 个答案:

答案 0 :(得分:0)

  1. 在Input_latlong_table- readings_table_19cols中:我创建了一个自动递增的readings_id作为主键,并创建了一个名为read_flag的列,其默认值为0。

  2. weather_table_19cols中,我创建了一个自动递增的weather_id作为主键。

  3. 由于我的方法涉及读取输入的latlong记录并将其天气数据相应地写入weather_table,因此我比较了readings_table_19colsweather_table_19cols的索引。如果它们匹配,则表示已读取输入记录,我将read_flag设置为1。

..

for index_1, row_1 in readings_df_1.iterrows():

                            gwd= get_weather_data(row_1['imei'], row_1['reading_id'] ,row_1['send_time'],row_1['latitude'], row_1['longitude'])

                            q_2= "INSERT INTO weather_table_23cols(my_imei, my_reading_id, actual_latitude, actual_longitude, w_latitude, w_longitude, city, weather_time_gmt, OBD_send_time_gmt, call_time_torontotime, \
                            short_status, detailed_status, \
                            temp_celsius, cloud_coverage, humidity, wind_deg, wind_speed, \
                            snow, rain, atm_pressure, sea_level_pressure,sunset_time_gmt) VALUES("  +  ",".join(["%s"]*len(gwd))     +    " ); "

                            q_1b= "UPDATE ds_testdb1.readings_table_22cols re,  ds_testdb2.weather_table_23cols we \
                             SET re.read_flag=1 WHERE (re.reading_id= we.weather_id);"  # use the prefix 'db_name.table_name' if 1 cursor is being used for 2 different db's

                            cursor_2.execute(q_2, gwd)
                            my_conn_2.commit()
                            cursor_1.execute(q_1b) # use Cursor_1 for 1st query 
                            my_conn_1.commit()