Skip to content

Instantly share code, notes, and snippets.

@strumer69
Created August 6, 2023 07:54
Show Gist options
  • Save strumer69/2ed7e4b4df40598f88f42ffdeb8c64f5 to your computer and use it in GitHub Desktop.
Save strumer69/2ed7e4b4df40598f88f42ffdeb8c64f5 to your computer and use it in GitHub Desktop.
python_SQL_connection
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