You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contrib/database/sqlalchemy-aggregation.md
+19-1Lines changed: 19 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
# SQLAlchemy
2
-
SQLAlchemy is a powerful and flexible SQL toolkit and Object-Relational Mapping (ORM) library for Python. It is a versatile library that bridges the gap between Python applications and relational databases.<br>
2
+
SQLAlchemy is a powerful and flexible SQL toolkit and Object-Relational Mapping (ORM) library for Python. It is a versatile library that bridges the gap between Python applications and relational databases.
3
3
4
4
SQLAlchemy allows the user to write database-agnostic code that can work with a variety of relational databases such as SQLite, MySQL, PostgreSQL, Oracle, and Microsoft SQL Server. The ORM layer in SQLAlchemy allows developers to map Python classes to database tables. This means you can interact with your database using Python objects instead of writing raw SQL queries.
5
5
@@ -10,9 +10,11 @@ SQLAlchemy allows the user to write database-agnostic code that can work with a
10
10
```bash
11
11
pip install sqlalchemy mysql-connector-python
12
12
```
13
+
13
14
* If not installed, you can install them using the above command in terminal,
14
15
15
16
## Establishing Connection with Database
17
+
16
18
* Create a connection with the database using the following code snippet:
17
19
```python
18
20
from sqlalchemy import create_engine
@@ -27,11 +29,13 @@ session = Session()
27
29
28
30
Base = declarative_base()
29
31
```
32
+
30
33
* The connection string **DATABASE_URL** is passed as an argument to **create_engine** function which is used to create a connection to the database. This connection string contains the database credentials such as the database type, username, password, and database name.
31
34
* The **sessionmaker** function is used to create a session object which is used to interact with the database
32
35
* The **declarative_base** function is used to create a base class for all the database models. This base class is used to define the structure of the database tables.
33
36
34
37
## Creating Tables
38
+
35
39
* The following code snippet creates a table named **"products"** in the database:
36
40
```python
37
41
from sqlalchemy import Column, Integer, String, Float
@@ -46,10 +50,12 @@ class Product(Base):
46
50
47
51
Base.metadata.create_all(engine)
48
52
```
53
+
49
54
* The **Product class** inherits from **Base**, which is a base class for all the database models.
50
55
* The **Base.metadata.create_all(engine)** statement is used to create the table in the database. The engine object is a connection to the database that was created earlier.
51
56
52
57
## Inserting Data for Aggregation Functions
58
+
53
59
* The following code snippet inserts data into the **"products"** table:
54
60
```python
55
61
products = [
@@ -63,39 +69,51 @@ products = [
63
69
session.add_all(products)
64
70
session.commit()
65
71
```
72
+
66
73
* A list of **Product** objects is created. Each Product object represents a row in the **products table** in the database.
67
74
* The **add_all** method of the session object is used to add all the Product objects to the session. This method takes a **list of objects as an argument** and adds them to the session.
68
75
* The **commit** method of the session object is used to commit the changes made to the database.
69
76
70
77
## Aggregation Functions
78
+
71
79
SQLAlchemy provides functions that correspond to SQL aggregation functions and are available in the **sqlalchemy.func module**.
80
+
72
81
### COUNT
82
+
73
83
The **COUNT** function returns the number of rows in a result set. It can be demonstrated using the following code snippet:
0 commit comments