I have a solution that might work for you. It should give you a nice little pandas.DataFrame.
First, you have to read the query inside the sql file. Then just use the pd.read_sql_query() instead of pd.read_sql()
I am sure you know it, but here is the doc for the function.
# Read the sql file and execute the query
with open('filename.sql', 'r') as query:
# connection == the connection to your database, in your case prob_db
DF = pd.read_sql_query(query.read(),connection)
I can assure you that it is working with T-SQL, but I never used it with MySQL.
Answer from Geof on Stack OverflowI have a solution that might work for you. It should give you a nice little pandas.DataFrame.
First, you have to read the query inside the sql file. Then just use the pd.read_sql_query() instead of pd.read_sql()
I am sure you know it, but here is the doc for the function.
# Read the sql file and execute the query
with open('filename.sql', 'r') as query:
# connection == the connection to your database, in your case prob_db
DF = pd.read_sql_query(query.read(),connection)
I can assure you that it is working with T-SQL, but I never used it with MySQL.
This is a MWE of how it worked for me:
query = open('./query_file.sql', 'r')
db_config = {
'server': server address,
'port': port,
'user': user,
'password': password,
'database': db name
}
try:
sql_conn = pymssql.connect(**db_config)
logging.info('SQL connection is opened')
avise_me_df = pd.read_sql(query.read(),sql_conn)
logging.info('pandas df recorded')
except OperationalError as e:
connected = False
logging.error('Error reading data from SQL table')
else:
connected = True
finally:
if connected:
sql_conn.close()
logging.info('SQL connection is closed')
I hope this might help.