Created
February 3, 2021 13:18
-
-
Save grekodev/a10fc2efa53d97697c81082327ea78a4 to your computer and use it in GitHub Desktop.
Script Python to migrate data from local to server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymysql | |
import pandas as pd | |
import math | |
import logging | |
LOG_FILE = "logging.log" | |
logging.basicConfig(filename=LOG_FILE,level=logging.DEBUG,format='%(asctime)s %(message)s', datefmt='%d/%m/%Y %H:%M:%S') | |
try: | |
def set_log(e, block): | |
error_str = 'ERROR: '+ str(e) + ' ' + block | |
logging.error(error_str) | |
print(error_str) | |
except Exception as e: | |
set_log(e, '::FUNCTIONS') | |
# CREATE CSV | |
try: | |
conn = pymysql.connect(host='localhost', | |
user='root', | |
password='', | |
database='db_name', local_infile=True) | |
cursor = conn.cursor() | |
query = 'CALL sp_select_user();' | |
results = pd.read_sql_query(query, conn) | |
results.to_csv("output.csv",header=False, index=False, sep='[') | |
conn.commit() | |
cursor.close() | |
except Exception as e: | |
set_log(e, '::CONECTION LOCAL') | |
#============================================================================= | |
# READ CSV AND SET DATA IN HOSTING | |
try: | |
conn_hosting = pymysql.connect(host='server', | |
user='user', | |
password='pass', | |
database='db_server', local_infile=True) | |
cursor_hosting = conn_hosting.cursor() | |
query_hosting = 'TRUNCATE TABLE db_table;' | |
cursor_hosting.execute(query_hosting) | |
cursor_hosting.execute("LOAD DATA LOCAL INFILE 'D:/DEV/python/output.csv' INTO TABLE db_table fields terminated by '[' lines terminated by '\n' (name_column,name_column,name_column);") | |
conn_hosting.commit() | |
cursor_hosting.close() | |
except Exception as e: | |
set_log(e, '::CONECTION HOSTING') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment