Skip to content

Instantly share code, notes, and snippets.

@shidktbw
Created September 27, 2022 10:24
Show Gist options
  • Save shidktbw/2ad0afe0bd45fe464986c2c27a595506 to your computer and use it in GitHub Desktop.
Save shidktbw/2ad0afe0bd45fe464986c2c27a595506 to your computer and use it in GitHub Desktop.
my sql py
host = "your_host"
user = "your_user"
password = "your_password"
db_name = "your_db_name"
import pymysql
from config import host, user, password, db_name
try:
connection = pymysql.connect(
host=host,
port=3306,
user=user,
password=password,
database=db_name,
cursorclass=pymysql.cursors.DictCursor
)
print("successfully connected...")
print("#" * 20)
try:
with connection.cursor() as cursor:
select_all_rows = "SELECT * FROM `users`"
cursor.execute(select_all_rows)
# cursor.execute("SELECT * FROM `users`")
rows = cursor.fetchall()
for row in rows:
print(row)
print("#" * 20)
finally:
connection.close()
except Exception as ex:
print("Connection refused...")
print(ex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment