Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59678

How to round trip python and sqlite dates

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'output': 0.05; 'string': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'tismer': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; 'called.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'row)': 0.16; 'str,': 0.16; 'subject:dates': 0.16; 'temp': 0.16; 'timestamp': 0.16; 'typeerror:': 0.16; 'subject:python': 0.16; 'language': 0.16; 'bit': 0.19; 'any,': 0.19; 'memory': 0.22; 'programming': 0.22; 'import': 0.22; 'putting': 0.22; 'print': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'references': 0.26; 'skip:" 30': 0.26; 'second': 0.26; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'appear': 0.29; 'heading': 0.30; "i'm": 0.30; '13,': 0.31; 'file': 0.32; 'this.': 0.32; 'run': 0.32; '(most': 0.33; 'checking': 0.33; 'comment': 0.34; 'table': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; "can't": 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'like,': 0.36; "didn't": 0.36; 'two': 0.37; 'christian': 0.38; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'world.': 0.61; 'simply': 0.61; 'further': 0.61; 'back': 0.62; 'hours': 0.66; 'subject': 0.69; 'cut': 0.74; 'loses': 0.84; 'subject:round': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject How to round trip python and sqlite dates
Date Sun, 17 Nov 2013 02:16:49 +0000
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-92-24-222-204.ppp.as43234.net
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.1.0
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2752.1384654581.18130.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1384654582 news.xs4all.nl 15875 [2001:888:2000:d::a6]:53865
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:59678

Show key headers only | View raw


All the references regarding the subject that I can find, e.g. 
http://stackoverflow.com/questions/1829872/read-datetime-back-from-sqlite-as-a-datetime-in-python, 
talk about creating a table in memory using the timestamp type from the 
Python layer.  I can't see how to use that for a file on disk, so after 
a bit of RTFM I came up with this.

import sqlite3
from datetime import datetime, date

def datetime2date(datetimestr):
     return datetime.strptime(datetimestr, '%Y-%m-%d')

sqlite3.register_converter('DATETIME', datetime2date)

db = sqlite3.connect(r'C:\Users\Mark\Cash\Data\test.sqlite', 
detect_types=sqlite3.PARSE_DECLTYPES)
c = db.cursor()
c.execute('delete from temp')
row = 'DWP ESA', date(2013,11,18), 'Every two weeks', 143.4, date.max
c.execute('insert into temp values (?,?,?,?,?)', row)
c.execute('select * from temp')
row = c.fetchone()
nextdate = row[1]
print(nextdate, type(nextdate))

Run it and

Traceback (most recent call last):
   File "C:\Users\Mark\MyPython\mytest.py", line 13, in <module>
     c.execute('select * from temp')
   File "C:\Users\Mark\MyPython\mytest.py", line 7, in datetime2date
     return datetime.strptime(datetimestr, '%Y-%m-%d')
TypeError: must be str, not bytes

However if I comment out the register_converter line this output is printed

2013-11-18 <class 'str'>

Further digging in the sqlite3 file dbapi2.py I found references to 
convert_date and convert_timestamp, but putting print statements in them 
and they didn't appear to be called.

So how do I achieve the round trip that I'd like, or do I simply cut my 
loses and use strptime on the string that I can see returned?

Note that I won't be checking replies, if any, for several hours as it's 
now 02:15 GMT and I'm heading back to bed.

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

How to round trip python and sqlite dates Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-17 02:16 +0000
  Re: How to round trip python and sqlite dates "Paul Simon" <psimon@sonic.net> - 2013-11-16 20:44 -0800

csiph-web