-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_test.py
29 lines (25 loc) · 900 Bytes
/
mysql_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import mysql.connector
from mysql.connector import Error
try:
# Connect MySQL/test1 database
connection = mysql.connector.connect(
host='localhost', # host name
database='classmate', # database name
user='Ta', # account
password='123456') # password
if connection.is_connected():
# version of database
db_Info = connection.get_server_info()
print("version of database:", db_Info)
# show information of the current database
cursor = connection.cursor()
cursor.execute("SELECT DATABASE();")
record = cursor.fetchone()
print("current database:", record)
except Error as e:
print("connection failure:", e)
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("database connection closed")