11
The following method c.filtraNome(nome)
carry out a consultation at the bank (sqlite3), however, I don’t know how I can group each field and return the already formatted query in the following way:
Nome: Fulano Silvia
Idade: 19
CPF: 00000000000
Email: [email protected]
Celular: 1131345678
Cidade: Nova Vila
UF: MT
Data do Cadastro: 2015-08-25
I tried to concatenate the strings with the operator +
and with the method ''.join('String')
but I was unsuccessful in any of them.
Routine that performs the query:
def filtraNome(self,nome):
self.db.cursor.execute(""" SELECT * FROM clientes WHERE nome like ? ; """,(['%' + nome + '%']))
for campo in self.db.cursor.fetchall():
self.dados = 'Nome: '.join(campo[1]).join('Idade: '.join(campo[2])).join('CPF: '.join(campo[3])).join('Email: '.join(campo[4])).join('Celular: '.join(campo[5])).join('Cidade: '.join(campo[6])).join('UF: '.join(campo[7])).join('Data do cadastro: '.join(campo[8])) #Gera um erro nesta linha.
return self.dados
The error being generated when using the method join()
:
self.dados = 'Nome: '.join(campo[1]).join('Idade: '.join(campo[2])).join('CPF: '.join(campo[3])).join('Email: '.join(campo[4])).join('Celular: '.join(campo[5])).join('Cidade: '.join(campo[6])).join('UF: '.join(campo[7])).join('Data do cadastro: '.join(campo[8]))
TypeError: can only join an iterable
Routine calling class method ClienteDB
:
if __name__ == '__main__':
c = ClienteDB()
if c.cria_tabela():
print("Tabela %s criada com sucesso.", c.tb_nome)
#c.inserir_registro()
#c.listar_todos()
nome = raw_input("Informe um nome para pesquisa: ")
print(c.filtraNome(nome))
How can I concatenate the fields returned from the query in Python?
which error is generated on the line ?
– Pedro Laini
This mistake: Typeerror: can only Join an iterable
– gato
Join only works with arrays or "things that can be iterated"... with '+' gives some error ?
– Pedro Laini
Yes with more also returns an error, in the last record
"Data: " + campo[8]
, the two are returning error.– gato
is probably giving an "index out of range"... I’m seeing that you started picking up the array fields from Indice 1... but the array starts at 0.. That’s right ?
– Pedro Laini
The zero index and the id I don’t show him.
– gato
What’s the mistake you’re making then? Do the following... to avoid a "conversation" in the comments, edit your question by putting more details, showing what approaches you used and what errors were when you used them.
– Pedro Laini
@Denercarvalho, try concatenating using
Nome: + str(campo[1])...
and let me know the result so I can post it as an answer. I really think that’s the problem.– Jéf Bueno