Fill in field with "00-00-0000" when empty

Asked

Viewed 201 times

0

I am making a recording in the database (Postgresql) with Python, I use a base on .txt.

Problem: At the base have some records are blank, and is giving error at the time of recording, occurs in the Date field. would like to know how do I put a if or a Exception to fill with by Ex: 00-00-0000 in such cases.
I can even put the whole table on Var Char, but data_ingresso = valores[12] I need to record as Data same, because I will account with her in the bank.

In case the structure of the question is bad I can try to improve.

import psycopg2

con = psycopg2.connect( database='cap',host='localhost', user='postgres', password='**********')
cur = con.cursor()
ref_arquivo = open("C:/Users/br0151338587/Desktop/!Alertas/ampla_Novo_Tempo_Ordens_GA.txt",'r')

input("Conexão OK - Enter para continuar")
sql = 'truncate "tb_ordens_ga"'
cur.execute(sql)


for linha in ref_arquivo:
    linha = linha.replace("'","")
    linha = linha.replace('"','')
    valores = linha.split("|")


    numero_ordem = valores[0]
    data_exec_visita = valores[1]
    data_estado_format = valores[2]
    numero_cliente = valores[3]
    cliente = valores[4]
    endereco = endereco = valores[5]
    telefone = valores[6]
    bairro = valores[7]
    municipio = valores[8]
    des_servico = valores[9]
    descricao_etapa = valores[10]
    descricao_estado = valores[11]
    data_ingresso = valores[12]
    observacao_executante = valores[13]
    observacao_atendente = valores[14]

    sql = 'insert into "public"."tb_ordens_ga"("numero_ordem","data_exec_visita","data_estado_format","numero_cliente","cliente","endereco","telefone","bairro",' \
      '"municipio","des_servico","descricao_etapa","descricao_estado","data_ingresso","observacao_executante","observacao_atendente"'') \
       values('+"'"+numero_ordem+"'"+', '+"'"+data_exec_visita+"'"+', '+"'"+data_estado_format+"'"+', '+\
      "'"+numero_cliente+"'"+', '+"'"+cliente+"'"+', '+"'"+endereco+"'"+', '+"'"+telefone+"'"+', '+"'"+bairro+"'"+', '+"'"+municipio+"'"\
      ', '+"'"+des_servico+"'"+', '+"'"+descricao_etapa+"'"+', '+"'"+descricao_estado+"'"+', '+"'"+data_ingresso+"'"+\
      ', '+"'"+observacao_executante+"'"+', '+"'"+observacao_atendente+"'"+')'
    cur.execute(sql)
    con.commit()

ref_arquivo.close()

con.close()

Image - Code

  • You may not want to record a date of "0000-00-00" in the bank: that day there is no in the Gregorian calendar. I marked the question as a duplicate of one where I deal with the question in detail, even though the question is not identical.

1 answer

-1

Treat before building the query:

data_exec_visita = '00-00-0000' if valores[1]=='' else valores[1]
data_estado_format = '00-00-0000' if valores[2]=='' else valores[2]

make a test before to see if the return of valores[1] and valores[2] is really '' when Voce says this "empty".

  • Remembering that 00-00-0000 is not a valid date, and postgresql will not accept this date

  • Yes, @nosklo, I just showed my solution to his question, but he can exchange it for a valid value.

  • Yes, well remembered, but I used a valid value here.

  • If the answer served, consider giving the acceptance (Green tick below the votes).

  • I do not think an answer that considers using that date without clarifying that it is not a valid date is constructive. (Invalid in date system used by us humans outside the computer - not only invalid "no postgres" or "in Python")

  • @jsbueno The answer used the data he brought in the question. You noticed this?

Show 1 more comment

Browser other questions tagged

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