Python MySQL Select From







googletag.cmd.push(function() { googletag.display('div-gpt-ad-1422003450156-2'); });



Python MySQL Select From



❮ Previous
Next ❯



Select From a Table


To select from a table in MySQL, use the "SELECT" statement:



Example


Select all records from the "customers" table, and display the
result:



import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
 
user="yourusername",
  passwd="yourpassword",
  database="mydatabase"
)

mycursor =
mydb.cursor()

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

Run example »



Note: We use the fetchall()
method, which fetches all rows from the last executed statement.






Selecting Columns


To select only some of the columns in a table, use the "SELECT" statement
followed by the column name(s):



Example


Select only the name and address columns:



import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
 
user="yourusername",
  passwd="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT name,
address FROM
customers")

myresult = mycursor.fetchall()

for x in myresult:
 
print(x)

Run example »




Using the fetchone() Method


If you are only interested in one row, you can use the
fetchone() method.


The fetchone() method will return the first row of
the result:



Example


Fetch only one row:



import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
 
user="yourusername",
  passwd="yourpassword",
  database="mydatabase"
)

mycursor =
mydb.cursor()

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchone()

print(myresult)

Run example »





❮ Previous
Next ❯

Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods