Python Database Tutorial
Python being a high-level language provides support for various databases. We can connect and run queries for a particular database using Python and without writing raw queries in the terminal or shell of that particular database, we just need to have that database installed in our system.
A database is an organized collection of data stored electronically. It is managed by a Database Management System (DBMS) that allows users to access, update, and manage data efficiently. Databases are primarily categorized into two types:
1. Relational Databases (RDBMS)
Store data in structured tables with rows and columns. They support relationships between data entities. Examples:
- MySQL
- PostgreSQL
- SQLite
2. Non-relational Databases (NoSQL)
Store data in various formats like documents, key-value pairs, or graphs, offering more flexibility. For Examples:
- MongoDB
- Cassandra
In this tutorial, we will focus on how to use Python with the most commonly used databases: MySQL, SQLite, and MongoDB. We will cover how to connect to these databases, run queries, and manage data efficiently using Python. Additionally, we will explore how to work with JSON data in Python.
Python MySQL
Python MySQL Connector is a Python driver that helps to integrate Python and MySQL. This Python MySQL library allows the conversion between Python and MySQL data types. MySQL Connector API is implemented using pure Python and does not require any third-party library.
MySQL Basics
Python MySQL Queries
- Create Database
- Create Table
- Insert into Table
- Select Query
- Where Clause
- Order By Clause
- Delete Query
- Drop Table
- Update Query
- Limit Clause
- Join
- Commit & RollBack Operation in Python
Note: For more information, refer to our detailed Python MySQL Tutorial
Python SQLite
Python SQLite3 module is used to integrate the SQLite database with Python. It is a standardized Python DBI API 2.0 and provides a straightforward and simple-to-use interface for interacting with SQLite databases. There is no need to install this module separately as it comes along with Python after the 2.5x version.
SQLite Basics
- Introduction
- Python SQLite – Connecting to Database
- SQLite Datatypes and its Corresponding Python Types
Python SQLite Queries
- Cursor Object
- Create Table
- Insert Data
- Select Data from Table
- WHERE Clause
- ORDER BY Clause
- LIMIT Clause
- JOIN Clause
- Deleting Data in Table
- DROP Table
- Update Data
- Update Specific Column
Note: For more information, refer to our detailed Python SQLite3 Tutorial
Python JSON
JSON JavaScript Object Notation is a format for structuring data. It is mainly used for storing and transferring data between the browser and the server. Python too supports JSON with a built-in package called json. This package provides all the necessary tools for working with JSON Objects including parsing, serializing, deserializing, and many more.
JSON Basics
Python JSON - Reading and Writing
Parsing JSON
- How to Parse Data From JSON into Python?
- How To Convert Python Dictionary To JSON?
- Python – Convert JSON to string
- Ways to convert string to json object
- Convert JSON data Into a Custom Python Object
Python JSON - Serializing and Deserializing
- Serializing JSON
- json.dump()
- json.dumps()
- Deserialize JSON to Object
- json.load()
- json.loads()
- Encoding and Decoding Custom Objects in JSON
- Serialize and Deserialize complex JSON
Note: For more information, refer to Python JSON Tutorials
Python MongoDB
MongoDB is one of the most popular NoSQL database. It is a cross-platform, object-oriented database. Basically NoSQL means MongoDB does not store data in the table or relational format rather provide a different mechanism for storage and retrieval of data. This is called BSON which is similar to JSON. That’s why MongoDB offers high speed, high availability, and high scalability.
MongoDB Basics
- MongoDB and Python
- Guide to Install MongoDB with Python | Windows
- What is a PyMongo Cursor?
- Create a database in MongoDB using Python
Python MongoDB Queries
- What is a MongoDB Query?
- Insert and Update Data Query
- insert_one
- insert_many
- Difference Between insert, insert_one, and insert_many queries in Pymongo
- Update_one
- Update_many
- insert, replace_one, replace_many Queries
- Delete Data and Drop Collection
- Delete_one
- Delete_many
- Find
- find_one
- find_one_and_update
- find_one_and_delete
- find_one_and_replace
- Sort
- distinct
- rename
- bulk_write
- $group (aggregation) Operation
- Limit
- Nested Queries in PyMongo
Python MongoDB Indexing
- Indexing in MongoDB using Python
- Create_index
- How to create index for MongoDB Collection using Python
- Get all the information of a Collection’s indexes using PyMongo
- drop_index
- How to Drop all the indexes in a Collection using PyMongo
- How to rebuild all the indexes of a collection using PyMongo
Note: For more information, refer to Python MongoDB Tutorial