HOME/Articles/

mysql example test-mysql (snippet)

Article Outline

Python mysql example 'test-mysql'

Modules used in program:

  • import mysql.connector

python test-mysql

Python mysql example: test-mysql

import mysql.connector

# The connect() constructor creates a connection to the MySQL server and returns a MySQLConnection object.
cnx = mysql.connector.connect(
    host="localhost",
    database="schema",
    user="root",
    password="password"
)

# Test the connection
print(cnx)

cursor = cnx.cursor()

cursor.execute("select * from TABLE_NAME")

for x in cursor:
    print(x)

cnx.close()