101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    Python

    Receive aemail containing the next unit.
    • Refreshing Python Basics
      • 1.1Python Data Structures
      • 1.2Syntax and Semantics
      • 1.3Conditionals and Loops
    • Introduction to Object-Oriented Programming
      • 2.1Understanding Class and Objects
      • 2.2Design Patterns
      • 2.3Inheritance, Encapsulation, and Polymorphism
    • Python Libraries
      • 3.1Numpy and Matplotlib
      • 3.2Pandas and Seaborn
      • 3.3SciPy
    • Handling Files and Exception
      • 4.1Reading, writing and manipulating files
      • 4.2Introduction to Exceptions
      • 4.3Handling and raising Exceptions
    • Regular Expressions
      • 5.1Introduction to Regular Expressions
      • 5.2Python’s re module
      • 5.3Pattern Matching, Substitution, and Parsing
    • Databases and SQL
      • 6.1Introduction to Databases
      • 6.2Python and SQLite
      • 6.3Presentation of Data
    • Web Scraping with Python
      • 7.1Basics of HTML
      • 7.2Introduction to Beautiful Soup
      • 7.3Web Scraping Case Study
    • Python for Data Analysis
      • 8.1Data cleaning, Transformation, and Analysis using Pandas
      • 8.2Data visualization using Matplotlib and Seaborn
      • 8.3Real-world Data Analysis scenarios
    • Python for Machine Learning
      • 9.1Introduction to Machine Learning with Python
      • 9.2Scikit-learn basics
      • 9.3Supervised and Unsupervised Learning
    • Python for Deep Learning
      • 10.1Introduction to Neural Networks and TensorFlow
      • 10.2Deep Learning with Python
      • 10.3Real-world Deep Learning Applications
    • Advanced Python Concepts
      • 11.1Generators and Iterators
      • 11.2Decorators and Closures
      • 11.3Multithreading and Multiprocessing
    • Advanced Python Concepts
      • 12.1Generators and Iterators
      • 12.2Decorators and Closures
      • 12.3Multithreading and Multiprocessing
    • Python Project
      • 13.1Project Kick-off
      • 13.2Mentor Session
      • 13.3Project Presentation

    Databases and SQL

    Python and SQLite: A Comprehensive Guide

    general-purpose programming language

    General-purpose programming language.

    SQLite is a software library that provides a relational database management system. The lite in SQLite means light weight in terms of setup, database administration, and required resource. SQLite is an embedded SQL database engine. Unlike most other SQL databases, SQLite does not have a separate server process. SQLite is open source and is a great choice for local/client storage in application software such as web browsers.

    SQLite Basics

    SQLite is a C library that provides a lightweight disk-based database that doesn’t require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage. It’s also possible to prototype an application using SQLite and then port the code to a larger system such as PostgreSQL or Oracle.

    SQLite Data Types

    SQLite uses a more general dynamic type system. In SQLite, the datatype of a value is associated with the value itself, not with its container. The dynamic type system of SQLite is backwards compatible with the more common static type systems of other database engines in the sense that SQL statements that work on statically typed databases should work the same way in SQLite. However, the dynamic typing in SQLite allows it to do things which are not possible in traditional rigidly typed databases.

    SQLite Commands: SELECT, INSERT, UPDATE, DELETE

    SQLite commands are similar to SQL commands. The SELECT command is used to select data from a database. The data returned is stored in a result table, called the result-set. The INSERT command is used to insert new data into a database. The UPDATE command is used to modify the existing records in a table. The DELETE command is used to delete existing records in a table.

    Python SQLite3 Module

    Python’s sqlite3 module provides an API for SQLite database. It comes with Python distribution.

    Connecting to SQLite Database

    To use SQLite3 in Python, first of all, you will have to import the sqlite3 module and then create a connection object which will connect us to the database and will let us execute the SQL statements. A connection object is created using the connect() function:

    import sqlite3 conn = sqlite3.connect('example.db')

    Creating Tables

    Once you have a connection, you can create a cursor object and call its execute() method to perform SQL commands:

    c = conn.cursor() c.execute('''CREATE TABLE stocks (date text, trans text, symbol text, qty real, price real)''')

    Inserting, Updating, and Deleting Records

    Inserting, updating, and deleting records is done using SQL commands:

    c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)") conn.commit()

    Querying Data

    To retrieve data after executing a SELECT statement, you can either treat the cursor as an iterator, call the cursor’s fetchone() method to retrieve a single matching row, or call fetchall() to get a list of the matching rows.

    for row in c.execute('SELECT * FROM stocks ORDER BY price'): print(row)

    Error Handling

    SQLite Exceptions

    If an error occurs, the sqlite3 module raises an exception. All exceptions raised by sqlite3 are subclasses of a base class named Error.

    Handling SQLite Exceptions in Python

    You can handle SQLite exceptions in Python using try...except blocks. If an error occurs inside the try block, Python will raise an exception and execution will jump immediately to the except block.

    try: c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)") except sqlite3.IntegrityError: print("Record already exists.")

    In conclusion, SQLite is a powerful library that offers full, relational database functionality in a no-server, zero-configuration package. Python's sqlite3 module makes it easy to use this database for a wide range of applications.

    Test me
    Practical exercise
    Further reading

    Howdy, any questions I can help with?

    Sign in to chat
    Next up: Presentation of Data