Path: blob/master/Generative AI for Intelligent Data Handling/Day 3 Running SQL Queries .ipynb
3074 views
Python offers several libraries and modules for interacting with SQL databases. Here are some of the most popular ones:
1. SQLite3: This is a built-in library in Python, which provides an interface for SQLite databases
2. SQLAlchemy: A powerful ORM library that supports multiple database backends (SQLite, MySQL, PostgreSQL, and more)
3.pyodbc: A library for accessing databases with ODBC (Open Database Connectivity)
4.MySQL Connector/Python: This is an official Oracle-supported driver to connect to MySQL databases
These Libraries provide various functionalities and are suitable for different types of SQL databases. Depending on your specific needs and the type of database you are working with, you can choose the appropriate library.
Steps:
Create a conn
Cursor()
exceute()
close connection()
SQLite3
Quick Practice:
: Working with SQLite3
Task 1: Setup and Create a Database Import the sqlite3 module. Create a connection to a new SQLite database called lab.db. Create a table called students with columns id (INTEGER, PRIMARY KEY) and name (TEXT).
: Exercise 2: Working with SQLAlchemy
Task 1: Setup and Create a Database Install SQLAlchemy if you haven't already: pip install sqlalchemy Import the necessary modules from SQLAlchemy. Create an SQLite database called lab_alchemy.db. Define a Student class with columns id (INTEGER, primary key) and name (String).