Skip to content

Instantly share code, notes, and snippets.

@Mike-F362
Created January 23, 2023 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mike-F362/1e90bcd59bf541d9fb21505202e8ea97 to your computer and use it in GitHub Desktop.
Save Mike-F362/1e90bcd59bf541d9fb21505202e8ea97 to your computer and use it in GitHub Desktop.
Snippet to get csv-output from MySQL-Client
import shutil
def read_sql_client_tmpfile(query, connection):
df = None
tmp_dir = tempfile.mkdtemp()
try:
local_tmp_file = f'{tmp_dir}/sql_tmpfile'
cmd = f"mysql -u {username} -p{password} -h {db_server} -D {dbname} -e '{query}' >{local_tmp_file}"
if not os.system(cmd):
df = pd.read_csv(local_tmp_file)
finally:
shutil.rmtree(tmp_dir)
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment