Posts Tagged ‘python’

The basic steps in accessing a MySQL database is normally to connect, execute statement, fetch result, and then to close the connection. The following is the steps translated into Python codes;

# Import required module
import MySQLdb

# Connect
conn = MySQLdb.connect(host="localhost", port=3306, user="root", passwd="root123", db="mysql")
cursor = conn.cursor(  )

# Execute statement
stmt = "SELECT * FROM user"
cursor.execute(stmt)

# Fetch and output [...]


Categories