Return mysql query in python list

Asked

Viewed 323 times

-1

Good Afternoon! How do I take the data that comes back from the sql query and play it in a list knowing that I need to return the query = to return the file folder + the files larger than the query.? Thanks for your help.

#Busca no banco de dados o ultimo Id inserido
cursor = conn.cursor()
cursor.execute('Select posicao, ArquivoLog from logbin where Id = (select max(Id) from logbin)')
recordset = cursor.fetchall()
execucao_anterior = recordset[0]
#Retorno da consulta mysql 
#bin-log.000028

#le os arquivos da pasta logs
os.chdir("C:\\Backups\\Logs\\")
lista = glob.glob("*.0*")
tm_lista = (len(lista))
posicao_na_lista = ((lista.index(arquivo_anterior)))   
print(posicao_na_lista)

#retorno
#bin-log.000028
#bin-log.000029
#bin-log.000030
#bin-log.000031

1 answer

0

Hello! I made a query using Python’s Sqlite3 and saving the return of the query in a "txt" file. I hope it helps you.

import sqlite3
conteudo = []

conection = sqlite3.connect('banco.db')
c = conection.cursor()
c.execute('CREATE TABLE IF NOT EXISTS REMESSA (DATA VARCHAR(50), HORA VARCHAR(50), DESCRICAO VARCHAR(50))')
c.execute('INSERT INTO REMESSA (DATA,HORA,DESCRICAO) VALUES ("10","20","30")')
conection.commit()

teste = c.execute('SELECT * FROM REMESSA')

for row in teste:
    conteudo.append(row)
arquivo = open('C:/Teste/Arquivo_Teste_2.txt','w')
arquivo.writelines(str(conteudo))
arquivo.close()

Abs!

  • Lucas thanks for the help, but I really need to do a comparison of the data I have a database that stores the log information. in my query above I look for the last insertion in my bd, now I need to compare with the files I search in the folder with those of the query and save in a variable. bd query return was bin-log.000035 And I have in the folder of the following files 'bin-log.000034', 'bin-log.000035', 'bin-log.000036', 'bin-log.000037', 'bin-log.000038' I need to get the files [bin-log.000035', 'bin-log.000036', 'bin-log.000037', 'bin-log.000038' And play in a variable.

  • Suddenly you can use the contents of this link: https://answall.com/questions/200249/listar-somente-arquivos-sem-pastas-em-dir%C3%A9torio-em-python It is demonstrated how to list the files of a given directory, then just compare the two lists in a for and the different saved in a variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.