1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',
database='world',
user='username',
password='userpassword')
if connection.is_connected():
cursor = connection.cursor()
cursor.execute('select * from city')
record=cursor.fetchone()
print('fetched one record from world.city: ', record)
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed") |
Partager