summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Di Gregorio <fog@initd.org>2005-06-02 06:56:21 +0000
committerFederico Di Gregorio <fog@initd.org>2005-06-02 06:56:21 +0000
commit7ead773fc071a4d1879e919f9a679c99c8c8cf3b (patch)
tree421c32be55b48abbd4585ce21d8db04d47072dfb
parentb300cd2550399a676d7e639c7adcd1f82632cfb9 (diff)
downloadpsycopg2-7ead773fc071a4d1879e919f9a679c99c8c8cf3b.tar.gz
TimestampFromTicks fix.
-rw-r--r--ChangeLog9
-rw-r--r--examples/fetch.py71
-rw-r--r--psycopg/adapter_datetime.c4
3 files changed, 82 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 3da6e54..1c8d956 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,15 @@
* psycopg/cursor_type.c (_psyco_curs_execute): fixed segfault when
not passing string or unicode to .execute().
+2005-06-01 Federico Di Gregorio <fog@debian.org>
+
+ * examples/fetch.py: added example about using DECLARE CURSOR.
+
+ * psycopg/adapter_datetime.c (psyco_TimestampFromTicks): "Hmmm,
+ looks like someone forgot that C expects months to start counting
+ from 0, but the Python date routines start counting from 1." That
+ was me: fixed.
+
2005-05-31 Federico Di Gregorio <fog@debian.org>
* psycopg/cursor_type.c (_psyco_curs_execute): if a
diff --git a/examples/fetch.py b/examples/fetch.py
new file mode 100644
index 0000000..e0a5728
--- /dev/null
+++ b/examples/fetch.py
@@ -0,0 +1,71 @@
+# fetch.py -- example about declaring cursors
+#
+# Copyright (C) 2001-2005 Federico Di Gregorio <fog@debian.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
+# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+# for more details.
+#
+
+## put in DSN your DSN string
+
+DSN = 'dbname=test'
+
+## don't modify anything below tis line (except for experimenting)
+
+import sys
+import psycopg
+
+if len(sys.argv) > 1:
+ DSN = sys.argv[1]
+
+print "Opening connection using dns:", DSN
+conn = psycopg.connect(DSN)
+print "Encoding for this connection is", conn.encoding
+
+curs = conn.cursor()
+try:
+ curs.execute("CREATE TABLE test_fetch (val int4)")
+except:
+ conn.rollback()
+ curs.execute("DROP TABLE test_fetch")
+ curs.execute("CREATE TABLE test_fetch (val int4)")
+conn.commit()
+
+# we use this function to format the output
+
+def flatten(l):
+ """Flattens list of tuples l."""
+ return map(lambda x: x[0], l)
+
+# insert 20 rows in the table
+
+for i in range(20):
+ curs.execute("INSERT INTO test_fetch VALUES(%s)", (i,))
+conn.commit()
+
+# does some nice tricks with the transaction and postgres cursors
+# (remember to always commit or rollback before a DECLARE)
+
+curs.execute("DECLARE crs CURSOR FOR SELECT * FROM test_fetch")
+curs.execute("FETCH 10 FROM crs")
+print "First 10 rows:", flatten(curs.fetchall())
+curs.execute("MOVE -5 FROM crs")
+print "Moved back cursor by 5 rows (to row 5.)"
+curs.execute("FETCH 10 FROM crs")
+print "Another 10 rows:", flatten(curs.fetchall())
+curs.execute("FETCH 10 FROM crs")
+print "The remaining rows:", flatten(curs.fetchall())
+
+# rollback to close the transaction
+
+conn.rollback()
+
+curs.execute("DROP TABLE test_fetch")
+conn.commit()
diff --git a/psycopg/adapter_datetime.c b/psycopg/adapter_datetime.c
index ffff6f5..656e14f 100644
--- a/psycopg/adapter_datetime.c
+++ b/psycopg/adapter_datetime.c
@@ -338,7 +338,7 @@ psyco_DateFromTicks(PyObject *self, PyObject *args)
t = (time_t)round(ticks);
if (gmtime_r(&t, &tm)) {
- args = Py_BuildValue("iii", tm.tm_year, tm.tm_mon, tm.tm_mday);
+ args = Py_BuildValue("iii", tm.tm_year, tm.tm_mon+1, tm.tm_mday);
if (args) {
res = psyco_Date(self, args);
Py_DECREF(args);
@@ -383,7 +383,7 @@ psyco_TimestampFromTicks(PyObject *self, PyObject *args)
t = (time_t)round(ticks);
if (gmtime_r(&t, &tm)) {
args = Py_BuildValue("iiiiid",
- tm.tm_year, tm.tm_mon, tm.tm_mday,
+ tm.tm_year, tm.tm_mon+1, tm.tm_mday,
tm.tm_hour, tm.tm_min, (double)tm.tm_sec);
if (args) {
res = psyco_Timestamp(self, args);