0

I have a txt file, which contains lines:

Date=28/11/2016
Time=12:16:06

I want to create SQL-query, so I need to format them in true way.

connect = psycopg2.connect(database='eco_db', user='postgres', host='localhost', password='1111', port=5433)
cursor = connect.cursor()

entry=list()
for line in infile:
    line = line.strip()
    value = line.split('=', 1)
    entry.append(value[1])

sql_query=str(entry[0] + ' '+entry[1])


SQL = '''
INSERT INTO eco.noise (time) VALUES (%s);

'''
data = (sql_query)
cursor.execute(SQL, data)

connect.commit()
cursor.close()
connect.close()

But the sql_query has a wrong format. Mayve I should use datetime package? Many thanks.

ERROR:

    cursor.execute(SQL, data)
psycopg2.DataError: date/time field value out of range: "28/11/2016 12:16:06"
LINE 2: ...ERT INTO eco.noise (time_noise) VALUES ('28/11/201...
                                                             ^
HINT:  Perhaps you need a different "datestyle" setting.

Solution: use datetime.strptime(YOUR_DATE, '%d/%m/%Y'), where '%d/%m/%Y' - some format.

user7198142
  • 111
  • 1
  • 2
  • 8

0 Answers0