How to write accented fields in sqlite3 via Python?

3

Hi everyone! I am doing a small program in python and using sqlite3 to save the database. The program is inserting, reading and deleting data in the database called "location". I would like to know if there is a way to display accented data when I read it on the console (terminal). See the image:

#JavaScript#ThisPythonfileusesthefollowingencoding:utf-8importos,sysimportsqlite3defInserir():###INSERINDO#solicitandoosdadosaousuáriop_nome=raw_input('Nome:')titulo_unicode=p_nome.decode('utf8')p_criado_em=raw_input('Criadoem(yyyy-mm-dd):')print(titulo_unicode)#inserindodadosnatabelacursor.execute("""
INSERT INTO posicao (nome, criado_em)
VALUES (?,?)
""", (titulo_unicode, p_criado_em))

conn.commit()
def Ler():
    ###LENDO
    # lendo os dados
    cursor.execute("""
    SELECT * FROM posicao;
    """)

    for linha in cursor.fetchall():
        print(linha)

def Excluir():
    id_posicao = int(raw_input('Digite a posicao: '))

    # excluindo um registro da tabela
    cursor.execute("""
    DELETE FROM posicao
    WHERE id = ?
    """, (id_posicao,))

    conn.commit()

    print('Registro excluido com sucesso.')


conn = sqlite3.connect('posicao.db')
#conn.text_factory = str
cursor = conn.cursor()
opcao = 1
while opcao!=4 and opcao!=0:
    print ('Menu:')
    print ('1 - Inserir dados: ')
    print ('2 - Ler dados: ')
    print ('3 - Deletar dados: ')
    print ('0 ou 4 - Sair!!!')
    opcao = int(raw_input('Opção: '))
    if opcao == 1:
        Inserir()
    if opcao == 2:
        Ler()
    if opcao == 3:
        Excluir()

conn.close()

How do you do when reading the bank that the accented words do not appear coded?

    
asked by anonymous 25.06.2016 / 01:55

1 answer

1

I was able to solve it! I put the code to print each column of the tuple separately.

def Ler():
    ###LENDO
    # lendo os dados
    cursor.execute("""
    SELECT * FROM posicao;
    """)

    for linha in cursor.fetchall():
        print "|\t",(linha)[0],"\t|",(linha)[1],"\t|", (linha)[2]

    
25.06.2016 / 02:43