Python Forum
Problem with inserting a string in to Sqlite db
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with inserting a string in to Sqlite db
#1
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.
Reply
#2
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/sqlite...-databases
Reply
#3
(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/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))
Reply
#4
That's also fine if you don't mind manually calling str each time you want to insert a timedelta.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  string problem Mathisdlg 6 655 Aug-05-2020, 09:31 AM
Last Post: Mathisdlg
  Inserting values from multiple lists sqlite azulu 1 505 May-24-2020, 08:40 AM
Last Post: ibreeden
  Problem with sqlite bindings scratchmyhead 4 462 May-18-2020, 03:55 AM
Last Post: scratchmyhead
  Unicode string index problem luoheng 6 659 Nov-23-2019, 03:04 PM
Last Post: luoheng
  simple string & input problem kungshamji 5 851 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  Inserting new row into SQLite KevinBrown 5 965 Mar-28-2019, 07:43 PM
Last Post: nilamo
  IP string manipulation problem TheRealNoob 12 1,844 Feb-04-2019, 09:29 AM
Last Post: perfringo
  problem in replace string into file Saeid_Bibak 1 740 Jan-20-2019, 09:46 PM
Last Post: Gribouillis
  How to reliably split string containing multiple sqlite statements? shanepy 2 882 Nov-21-2018, 12:07 AM
Last Post: shanepy
  How to execute a string with multiple sqlite statements and print results? shanepy 0 789 Nov-20-2018, 10:19 PM
Last Post: shanepy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020