Created
September 27, 2022 10:24
-
-
Save shidktbw/2ad0afe0bd45fe464986c2c27a595506 to your computer and use it in GitHub Desktop.
my sql py
This file contains hidden or 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
host = "your_host" | |
user = "your_user" | |
password = "your_password" | |
db_name = "your_db_name" |
This file contains hidden or 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
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