I am working on a Flask project with Postgresql as my database. I have a schema called interest which looks like
create table interest(
usr int,
activity_category int,
activity_subcategory int,
pid int,
start_time timestamp,
end_time timestamp,
budget int check (budget >= 0),
primary key (usr, activity_category, activity_subcategory, pid, start_time, end_time),
foreign key (usr) references users(userid),
foreign key(pid, activity_category, activity_subcategory) references location(pid, aid, aaid)
);
I am trying to insert values into the interest table. At present, I have starttime and endtime as strings but I guess I need to somehow typecast them to timestamp. I have tried these two methods but none of them work.
entry_by_location = g.conn.execute("insert into interest(usr, activity_category, activity_subcategory, pid, start_time, end_time, budget) values (%d, %d, %d, %d, to_timestamp(%s::text, 'YYYY-MM-DD HH:MI'), to_timestamp(%s::text, 'YYYY-MM-DD HH:MI'), %d)" %(int(uid), int(aaid), int(aid), int(pid), starttime, endtime, budget))
and
entry_by_location = g.conn.execute("insert into interest(usr, activity_category, activity_subcategory, pid, start_time, end_time, budget) values (%d, %d, %d, %d, %s::timestamp, %s::timestamp, %d)" %(int(uid), int(aaid), int(aid), int(pid), starttime, endtime, budget))
The error that I get is -
ProgrammingError: (psycopg2.ProgrammingError) syntax error at or near "10" LINE 1: ... end_time, budget) values (1, 2, 7, 1, 2016-11-15 10:13::tim... ^ [SQL: 'insert into interest(usr, activity_category, activity_subcategory, pid, start_time, end_time, budget) values (1, 2, 7, 1, 2016-11-15 10:13::timestamp, 2016-10-15 11:24::timestamp, 1000)']
I've spent two days trying to figure this out. Any sort of help would be really appreciated.
Thank you!
'%s'