Last active
December 11, 2021 07:27
-
-
Save kirang89/7161185 to your computer and use it in GitHub Desktop.
Connect to a remote mysql database using Python and MySQLdb
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
############################################################ | |
# # | |
# Simple script to connect to a remote mysql database # | |
# # | |
# # | |
# Install MySQLdb package by running: # | |
# # | |
# pip install MySQL-python # | |
# # | |
############################################################ | |
import MySQLdb as db | |
HOST = "remote host" | |
PORT = 3306 | |
USER = "username of remote mysql instance" | |
PASSWORD = "password of remote mysql instance" | |
DB = "database name" | |
try: | |
connection = db.Connection(host=HOST, port=PORT, | |
user=USER, passwd=PASSWORD, db=DB) | |
dbhandler = connection.cursor() | |
dbhandler.execute("SELECT * from your_table") | |
result = dbhandler.fetchall() | |
for item in result: | |
print item | |
except Exception as e: | |
print e | |
finally: | |
connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @ault2910, that link did the trick for me.