Changed poems adding to packet add_all

This commit is contained in:
Dmitriy Shishkov 2023-09-14 01:30:08 +03:00
parent 860ea43091
commit 1f7f69e933
Signed by: dm1sh
GPG Key ID: 027994B0AA357688

View File

@ -11,6 +11,7 @@ from . import auth_utils, orm_models, pydantic_schemas
# Загружаем стихи # Загружаем стихи
async def add_poems_to_db(async_db: AsyncSession): async def add_poems_to_db(async_db: AsyncSession):
poems = []
f1 = open('poems.txt', encoding='utf-8', mode='r')#открыть фаил для чтения на русском f1 = open('poems.txt', encoding='utf-8', mode='r')#открыть фаил для чтения на русском
for a in range(1, 110): for a in range(1, 110):
f1.seek(0)#перейти к началу f1.seek(0)#перейти к началу
@ -35,8 +36,11 @@ async def add_poems_to_db(async_db: AsyncSession):
author += str1 author += str1
poem = orm_models.Poems(title=name, text=stixi, author=author) poem = orm_models.Poems(title=name, text=stixi, author=author)
# В конце каждой итерации добавляем в базу данных # В конце каждой итерации добавляем в базу данных
async_db.add(poem) poems.append(poem)
async_db.commit()
async_db.add_all(poems)
await async_db.commit()
# close the file # close the file
f1.close() f1.close()