Python

Getting some columns from a table in python

Pinterest LinkedIn Tumblr

import mysql.connector as mysql

db = mysql.connect(
    host="localhost",
    user="root",
    passwd="root@123",
    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)

Write A Comment