SQLite with Python PDF

Summary

This document provides an introduction to SQLite and how to use it with Python. It covers topics such as database definition, importing and exporting data, and various SQL operations. This document is a useful guide for those beginning their database journey.

Full Transcript

SQLite (Database) Introduction to Databases Definition: A database is an organized collection of data that allows easy access, management, and updating. Types of Databases: ○ Relational Databases: Data is organized in tables with rows and columns SQLite, MySQL ○ NoSQL...

SQLite (Database) Introduction to Databases Definition: A database is an organized collection of data that allows easy access, management, and updating. Types of Databases: ○ Relational Databases: Data is organized in tables with rows and columns SQLite, MySQL ○ NoSQL Databases: Used for unstructured or semi-structured data MongoDB SQL (Structured Query Language) ○ A language used to interact with relational databases ○ Common operations: Create, Read, Update, Delete What is SQLite SQLite is a lightweight, embedded, serverless database. ○ Features: Simple and easy to set up (no installation needed). Stores data in a single file (.db file). Suitable for small to medium-sized applications (e.g., mobile apps, testing). ○ Advantages: Zero-configuration: No server setup required. Cross-platform: Works on multiple operating systems. Fast and efficient for local data storage. ○ Disadvantages: Not suitable for large-scale applications with heavy concurrent users. Setting Up SQLite in Python To interact with an SQLite database in Python, you need to follow these steps: ○ Import the sqlite3 module. ○ Create a connection to the SQLite database file (if the file doesn't exist, SQLite will create it). ○ Create a cursor object to execute SQL queries. ○ Perform SQL operations (e.g., creating tables, inserting data, querying data). ○ Commit changes and close the connection when done. Operations There are opeartions we can perform with SQLite: ○ INSERT - storing of data ○ SELECT - retrieval of data ○ UPDATE - modification of data ○ DELETE - removing of data Hands-on example We should connect to the database, and also close (disconnect) after use. Operations There are opeartions we can perform with SQLite: ○ INSERT - storing of data ○ SELECT - retrieval of data ○ UPDATE - modification of data ○ DELETE - removing of data INSERT SQL INSERT Syntax: Using placeholders for secure queries to avoid SQL Injection SELECT SQL SELECT Syntax: Fetching rows with fetchone(), fetchall() UPDATE SQL UPDATE Syntax: Updating records based on conditions DELETE SQL DELETE Syntax: Deleting records based on conditions END

Use Quizgecko on...
Browser
Browser