Created
August 6, 2023 07:54
-
-
Save strumer69/2ed7e4b4df40598f88f42ffdeb8c64f5 to your computer and use it in GitHub Desktop.
python_SQL_connection
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
import mysql.connector | |
try: | |
connection = mysql.connector.connect(host='localhost', | |
database='sakila', | |
user='root', | |
password='sql12345678') | |
sql_select_Query = "select * from customer" | |
cursor = connection.cursor() | |
cursor.execute(sql_select_Query) | |
# get all records | |
records = cursor.fetchall() | |
print("Total number of rows in table: ", cursor.rowcount) | |
print('----------------------------------') | |
print("\nPrinting each row") | |
for row in records: | |
print("customer Id = ", row[0], ) | |
print("Name = ", row[2]) | |
print("family = ", row[3]) | |
print("Email = ", row[4], "\n") | |
print('---------------------') | |
except mysql.connector.Error as e: | |
print("Error reading data from MySQL table", e) | |
finally: | |
if connection.is_connected(): | |
connection.close() | |
cursor.close() | |
print("MySQL connection is closed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment