@@ -62,12 +62,11 @@ from psqlpy.extra_types import SmallInt, Integer, BigInt, Float32, Float64
6262async def main () -> None :
6363 # It uses default connection parameters
6464 db_pool: Final = ConnectionPool()
65-
66- await db_pool.execute(
67- " INSERT INTO numbers (index, elf_life, elon_musk_money) VALUES ($1, $2, $3, $4, $5)" ,
68- [SmallInt(101 ), Integer(10500 ), BigInt(300000000000 ), Float32(123.11 ), Float64(222.12 )],
69- )
70- db_pool.close()
65+ async with db_pool.acquire() as connection:
66+ await connection.execute(
67+ " INSERT INTO numbers (index, elf_life, elon_musk_money) VALUES ($1, $2, $3, $4, $5)" ,
68+ [SmallInt(101 ), Integer(10500 ), BigInt(300000000000 ), Float32(123.11 ), Float64(222.12 )],
69+ )
7170```
7271
7372::: important
@@ -96,16 +95,16 @@ async def main() -> None:
9695 # It uses default connection parameters
9796 db_pool: Final = ConnectionPool()
9897
99- await db_pool.execute(
100- " INSERT INTO banners (title, description) VALUES ($1, $2) " ,
101- [ " SomeTitle " , PyText( " Very long description" )] ,
102- )
103- # Alternatively, you can do this:
104- await db_pool.execute(
105- " INSERT INTO banners (title, description) VALUES ($1, $2) " ,
106- [PyVarChar( " SomeTitle " ), PyText( " Very long description" )] ,
107- )
108- db_pool.close( )
98+ async with db_pool.acquire() as connection:
99+ await connection.execute(
100+ " INSERT INTO banners (title, description) VALUES ($1, $2) " ,
101+ [ " SomeTitle " , PyText( " Very long description " )],
102+ )
103+ # Alternatively, you can do this:
104+ await connection.execute(
105+ " INSERT INTO banners (title, description) VALUES ($1, $2) " ,
106+ [PyVarChar( " SomeTitle " ), PyText( " Very long description " )],
107+ )
109108```
110109
111110## PyJSON & PyJSONB
@@ -140,6 +139,7 @@ from psqlpy.extra_types import PyJSON
140139async def main () -> None :
141140 # It uses default connection parameters
142141 db_pool: Final = ConnectionPool()
142+
143143 list_for_jsonb_field = [
144144 {" some" : " dict" },
145145 [
@@ -154,16 +154,15 @@ async def main() -> None:
154154 ]
155155 }
156156
157- await db_pool.execute(
158- " INSERT INTO users (additional_user_info) VALUES ($1)" ,
159- [PyJSONB(list_for_jsonb_field)],
160- )
161- await db_pool.execute(
162- " INSERT INTO users (additional_user_info) VALUES ($1)" ,
163- [dict_for_jsonb_field,],
164- )
165-
166- db_pool.close()
157+ async with db_pool.acquire() as connection:
158+ await connection.execute(
159+ " INSERT INTO users (additional_user_info) VALUES ($1)" ,
160+ [PyJSONB(list_for_jsonb_field)],
161+ )
162+ await connection.execute(
163+ " INSERT INTO users (additional_user_info) VALUES ($1)" ,
164+ [dict_for_jsonb_field,],
165+ )
167166```
168167
169168## PyMacAddr6 & PyMacAddr8
@@ -186,15 +185,14 @@ async def main() -> None:
186185 # It uses default connection parameters
187186 db_pool: Final = ConnectionPool()
188187
189- await db_pool.execute(
190- " INSERT INTO devices (device_macaddr6, device_macaddr8) VALUES ($1, $2)" ,
191- [
192- PyMacAddr6(" 08:00:2b:01:02:03" ),
193- PyMacAddr8(" 08:00:2b:01:02:03:04:05" ),
194- ],
195- )
196-
197- db_pool.close()
188+ async with db_pool.acquire() as connection:
189+ await connection.execute(
190+ " INSERT INTO devices (device_macaddr6, device_macaddr8) VALUES ($1, $2)" ,
191+ [
192+ PyMacAddr6(" 08:00:2b:01:02:03" ),
193+ PyMacAddr8(" 08:00:2b:01:02:03:04:05" ),
194+ ],
195+ )
198196```
199197
200198## Geo Types
@@ -222,17 +220,16 @@ async def main() -> None:
222220 # It uses default connection parameters
223221 db_pool: Final = ConnectionPool()
224222
225- await db_pool.execute(
226- " INSERT INTO geo_info VALUES ($1, $2, $3, $4, $5, $6)" ,
227- [
228- Point([1.5 , 2 ]),
229- Box([(1.7 , 2.8 ), (9 , 9 )]),
230- Path([(3.5 , 3 ), (9 , 9 ), (8 , 8 )]),
231- Line([1 , - 2 , 3 ]),
232- LineSegment([(5.6 , 3.1 ), (4 , 5 )]),
233- Circle([5 , 1.8 , 10 ]),
234- ],
235- )
236-
237- db_pool.close()
223+ async with db_pool.acquire() as connection:
224+ await connection.execute(
225+ " INSERT INTO geo_info VALUES ($1, $2, $3, $4, $5, $6)" ,
226+ [
227+ Point([1.5 , 2 ]),
228+ Box([(1.7 , 2.8 ), (9 , 9 )]),
229+ Path([(3.5 , 3 ), (9 , 9 ), (8 , 8 )]),
230+ Line([1 , - 2 , 3 ]),
231+ LineSegment([(5.6 , 3.1 ), (4 , 5 )]),
232+ Circle([5 , 1.8 , 10 ]),
233+ ],
234+ )
238235```
0 commit comments