The Wayback Machine - https://web.archive.org/web/20241112191702/https://www.geeksforgeeks.org/python-sqlite-cursor-object/
Open In App

Python SQLite – Cursor Object

Last Updated : 16 Feb, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we are going to discuss cursor objects in sqlite3 module of Python.

Cursor Object

It is an object that is used to make the connection for executing SQL queries. It acts as middleware between SQLite database connection and SQL query. It is created after giving connection to SQLite database.

Syntax: cursor_object=connection_object.execute(“sql query”);

Example 1: Python code to create a hotel_data database and insert records into the hotel table.

Python3




# importing sqlite3 module
import sqlite3
 
 
# create connection by using object
# to connect with hotel_data database
connection = sqlite3.connect('hotel_data.db')
 
# query to create a table named FOOD1
connection.execute(''' CREATE TABLE hotel
         (FIND INT PRIMARY KEY     NOT NULL,
         FNAME           TEXT    NOT NULL,
         COST            INT     NOT NULL,
         WEIGHT        INT);
         ''')
 
# insert query to insert food  details in
# the above table
connection.execute("INSERT INTO hotel VALUES (1, 'cakes',800,10 )")
connection.execute("INSERT INTO hotel VALUES (2, 'biscuits',100,20 )")
connection.execute("INSERT INTO hotel VALUES (3, 'chocos',1000,30 )")
 
 
print("All data in food table\n")
 
# create a cousor object for select query
cursor = connection.execute("SELECT * from hotel ")
 
# display all data from hotel table
for row in cursor:
    print(row)


Output:

Image

Now go to your location and see the SQLite database is created.

Image

Example 2: Python code to display data from hotel table.

Python3




# importing sqlite3 module
import sqlite3
 
# create connection by using object
# to connect with hotel_data database
connection = sqlite3.connect('hotel_data.db')
 
 
# insert query to insert food  details
# in the above table
connection.execute("INSERT INTO hotel VALUES (1, 'cakes',800,10 )");
connection.execute("INSERT INTO hotel VALUES (2, 'biscuits',100,20 )");
connection.execute("INSERT INTO hotel VALUES (3, 'chocos',1000,30 )");
 
 
print("Food id and Food Name\n")
 
# create a cousor object for select query
cursor = connection.execute("SELECT FIND,FNAME from hotel ")
 
# display all data from FOOD1 table
for row in cursor:
     print(row)


Output:

Image



Previous Article
Next Article

Similar Reads

PYGLET – Getting System Mouse Cursor Object
In this article we will see how we can get the system mouse cursor object in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or can be set to fi
2 min read
Python Psycopg - Cursor class
The cursor class Enables Python scripts to use a database session to run PostgreSQL commands. The connection class is what creates cursors. cursor() method: They are permanently connected to the connection, and all instructions are run in the context of the database session covered by the connection. Cursors generated from the same connection aren'
5 min read
Python VLC MediaPlayer - Getting Cursor
In this article we will see how we can get the cursor position in the MediaPlayer object in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. MediPlyer object is the basic object in vlc module for playing the video. We can cre
2 min read
PyGame Set Mouse Cursor from Bitmap
In this article, we are going to see how to set the mouse cursor from bitmap using the PyGame module in Python. What is PyGame?It is a cross-platform set of Python modules designed for writing video games.It includes computer graphics and sound libraries designed to be used with the Python programming language.It can handle time, video, music, font
5 min read
PyQt5 QSpinBox - Setting cursor
In this article we will see how we can set the cursor to the spin box, a cursor is an indicator used to show the current position for user interaction on a computer monitor or other display device that will respond to input from a text input or pointing device. In order to do this we use setCursor method Syntax : spin_box.setCursor(cursor) Argument
2 min read
PyQt5 QSpinBox - Accessing the cursor
In this article we will see how we can get the cursor of the spin box, a cursor is an indicator used to show the current position for user interaction on a computer monitor or other display device that will respond to input from a text input or pointing device. We use setCursor method to set new cursor to it. In order to do this we use cursor metho
2 min read
PyQt5 QSpinBox - How to unset the cursor
In this article we will see how we can unset the cursor of the spin box, a cursor is an indicator used to show the current position for user interaction on a computer monitor or other display device that will respond to input from a text input or pointing device. We use setCursor method to set new cursor to it. In order to do this we use unsetCurso
2 min read
Convert PyMongo Cursor to JSON
Prerequisites: MongoDB Python Basics This article is about converting the PyMongo Cursor to JSON. Functions like find() and find_one() returns the Cursor instance. Let's begin: Importing Required Modules: Import the required module using the command: from pymongo import MongoClient from bson.json_util import dumps If MongoDB is already not installe
2 min read
Convert PyMongo Cursor to Dataframe
Prerequisites: MongoDB Python Basics This article is about converting the PyMongo Cursor to Pandas Dataframe. Functions like find() and find_one() returns the Cursor instance. Let's begin: Importing Required Modules: Import the required module using the command: from pymongo import MongoClient from pandas import DataFrame If MongoDB is already not
3 min read
PyQt5 QCalendarWidget - Changing Cursor Shape
In this article we will see how we can change the cursor shape for the QCalendarWidget. Cursor is the mouse pointer by default cursor shape i.e cursor look same for both window or calendar although there are a variety of cursor available for calendar, for example, split cursor, size cursor etc. In order to do this we will use setCursor method with
1 min read
PyQt5 QCalendarWidget - Getting Cursor Shape
In this article we will see how we can get the cursor shape of the QCalendarWidget. Cursor is the mouse pointer by default cursor shape i.e cursor look same for both window or calendar although there are a variety of cursor available for calendar, for example, split cursor, size cursor etc. By default, it has the arrow cursor but can be changed wit
2 min read
PyQt5 QCalendarWidget - Making Cursor Shape back to normal
In this article we will see how we can reset the cursor shape of the QCalendarWidget. Cursor is the mouse pointer by default cursor shape i.e cursor look same for both window or calendar although there are a variety of cursor available for calendar, for example, split cursor, size cursor etc. By default, it has the arrow cursor but can be changed w
2 min read
What is a PyMongo Cursor?
MongoDB is an open-source database management system that uses the NoSql database to store large amounts of data. MongoDB uses collection and documents instead of tables like traditional relational databases. MongoDB documents are similar to JSON objects but use a variant called Binary JSON (BSON) that accommodates more data types. What is a Cursor
2 min read
wxPython - Change Cursor image on StaticText
In this article we will learn how can we change cursor image when cursor hovers over the static text. We can do it by creating a cursor object and using SetCursor() function associated with wx.StaticText class of wxPython. SetCursor() takes wx.Cursor object as a parameter. Syntax: wx.StaticText.SetCursor(cursor) Parameters: Parameter Input Type Des
1 min read
wxPython - Change Cursor on hover on Button
In this article we will learn that how can we change cursor when it hovers on Button present in frame. We need to follow some steps as followed. Step 1- Create a wx.Image object with image you want to use as cursor image. Step 2- Create a wx.Cursor object and pass wx.Image object above created. Step 3- Set the cursor using SetCursor() function. Syn
1 min read
How to check if the PyMongo Cursor is Empty?
MongoDB is an open source NOSQL database, and is implemented in C++. It is a document oriented database implementation that stores data in structures called Collections (group of MongoDB documents). PyMongo is a famous open source library that is used for embedded MongoDB queries. PyMongo is widely used for interacting with a MongoDB database as py
2 min read
wxPython - Change cursor for ToolBar
In this article will learn how can we change the cursor to a custom image cursor when it hovers over the toolbar. To do this we need to follow some steps as follows. Step 1: Create wx.Image object of the image you like. Step 2: Create wx.Cursor object passing image as parameter. Step 3: Set cursor for toolbar using SetCursor() method. Syntax: wx.To
1 min read
PyQt5 QCommandLinkButton - Setting Default Cursor Back
In this article we will see how we can remove the new cursor and set the original cursor back to the QCommandLinkButton. Assigning cursor means the mouse cursor will assume the new shape when it's over the command link button. Cursor shape is basically cursor icons these are used to classify the actions. It can be set with the help of setCursor met
2 min read
PyQt5 QCommandLinkButton - Accessing Cursor
In this article we will see how we can get i.e access cursor of the QCommandLinkButton. Assigning cursor means the mouse cursor will assume the new shape when it's over the command link button. Cursor shape is basically cursor icons these are used to classify the actions. It can be set with the help of setCursor method. In order to do this we use c
2 min read
PyQt5 QCommandLinkButton - Assigning Cursor
In this article we will see how we can set i.e assign cursor to the QCommandLinkButton. Assigning cursor means the mouse cursor will assume the new shape when it's over the command link button. Cursor shape is basically cursor icons these are used to classify the actions. In order to do this we use setCursor method with the command link button obje
1 min read
PyQt5 QScrollBar – Setting Cursor
In this article we will see how we can set cursor to the QScrollBar. QScrollBar is a control that enables the user to access parts of a document that is larger than the widget used to display it. Slider is the scroll-able object inside the bar. Setting cursor means to set special cursor to the scroll bar which is only for the scroll bar only. In or
2 min read
PyQt5 QScrollBar – Getting Cursor
In this article we will see how we can get cursor of the QScrollBar. QScrollBar is a control that enables the user to access parts of a document that is larger than the widget used to display it. Slider is the scroll-able object inside the bar. Setting cursor means to set special cursor to the scroll bar which is only for the scroll bar only, it ca
2 min read
PyQt5 QScrollBar – Unsetting Cursor
In this article we will see how we can unset the special cursor of the QScrollBar. QScrollBar is a control that enables the user to access parts of a document that is larger than the widget used to display it. Slider is the scroll-able object inside the bar. Setting cursor means to set special cursor to the scroll bar which is only for the scroll b
2 min read
MoviePy – Getting color of a Frame of Video Clip where cursor touch
In this article, we will see how we can get a color of single frame at given time of the video file clip in MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF's. Video is formed by the frames, combination of frames creates a video each frame is an individual image. We can store the specif
2 min read
PYGLET – Setting Cursor
In this article we will see how we can set special cursor to the window in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or can be set to fill
2 min read
PYGLET – Drawing Mouse Cursor for Window
In this article we will see how we can draw mouse cursor for window in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or can be set to fill an
2 min read
PyQtGraph – Getting Cursor of Bar Graph
In this article we will see how we can get cursor of the bar graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) and second
3 min read
PyQtGraph – Setting Cursor of Bar Graph
In this article we will see how we can set cursor to the bar graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) and second
3 min read
PyQtGraph – Setting Custom Cursor for Image View
In this article, we will see how we can set a custom cursor to the image view object in PyQTGraph. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.). Wi
4 min read
PyQtGraph – Getting Custom Cursor of Image View
In this article, we will see how we can get a custom cursor of the image view object in PyQTGraph. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.). Wi
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg