Thread: database not accepting data properly
Hey guys, I hope I used the Mailing List properly. I am trying to upload a datetime.timedelta object into a column of type interval... But when I query it says datetime.interval(0).
here are the lines where the error probably is:-print(cur.mogrify("""SELECT %s, %s, %s, %s""", (self.start_time.date(), self.start_time.time(), datetime.datetime.now() - self.start_time, desc_text)))
cur.execute("""INSERT INTO sessionlogs(sessiondate, sessiontime, sessionduration, sessiondesc)
VALUES(%s, %s, %s, %s);""", (self.start_time.date(), self.start_time.time(), datetime.datetime.now() - self.start_time, desc_text))
cur.execute("""INSERT INTO sessionlogs(sessiondate, sessiontime, sessionduration, sessiondesc)
VALUES (%s, %s, %s, %s)""", (datetime.datetime.now().date(), datetime.datetime.now().time(), datetime.timedelta(1, 18268), "This is a manual entry"))
Attachment
On 02/21/2016 02:45 AM, Shaan Repswal wrote: > Hey guys, I hope I used the Mailing List properly. I am trying to upload > a datetime.timedelta object into a column of type interval... But when I > query it says datetime.interval(0) Can you post the actual error message? What are you querying that gets you datetime.interval[0]? > > here are the lines where the error probably is:- > print(cur.mogrify("""SELECT %s, %s, %s, %s""", (self.start_time.date(), > self.start_time.time(), datetime.datetime.now() - self.start_time, > desc_text))) > cur.execute("""INSERT INTO sessionlogs(sessiondate, > sessiontime, sessionduration, sessiondesc) > VALUES(%s, %s, %s, %s);""", > (self.start_time.date(), self.start_time.time(), datetime.datetime.now() > - self.start_time, desc_text)) > > although when I wrote another script to enter a specific code, it was > working just fine. > > datetime.datetime.now() - self.start_time is the time interval which > seems to be where the error is. > > when I did this :- > cur.execute("""INSERT INTO sessionlogs(sessiondate, sessiontime, > sessionduration, sessiondesc) > VALUES (%s, %s, %s, %s)""", > (datetime.datetime.now().date(), datetime.datetime.now().time(), > datetime.timedelta(1, 18268), "This is a manual entry")) > > It was working quite fine. > > I will attach the two scripts > > Sorry for the uber messy code. I hope you can read through my whole > script as it isn't very big. I am using Gtk 3+ here so be warned... :P > > > -- Adrian Klaver adrian.klaver@aklaver.com
On 02/22/2016 02:03 AM, Shaan Repswal wrote: Ccing list. > That was so stupid of me. I am so sorry. When I use a script to query > the database, the fourth column of my database is of type "interval" I > store datetime.timedelta() objects in it from Python 3.4 But everytime I > query it with the attached querying script it always returns > datetime.timedelta(0) as the values of my sessionduration column. So to be clear no error is thrown, correct? You are just not seeing what you expect. > > I am storing them there from the testProductivityApp.py script that I > sent to you in an earlier email. You will need to install GTK 3 for > Python 3.4 to run that script. So bascally the datetime.timedelta() > objects are stored, but they are always datetime.timedelta(0,0,0.0) Any > idea why that could be happening? First what are the values in that field in the database itself? Second could it be (datetime.datetime.now() - self.start_time) is returning the correct result. In other words that now() and self.start_time are the same. Have you looked at what those values are as the script runs? I ask because you have self.start_time created twice in your code: 1) In the __init__ as self.start_time = datetime.datetime 2) In startRecording as self.start_time = datetime.datetime.now() I am not sure, but I have to believe that is causing at least part of your issue. > > However to troubleshoot the program I wrote another micro script (also > sent with the last email) called manualaitestdbentry.py which was to > check if I could store datetime.timedelta() objects in the postgresql > database, turns out I can store the datetime.timedelta() objects with > their seconds, days and microseconds attributes intact. > > On Sun, Feb 21, 2016 at 9:28 PM, Adrian Klaver > <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> wrote: > > On 02/21/2016 02:45 AM, Shaan Repswal wrote: > > Hey guys, I hope I used the Mailing List properly. I am trying > to upload > a datetime.timedelta object into a column of type interval... > But when I > query it says datetime.interval(0) > > > Can you post the actual error message? > > What are you querying that gets you datetime.interval[0]? > > > > here are the lines where the error probably is:- > print(cur.mogrify("""SELECT %s, %s, %s, %s""", > (self.start_time.date(), > self.start_time.time(), datetime.datetime.now() - self.start_time, > desc_text))) > cur.execute("""INSERT INTO sessionlogs(sessiondate, > sessiontime, sessionduration, sessiondesc) > VALUES(%s, %s, %s, %s);""", > (self.start_time.date(), self.start_time.time(), > datetime.datetime.now() > - self.start_time, desc_text)) > > although when I wrote another script to enter a specific code, > it was > working just fine. > > datetime.datetime.now() - self.start_time is the time interval which > seems to be where the error is. > > when I did this :- > cur.execute("""INSERT INTO sessionlogs(sessiondate, sessiontime, > sessionduration, sessiondesc) > VALUES (%s, %s, %s, %s)""", > (datetime.datetime.now().date(), datetime.datetime.now().time(), > datetime.timedelta(1, 18268), "This is a manual entry")) > > It was working quite fine. > > I will attach the two scripts > > Sorry for the uber messy code. I hope you can read through my whole > script as it isn't very big. I am using Gtk 3+ here so be > warned... :P > > > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com> > > -- Adrian Klaver adrian.klaver@aklaver.com
On 02/21/2016 02:45 AM, Shaan Repswal wrote: > Hey guys, I hope I used the Mailing List properly. I am trying to upload > a datetime.timedelta object into a column of type interval... But when I > query it says datetime.interval(0). > Had some time to run the attached code. Other then making a change to the database in connect() and adding some print functions it is the same. Running the application I got an interval in the sessionduration field. Details below: postgres@test=# \d sessionlogs Table "public.sessionlogs" Column | Type | Modifiers -----------------+------------------------+----------- sessiondate | date | sessiontime | time without time zone | sessionduration | interval(0) | sessiondesc | text | aklaver@killi:~> python testProductivityApp.py ('First', <type 'datetime.datetime'>) ('Second', datetime.datetime(2016, 2, 22, 13, 38, 34, 870868)) SELECT '2016-02-22'::date, '13:38:34.870868'::time, '0 days 4.497017 seconds'::interval, 'Enter here how you utilised your time.' ('Second', datetime.datetime(2016, 2, 22, 13, 38, 42, 440964)) SELECT '2016-02-22'::date, '13:38:42.440964'::time, '0 days 184.828224 seconds'::interval, 'Enter here how you utilised your time.' ('Second', datetime.datetime(2016, 2, 22, 13, 41, 50, 29445)) SELECT '2016-02-22'::date, '13:41:50.029445'::time, '0 days 568.953356 seconds'::interval, 'Enter here how you utilised your time.' ('Second', datetime.datetime(2016, 2, 22, 13, 51, 51, 293405)) SELECT '2016-02-22'::date, '13:51:51.293405'::time, '0 days 0.611630 seconds'::interval, 'Enter here how you utilised your time.' ('Second', datetime.datetime(2016, 2, 22, 13, 52, 17, 473494)) SELECT '2016-02-22'::date, '13:52:17.473494'::time, '0 days 0.417363 seconds'::interval, 'Enter here how you utilised your time. postgres@test=# select * from sessionlogs ; sessiondate | sessiontime | sessionduration | sessiondesc -------------+-----------------+-----------------+---------------------------------------- 2016-02-22 | 13:38:34.870868 | 00:00:04 | Enter here how you utilised your time. 2016-02-22 | 13:38:42.440964 | 00:03:05 | Enter here how you utilised your time. 2016-02-22 | 13:41:50.029445 | 00:09:29 | Enter here how you utilised your time. 2016-02-22 | 13:51:51.293405 | 00:00:01 | Enter here how you utilised your time. 2016-02-22 | 13:52:17.473494 | 00:00:00 | Enter here how you utilised your time. (5 rows) The only way I could get only 0's was to have a duration less then 0.5 seconds. So the code works here. -- Adrian Klaver adrian.klaver@aklaver.com