The right way to Choose From MySQL in Python


First, you have to the mysql.connector. In case you are not sure of methods to get this setup, consult with The right way to Set up MySQL Driver in Python.

The right way to Choose From a MySQL Desk in Python

import mysql.connector

mydb = mysql.connector.join(
  host = "localhost",
  person = "username",
  password = "YoUrPaSsWoRd",
  database = "your_database"
)

mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM clients")
myresult = mycursor.fetchall()

for x in myresult:
  print(x)

The right way to Choose Columns From a MySQL Desk in Python

import mysql.connector

mydb = mysql.connector.join(
  host = "localhost",
  person = "username",
  password = "YoUrPaSsWoRd",
  database = "your_database"
)

mycursor = mydb.cursor()
mycursor.execute("SELECT identify, handle FROM clients")
myresult = mycursor.fetchall()

for x in myresult:
  print(x)

The right way to Fetch Solely a Single Row From MySQL in Python

import mysql.connector

mydb = mysql.connector.join(
  host = "localhost",
  person = "username",
  password = "YoUrPaSsWoRd",
  database = "your_database"
)

mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM clients")
myresult = mycursor.fetchone()

print(myresult)

Leave a Reply