Skip to content

Instantly share code, notes, and snippets.

@yogesh-aggarwal
Created August 16, 2019 15:27
Show Gist options
  • Save yogesh-aggarwal/3ed8b0465eeab5a29670ed56d76d2b58 to your computer and use it in GitHub Desktop.
Save yogesh-aggarwal/3ed8b0465eeab5a29670ed56d76d2b58 to your computer and use it in GitHub Desktop.
Basic MySQL-Python connector code to connect database.
import mysql.connector
db_connection = mysql.connector.connect(
host= "localhost",
user= "root",
passwd= "root"
)
# creating database_cursor to perform SQL operation
db_cursor = db_connection.cursor()
# executing cursor with execute method and pass SQL query
# db_cursor.execute("CREATE DATABASE my_firs")
# get list of all databases
result = db_cursor.execute("SHOW DATABASES;")
print(result)
#print all databases
# print(db_cursor)
# for db in db_cursor:
# print(db)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment