In This Article, You Will Know About Python MySQL Insert Table.
Python MySQL Select From – Before moving ahead, let’s know a bit about Python MySQL Insert Table.
Table of Contents
Select From a Table
Use “SELECT” statement to get the data from table.
Example: Select each row’s data from the “Stu_information” table, and show the result.
import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
username="your_MySQL_username",
password="Your_MySQL_password",
database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()
database_for_mysql.execute("SELECT * FROM Stu_information")
for result in database_for_mysql:
print(result)
Select particular column
Use ‘SELECT’ statement with column name to select some of the columns from the table.
Example: Select a few particular columns from the table.
import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
username="your_MySQL_username",
password="Your_MySQL_password",
database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()
database_for_mysql.execute("SELECT name, class FROM Stu_information")
for result in database_for_mysql:
print(result)
fetchone() Method
Use fetchone() method to return only one row from the table.
Example: Use fetchone() method to return only one row from the table.
import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
username="your_MySQL_username",
password="Your_MySQL_password",
database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()
database_for_mysql.execute("SELECT * FROM Stu_information")
one_row = database_for_mysql.fetchone()
print(one_row)