Python MySQL Select From Table¶
To select from a table in MySQL, use the “SELECT” statement:
In [11]:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="itronix"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Selecting Columns¶
- To select only some of the columns in a table, use the “SELECT” statement followed by the column name(s):
In [12]:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="itronix"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT name, address FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
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:
In [13]:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="itronix"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT name, address FROM customers")
myresult = mycursor.fetchone()
print(myresult)