-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Term Entry] Python SQL Connectors: SQLAlchemy
* [Term Entry]Python SQL Connectors: SQLAlchemy * Rename sqlalchemy.md to sqlalchemy.md file name fixed * Minor changes ---------
- Loading branch information
1 parent
43e2430
commit 0bf782d
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
content/python/concepts/sql-connectors/terms/sqlalchemy/sqlalchemy.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
Title: 'SQLAlchemy' | ||
Description: 'SQLAlchemy is a flexible SQL toolkit and Object-Relational Mapping (ORM) library for Python, designed to enable efficient and Pythonic database interactions.' | ||
Subjects: | ||
- 'Data Science' | ||
- 'Web Development' | ||
Tags: | ||
- 'Database' | ||
- 'SQL' | ||
- 'Python' | ||
CatalogContent: | ||
- 'learn-python-3' | ||
- 'paths/data-science' | ||
--- | ||
|
||
**SQLAlchemy** is a widely used Python library that provides both high-level ORM and low-level SQL query capabilities. It simplifies database interactions by allowing developers to use Python objects for database manipulation while also supporting raw SQL queries. SQLAlchemy supports various relational databases like MySQL, PostgreSQL, SQLite, and others, and is highly customizable to suit complex database models. With SQLAlchemy, developers can interact with databases in a flexible and efficient manner, making it an essential tool for web development and data science projects. | ||
|
||
## Syntax | ||
|
||
```pseudo | ||
from sqlalchemy import create_engine | ||
# Create an engine instance | ||
engine = create_engine('dialect+driver://username:password@host:port/database') | ||
# Example of querying using SQLAlchemy | ||
with engine.connect() as connection: | ||
result = connection.execute("SELECT * FROM users") | ||
for row in result: | ||
print(row) | ||
``` | ||
|
||
- `create_engine`: Initializes the connection to the database with the provided database URI. | ||
- `dialect`: Specifies the type of database (e.g., `mysql`, `postgresql`, `sqlite`). | ||
- `driver` (Optional): Specifies the database driver to be used. | ||
- `username`, `password`: Credentials for authentication. | ||
- `host`, `port`: Database server's location. | ||
- `database`: Name of the database to connect to. | ||
|
||
## Example | ||
|
||
The following example shows how to connect to a SQLite database and query it using SQLAlchemy: | ||
|
||
```py | ||
from sqlalchemy import create_engine | ||
|
||
# Create an engine instance for SQLite | ||
engine = create_engine('sqlite:///example.db') | ||
|
||
# Connect to the database and then execute a query | ||
with engine.connect() as connection: | ||
result = connection.execute("SELECT * FROM users") | ||
for row in result: | ||
print(row) | ||
``` | ||
|
||
This code will output the rows from the `users` table in the `example.db` SQLite database. |