1

I'm making Car parking system, and I have some difficulities with SQL database. I'm selecting data from SQL database, but I need to get the time correctly that I could use it for further calculations. So for example I need to get the time that was inserted to database as VARCHAR, maybe the bad thing is that I needed to use other method as TIME, but that's not the case. The thing I need is to use this line Started_Parking = row [3]. This should get the time from database and after that, I should be able to see the time difference from the start when car was registered and current time. By doing that I should be able to calculate the sum which the "User" should pay for parking. So by short I just need to somehow get the time from database and use it for calculations. Here's my code, I also get errors when compiling :

Error while fetching data from PostgreSQL unsupported operand type(s) for -: 'datetime.datetime' and 'str'

try:
 connection = psycopg2.connect(user="postgres",
                                  password="Dziugas420",
                                  host="127.0.0.1",
                                  port="5432",
                                  database="postgres")
   cursor = connection.cursor()
   postgreSQL_select_Query = "select * from vartotojai WHERE carnum=('%s')" % car_numb
   cursor.execute(postgreSQL_select_Query) # PALEIST KOMANDA
   vartotoju_data = cursor.fetchall()  # READ DATA

   print("          CAR DETAILS: ")
   for row in vartotoju_data:
       print("Current ID: ", row[0])
       print("Car Number: ", row[1])
       print("Parked on: ", row[3], "\n")
       Pay_Time = datetime.datetime.now()
       Started_Parking = row [3] 
       Prastovetas_Laikas = Pay_Time - Started_Parking 
       print(Prastovetas_Laikas)
       # NOW LET'S CHECK IF THE TIME DIFFERENCE IS WORKING, LET'S SEE THE DIFFERENCE AFTER 20SECS. 
       time.sleep(20)
       Pay_Time2 = datetime.datetime.now()
       Prastovetas_Laikas2 = Pay_Time2 - Started_Parking 
       print(Prastovetas_Laikas2)`

**EDIT

Here's the code I use to import this time into database:

Car_Reg_Time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
postgres_insert_query = """ INSERT INTO vartotojai (CARNUM, TIME, LAIKAS) VALUES (%s,%s, %s)"""
record_to_insert = (car_numb, Reg_Tikslus_Laikas, Car_Reg_Time)

And here's the table of my database: DATABASE

! laikas in database is when car was registered, the time in database is the time when the injection was made.

1
Prastovetas_Laikas = Pay_Time - Started_Parking 

will not work

since Pay_Time is datetime.datetime and Started_Parking is str

you need to try to use datetime.strptime() to convert Started_Parking to correct type

and you want to store them as str in your DB using str(mydate)

|improve this answer|||||
  • Hey, please take a look at the post again, I added some details. – DzITC Sep 5 '19 at 13:16
  • Reg_Tikslus_Laikas is the time not when car registration was made, but when the import to sql were. – DzITC Sep 5 '19 at 13:22
  • Reg_Tikslus_Laikas = datetime.datetime.now() – DzITC Sep 5 '19 at 13:27
  • you need to pass all of your time to str before sending to DB – LinPy Sep 5 '19 at 13:28
  • (%s,%s, %s) means strings – LinPy Sep 5 '19 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.