2004-10-19 07:17:12 +04:00
|
|
|
# datetime.py - example of using date and time types
|
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# Copyright (C) 2001-2010 Federico Di Gregorio <fog@debian.org>
|
2004-10-19 07:17:12 +04:00
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2004-10-19 07:17:12 +04:00
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
# License for more details.
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
## put in DSN your DSN string
|
|
|
|
|
|
|
|
DSN = 'dbname=test'
|
|
|
|
|
2011-12-16 03:58:22 +04:00
|
|
|
## don't modify anything below this line (except for experimenting)
|
2004-10-19 07:17:12 +04:00
|
|
|
|
2005-10-18 18:44:57 +04:00
|
|
|
import sys
|
|
|
|
import psycopg2
|
2004-10-19 07:17:12 +04:00
|
|
|
import mx.DateTime
|
|
|
|
import datetime
|
|
|
|
|
2005-11-16 20:30:45 +03:00
|
|
|
from psycopg2.extensions import adapt
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
DSN = sys.argv[1]
|
|
|
|
|
2017-12-04 05:47:19 +03:00
|
|
|
print("Opening connection using dsn:", DSN)
|
2005-06-13 10:25:06 +04:00
|
|
|
conn = psycopg2.connect(DSN)
|
2004-10-19 07:17:12 +04:00
|
|
|
curs = conn.cursor()
|
|
|
|
|
|
|
|
try:
|
2005-10-18 18:44:57 +04:00
|
|
|
curs.execute("""CREATE TABLE test_dt (
|
|
|
|
k int4, d date, t time, dt timestamp, z interval)""")
|
2004-10-19 07:17:12 +04:00
|
|
|
except:
|
|
|
|
conn.rollback()
|
|
|
|
curs.execute("DROP TABLE test_dt")
|
2005-10-18 18:44:57 +04:00
|
|
|
curs.execute("""CREATE TABLE test_dt (
|
|
|
|
k int4, d date, t time, dt timestamp, z interval)""")
|
2004-10-19 07:17:12 +04:00
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
# build and insert some data using mx.DateTime
|
|
|
|
mx1 = (
|
|
|
|
1,
|
|
|
|
mx.DateTime.Date(2004, 10, 19),
|
|
|
|
mx.DateTime.Time(0, 11, 17.015),
|
|
|
|
mx.DateTime.Timestamp(2004, 10, 19, 0, 11, 17.5),
|
|
|
|
mx.DateTime.DateTimeDelta(13, 15, 17, 59.9))
|
|
|
|
|
2005-10-18 18:44:57 +04:00
|
|
|
from psycopg2.extensions import adapt
|
|
|
|
import psycopg2.extras
|
2017-12-04 05:47:19 +03:00
|
|
|
print(adapt(mx1))
|
2005-10-18 18:44:57 +04:00
|
|
|
|
2017-12-04 05:47:19 +03:00
|
|
|
print("Inserting mx.DateTime values...")
|
2004-10-19 07:17:12 +04:00
|
|
|
curs.execute("INSERT INTO test_dt VALUES (%s, %s, %s, %s, %s)", mx1)
|
|
|
|
|
|
|
|
# build and insert some values using the datetime adapters
|
|
|
|
dt1 = (
|
|
|
|
2,
|
|
|
|
datetime.date(2004, 10, 19),
|
|
|
|
datetime.time(0, 11, 17, 15000),
|
|
|
|
datetime.datetime(2004, 10, 19, 0, 11, 17, 500000),
|
|
|
|
datetime.timedelta(13, 15*3600+17*60+59, 900000))
|
|
|
|
|
2017-12-04 05:47:19 +03:00
|
|
|
print("Inserting Python datetime values...")
|
2004-10-19 07:17:12 +04:00
|
|
|
curs.execute("INSERT INTO test_dt VALUES (%s, %s, %s, %s, %s)", dt1)
|
|
|
|
|
|
|
|
# now extract the row from database and print them
|
2017-12-04 05:47:19 +03:00
|
|
|
print("Extracting values inserted with mx.DateTime wrappers:")
|
2004-10-19 07:17:12 +04:00
|
|
|
curs.execute("SELECT d, t, dt, z FROM test_dt WHERE k = 1")
|
|
|
|
for n, x in zip(mx1[1:], curs.fetchone()):
|
|
|
|
try:
|
2011-12-16 03:58:22 +04:00
|
|
|
# this will work only if psycopg has been compiled with datetime
|
2004-10-19 07:17:12 +04:00
|
|
|
# as the default typecaster for date/time values
|
2005-11-16 20:30:45 +03:00
|
|
|
s = repr(n) + "\n -> " + str(adapt(n)) + \
|
|
|
|
"\n -> " + repr(x) + "\n -> " + x.isoformat()
|
2004-10-19 07:17:12 +04:00
|
|
|
except:
|
2005-11-16 20:30:45 +03:00
|
|
|
s = repr(n) + "\n -> " + str(adapt(n)) + \
|
|
|
|
"\n -> " + repr(x) + "\n -> " + str(x)
|
2017-12-04 05:47:19 +03:00
|
|
|
print(s)
|
|
|
|
print()
|
2004-10-19 07:17:12 +04:00
|
|
|
|
2017-12-04 05:47:19 +03:00
|
|
|
print("Extracting values inserted with Python datetime wrappers:")
|
2004-10-19 07:17:12 +04:00
|
|
|
curs.execute("SELECT d, t, dt, z FROM test_dt WHERE k = 2")
|
|
|
|
for n, x in zip(dt1[1:], curs.fetchone()):
|
|
|
|
try:
|
2011-12-16 03:58:22 +04:00
|
|
|
# this will work only if psycopg has been compiled with datetime
|
2004-10-19 07:17:12 +04:00
|
|
|
# as the default typecaster for date/time values
|
|
|
|
s = repr(n) + "\n -> " + repr(x) + "\n -> " + x.isoformat()
|
|
|
|
except:
|
|
|
|
s = repr(n) + "\n -> " + repr(x) + "\n -> " + str(x)
|
2017-12-04 05:47:19 +03:00
|
|
|
print(s)
|
|
|
|
print()
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
curs.execute("DROP TABLE test_dt")
|
|
|
|
conn.commit()
|