Last active
August 6, 2020 21:47
-
-
Save flavioVitoriano/fbd776b174bb32c13a03d699f9d7d73d to your computer and use it in GitHub Desktop.
script inserir async python
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
from aiofile import AIOFile, LineReader | |
import pymysql.cursors | |
import asyncio | |
conn = pymysql.connect( | |
host="localhost", | |
user="root", | |
password="A1c2!!1aaa2", | |
db="dbl_matriz", | |
charset="utf8", | |
cursorclass=pymysql.cursors.DictCursor, | |
) | |
file_path = input("Digite o caminho do arquivo: ") | |
async def main(): | |
with conn.cursor() as cursor: | |
async with AIOFile(file_path, "r") as f: | |
async for line in LineReader(f): | |
data = line.split(" ") | |
data[2] = data[2].replace("\r", "") | |
data[2] = data[2].replace("\n", "") | |
data[2] = data[2].strip() | |
print(f"Inserindo registro {data[0]}") | |
sql = "INSERT INTO `mt` (`numero`, `combinacao`, `quina`) VALUES (%s, %s, %s)" | |
try: | |
cursor.execute(sql, (data[0], data[1], data[2])) | |
conn.commit() | |
except Exception as e: | |
print(f"Erro: ocorreu um erro ao inserir linha {data[0]} erro: {e}") | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment