1import mysql.connector
2
3cnx = mysql.connector.connect(user='scott', password='password',
4 host='127.0.0.1',
5 database='employees')
6cnx.close()
1import mysql.connector
2mydb = mysql.connector.connect(
3 host="localhost",
4 user="yourusername",
5 password="yourpassword",
6 port = 8888, #for Mamp users
7 database='whatever db you want'
8)
9print(mydb)
1import pymysql.cursors
2import pymysql
3
4# Connect to the database
5connection = pymysql.connect(host='localhost',
6 user='user',
7 password='passwd',
8 db='db',
9 charset='utf8mb4',
10 cursorclass=pymysql.cursors.DictCursor)
11
12try:
13 with connection.cursor() as cursor:
14 # Create a new record
15 sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
16 cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
17
18 # connection is not autocommit by default. So you must commit to save
19 # your changes.
20 connection.commit()
21
22 with connection.cursor() as cursor:
23 # Read a single record
24 sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
25 cursor.execute(sql, ('webmaster@python.org',))
26 result = cursor.fetchone()
27 print(result)
28finally:
29 connection.close()
30
1from sqlalchemy import types, create_engine
2import pymysql
3
4try:
5 conn = create_engine('mysql+pymysql://user:pass@IP/database_name')
6 print("MySQL Connection Sucessfull!!!!!!!!!!!")
7
8except Exception as err:
9
10 print("MySQL Connection Failed !!!!!!!!!!!")
11 print(err)
12