Skip to content

Instantly share code, notes, and snippets.

@flavioVitoriano
Last active August 6, 2020 21:47
Show Gist options
  • Save flavioVitoriano/fbd776b174bb32c13a03d699f9d7d73d to your computer and use it in GitHub Desktop.
Save flavioVitoriano/fbd776b174bb32c13a03d699f9d7d73d to your computer and use it in GitHub Desktop.
script inserir async python
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