A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/python/python_sqlite3_cursor_fetchone_function.htm below:

Python SQLite cursor.fetchone() Function

Python SQLite cursor.fetchone() Function

The Python cursor.fetchone() function retrieves data from the next query, this function returns a single sequence or None when there is no data is available.

This function can be used as an iterator, and this fetechone() can also be called fetchall() and fetchmany(). It is particularly useful when we expect a query to return a single row or when we want to process rows one at a time.

Syntax

Following is the syntax for the cursor.fetchone() function.

row = cursor.fetchone()
Parameters

This function doesn't take any parameters.

Return Value

If a single row is available, then this function returns the next row as a tuple.

Example

Consider the following EMPLOYEES table which stores employees ID, Name, Age, Salary, City and Country −

ID Name Age Salary City Country 1 Ramesh 32 2000.00 Maryland USA 2 Mukesh 40 5000.00 New York USA 3 Sumit 45 4500.00 Muscat Oman 4 Kaushik 25 2500.00 Kolkata India 5 Hardik 29 3500.00 Bhopal India 6 Komal 38 3500.00 Saharanpur India 7 Ayush 25 3500.00 Delhi India Example 1

Here's an example using the cursor.fetchone() function to retrieve the first row from the table.

import sqlite3
conn = sqlite3.connect('res.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM employees")
row = cursor.fetchone()
print(row)
Output

We will get the result as follows −

ID Name Age Salary City Country 1 Ramesh 32 2000.00 Maryland USA Example 2

In the example below, we are going to delete the first row from the table using the cursor.fetchone(), then this function will return the second row from the database.

import sqlite3
conn = sqlite3.connect('res.db')
cursor = conn.cursor()
cursor.execute("DELETE FROM employees WHERE ID = 1")
conn.commit()
cursor.execute("SELECT  * FROM employees")
print(cursor.fetchone())
conn.close()
Output

The result is obtained as follows −

ID Name Age Salary City Country 2 Mukesh 40 5000.00 New York USA Example 3

Now, we are updating the first row in the database. Using the cursor.fetchone() function, it will then return the updated first row from the database.

import sqlite3
conn = sqlite3.connect('res.db')
cursor = conn.cursor()
cursor.execute("UPDATE employees SET Salary = 5500.00 WHERE ID = 1")
cursor.commit()
cursor.execute("SELECT * FROM employees WHERE ID = 1")
print(cursor.fetechone())
conn.close()
Output

The output is obtained as follows −

ID Name Age Salary City Country 1 Ramesh 32 5500.00 Maryland USA

python_modules.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4