Getting some columns from a table in python


import mysql.connector as mysql

db = mysql.connect(
    host="localhost",
    user="root",
    passwd="[email protected]",
    database="realprogrammer"
)

cursor = db.cursor()

## defining the Query
query = "SELECT user_name FROM user"

## getting 'user_name' column from the table
cursor.execute(query)

## fetching all usernames from the 'cursor' object
usernames = cursor.fetchall()

for username in usernames:
    print(username)
Leave a Comment