66 lines
2.6 KiB
Python
66 lines
2.6 KiB
Python
import mysql.connector
|
|
from mysql.connector import Error
|
|
|
|
# Define the database connection details
|
|
db_configs = [
|
|
{
|
|
'user': 'snehalatha',
|
|
'password': 'paSsWord@#654',
|
|
'host': '35.153.190.180',
|
|
'database': 'kolm_lite_cardio_staging'
|
|
},
|
|
{
|
|
'user': 'snehalatha',
|
|
'password': 'paSsWord@#654',
|
|
'host': '35.153.190.180',
|
|
'database': 'kolm_lite_veterinary_staging'
|
|
},
|
|
{
|
|
'user': 'snehalatha',
|
|
'password': 'paSsWord@#654',
|
|
'host': '35.153.190.180',
|
|
'database': 'kolm_lite_oralhealth_staging'
|
|
}
|
|
]
|
|
|
|
def fetch_records(config):
|
|
try:
|
|
# Connect to the database
|
|
connection = mysql.connector.connect(
|
|
host=config['host'],
|
|
user=config['user'],
|
|
password=config['password'],
|
|
database=config['database']
|
|
)
|
|
if connection.is_connected():
|
|
|
|
cursor = connection.cursor(dictionary=True)
|
|
cursor.execute("SELECT kols.id,CONCAT_WS(' ',kols.first_name,kols.middle_name,kols.last_name) as kol_name, GROUP_CONCAT(DISTINCT projects.name) as project_name, clients.name as client_name,CONCAT(client_users.first_name,client_users.last_name) as user_name, GROUP_CONCAT(DISTINCT log_activities.transaction_name,':',date(log_activities.created_on)) as project_details FROM kols left join user_kols on user_kols.kol_id = kols.id inner join opt_inout_statuses on opt_inout_statuses.id = user_kols.opt_in_out_status left join log_activities on log_activities.miscellaneous1 = user_kols.kol_id left join client_users on client_users.id = user_kols.user_id left join clients on client_users.client_id = clients.id left join project_kols on project_kols.kol_id = user_kols.kol_id left join projects on projects.id = project_kols.project_id where log_activities.module ='opt_in_out' and log_activities.transaction_name in ('New','Opt-in Requested','Opt-out','Opt-in Approved','Opt-in Expired','Opt-in Received','Opt-in') group by kols.id;")
|
|
records = cursor.fetchall()
|
|
return records
|
|
|
|
except Error as e:
|
|
print(f"Error while connecting to MySQL: {e}")
|
|
return None
|
|
|
|
finally:
|
|
if connection.is_connected():
|
|
cursor.close()
|
|
connection.close()
|
|
|
|
# Loop through the databases and fetch records
|
|
|
|
def get_records():
|
|
all_records = []
|
|
for config in db_configs:
|
|
records = fetch_records(config)
|
|
if records:
|
|
all_records.extend(records)
|
|
|
|
return all_records
|
|
|
|
# Print or process the fetched records
|
|
# for record in all_records:
|
|
# print(record)
|
|
|