Python MongoDB
🐍 Python MongoDB In Python MongoDB is a NoSQL database that stores data in JSON-like documents.It can interact with MongoDB using the pymongo library. 1️⃣ Install PyMongo pip install pymongo 2️⃣ Connect to MongoDB...
🐍 Python MongoDB In Python MongoDB is a NoSQL database that stores data in JSON-like documents.It can interact with MongoDB using the pymongo library. 1️⃣ Install PyMongo pip install pymongo 2️⃣ Connect to MongoDB...
🐍 Python MySQL JOIN — Complete Guide In MySQL, JOINs are used to combine rows from two or more tables based on a related column.Python can execute JOIN queries using the mysql-connector-python module. 1️⃣...
🐍 Python MySQL CRUD Example import mysql.connector from mysql.connector import Error# —————————– 1️⃣ Connect to MySQL
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
try: conn = mysql.connector.connect( host="localhost", user="root", password="your_password", # replace with your MySQL password database="testdb" # make sure this DB exists ) if conn.is_connected(): print("✅ Connected to MySQL") except Error as e: print("❌ Error while connecting:", e) exit() |
2️⃣ Create Cursor
|
1 |
cursor = conn.cursor() |
3️⃣ Create Table
|
1 2 3 4 5 6 7 8 |
cursor.execute(""" CREATE TABLE IF NOT EXISTS students ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), age INT ) """) print("✅ Table 'students' ready") |
4️⃣ Insert Records
|
1 2 3 4 5 6 7 8 9 |
def insert_student(name, age): sql = "INSERT INTO students (name, age) VALUES (%s, %s)" values = (name, age) cursor.execute(sql, values) conn.commit() print(f"Inserted: {name}, {age}") insert_student("Vipul", 25) insert_student("Riya", 22) |
5️⃣...
🐍 Python MySQL Python can interact with MySQL databases using modules like mysql-connector-python or PyMySQL.This allows you to store, retrieve, and manipulate data from Python programs. 1️⃣ Install MySQL Connector pip install mysql-connector-python 2️⃣...
🐍 Python File Handling — Complete Guide Python provides built-in functions to create, read, write, and manipulate files.Files are essential for storing data persistently outside of your program. 1️⃣ Opening a File
|
1 |
file = open("example.txt", "mode") |
Mode...
🐍 Python Inner Classes — Complete Guide In Python, a class defined inside another class is called an inner class (or nested class).Inner classes are useful to group classes logically and hide implementation details....
🐍 Python Encapsulation Encapsulation is an OOP principle that restricts direct access to some attributes and methods of a class.It protects data and allows controlled access through getter and setter methods. 1️⃣ What is...
🐍 Python Polymorphism Polymorphism is an OOP concept that means “many forms”.In Python, it allows objects of different classes to be treated in a similar way. 1️⃣ Types of Polymorphism in Python Duck Typing...
🐍 Python Inheritance Inheritance is a core concept of Object-Oriented Programming (OOP).It allows a class (child) to inherit attributes and methods from another class (parent). 1️⃣ Why Use Inheritance? Reusability: Avoid rewriting code. Extensibility:...
🐍 Python Class Methods In Python, class methods are methods that operate on the class itself, not on instances (objects).They are defined using the @classmethod decorator. 1️⃣ Key Points About Class Methods Decorated with...