Python Forum

Full Version: Python MySQL - How to pick column row data as variable?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

New to python, I'm trying to do a ssh to one of the linux box using paramiko, I can already do this activity but I want to pick the credentials from mysql database instead from the conf.py file.

Here's my my sql connector script i'm testing, I get the output of two rows, username and password but how do I pick the "Username" column's value as a variable and "Password's" column value as a different variable? I'll eventually be using these variables as credentials when passing to the paramiko connect.username and password.

import mysql.connector

db = mysql.connector.connect(
    host="x.x.x.x",
    user="root",
    passwd="root",
    database="testdb"
    )

host = input("Hostname: ")

mycursor = db.cursor()
mycursor.execute("select username,password from hosts where host = '%s';" % host)
myresult = mycursor.fetchone()
for row in myresult:
    print(row)
Thanks, I will add the tags here onward.