- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the fetchone() method? Explain its use in MySQL Python?
Fetchone() method
Fetchone() method is used when you want to select only the first row from the table. This method only returns the first row from the MySQL table.
Use of fetchone() method
The fetchone() is not used as a query to be used to the cursor object. The query passed is “SELECT *” which fetches all the rows from the table.Later , we operate fetchone() method on the result returned by “SELECT *”. The fetchone() method then fetches the first row from that result.
Steps you need to follow to fetch first row from a table using MySQL in python
import MySQL connector
establish connection with the connector using connect()
create the cursor object using cursor() method
create a query using “SELECT *” statement
execute the SQL query using execute() method
operate fetchone() method on the result returned by “SELECT *” query.
close the connection
Suppose, we have a table named “MyTable” and we want to get only the first row from it.
+----------+---------+-----------+------------+ | Name | Class | City | Marks | +----------+---------+-----------+------------+ | Karan | 4 | Amritsar | 95 | | Sahil | 6 | Amritsar | 93 | | Kriti | 3 | Batala | 88 | | Khushi | 9 | Delhi | 90 | | Kirat | 5 | Delhi | 85 | +----------+---------+-----------+------------+
Example
import mysql.connector db=mysql.connector.connect(host="your host", user="your username", password="your password",database="database_name") cursor=db.cursor() query="SELECT * FROM MyTable" cursor.execute(query) #the cursor object has all the rows returned by the query #get the first row using the fetchone() method first_row=cursor.fetchone() print(first_row)
The above code gets the first row from the table and prints it.
Output
(‘Karan’, 4, ‘Amritsar’ , 95)
- Related Articles
- What is JSON.stringify() and explain its use in javascript?
- What is JSON.parse() and and explain its use in javascript?
- What is the rollback() method in Python MySQL?
- Explain the use of AVG() function in MySQL using Python?
- What is Python commit() method in MySQL?
- Explain the use of SELECT DISTINCT statement in MySQL using Python?
- Explain the use of MIN() and MAX() using MySQL in Python?
- Explain the use of sql LIKE operator using MySQL in Python?
- Explain the use of COUNT() AND SUM() in MySQL using Python?
- Explain and show the use of UNION in MySQL using Python?
- What is the tongue? Explain its parts.
- What is unitary method ? Explain.
- What is the use of NCHAR in MySQL?
- What is the use of _.size() method in JavaScript?
- What is the use of setBounds() method in Java?
