Python Forum
Problem with inserting a string in to Sqlite db - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem with inserting a string in to Sqlite db (/thread-16519.html)



Problem with inserting a string in to Sqlite db - darktitan - Mar-03-2019

Hi
I got a problem inserting an string to a sqlite database. What im trying to do is to take a time from the database and calculate how much time have elapsed from it to the time right now and insert the result back in the database. the code work as long as i just print out the result and dont insert anything to the database. But when i try inserting it to the daabase i get an error.


from datetime import datetime
import time
import random
import sqlite3

conn = sqlite3.connect('database.db')
c = conn.cursor()

ts = time.time()
date = datetime.fromtimestamp(ts).strftime('%Y-%m-%d')
time = datetime.fromtimestamp(ts).strftime('%H:%M:%S')

namn = "David andersen"
c.execute('SELECT * FROM tider WHERE Namn=? AND datum=?', (namn, date))
data = c.fetchall()     
for row in data:
  version = (row[3])


#s1 = '07:00:00'
#s2 = '16:00:00' # for example
FMT = "%H:%M:%S"
tdelta = datetime.strptime(time, FMT) - datetime.strptime(version, FMT)
#tdelta = "03:00:00"

print (tdelta)

c.execute("INSERT INTO arbetadetider (Namn, timmar) VALUES(?,?)", (namn, tdelta))
conn.commit()
c.close()  
The line that causes the error in the sqlite insert line is.
tdelta = datetime.strptime(time, FMT) - datetime.strptime(version, FMT)
Because when i comment it out and use this line tdelta = "03:00:00" everything works.

The error i get is.
Error:
Traceback (most recent call last): File "C:\Users\xzenon\Desktop\scripts\timecalculon\timecalculon.py", line 28, in <module> c.execute("INSERT INTO arbetadetider (Namn, timmar) VALUES(?,?)", (namn, tdelta)) sqlite3.InterfaceError: Error binding parameter 1 - probably unsupported type.



RE: Problem with inserting a string in to Sqlite db - stranac - Mar-03-2019

The sqlite3 module does not support the timedelta datatype.
You can get around this by registering an adapter function which will convert it to a type sqlite3 knows about:
https://docs.python.org/3/library/sqlite3.html#using-adapters-to-store-additional-python-types-in-sqlite-databases


RE: Problem with inserting a string in to Sqlite db - darktitan - Mar-03-2019

(Mar-03-2019, 09:03 AM)stranac Wrote: The sqlite3 module does not support the timedelta datatype.
You can get around this by registering an adapter function which will convert it to a type sqlite3 knows about:
https://docs.python.org/3/library/sqlite3.html#using-adapters-to-store-additional-python-types-in-sqlite-databases

OK i fixed the problem but i dont know if this is the correct way to fix the problem but my solution is
tdelta = str(datetime.strptime(time, FMT) - datetime.strptime(version, FMT))



RE: Problem with inserting a string in to Sqlite db - stranac - Mar-03-2019

That's also fine if you don't mind manually calling str each time you want to insert a timedelta.