waliwali / ibm-db

Automatically exported from code.google.com/p/ibm-db
0 stars 0 forks source link

Cannot use Python variables with ibm_db.bind_param #17

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
-What steps will reproduce the problem?
I used the sample code (attached to this issue)

--- BEGIN CODE ---
import os
import ibm_db
import datetime

query = """
SELECT
    CAL_DATE,
    CAL_YEAR
FROM
    DAR.TD_CALENDAR
WHERE
    CAL_DATE > ?
FETCH FIRST 5 ROWS ONLY;
"""

creds_file = os.path.join(os.getenv("HOME"), '.db2w.credentials')
user, passwd = open(creds_file).read().splitlines()[:2]
conn = ibm_db.connect('db2w', user, passwd)
stmt = ibm_db.prepare(conn, query)
how_far = datetime.datetime.now() - datetime.timedelta(hours=200)
# Notice how the parameter is a native Python datetime.date type
print ibm_db.bind_param(stmt, 1, how_far.date(), ibm_db.SQL_PARAM_INPUT,
ibm_db.SQL_TYPE_DATE)
print ibm_db.execute(stmt)
row = ibm_db.fetch_tuple(stmt)
while row:
    print row
    row = ibm_db.fetch_tuple(stmt)
--- END CODE ---

What is the expected output? What do you see instead?
I expect to have a working query instead I get the following exception:
True
Traceback (most recent call last):
  File "test_query.py", line 23, in <module>
    print ibm_db.execute(stmt)
Exception: Binding Error 3:

The API doc says this for ibm_db.bind_param:
----
ibm_db.bind_param

    Py_True/Py_None ibm_db.bind_param resource stmt, int parameter-number,
        string variable [, int parameter-type [, int data-type [, int   
        precision [, int scale [, int size[]]]]]] ) 

variable

    A Python variable to bind to the parameter specified by parameter-number. 
----
So I expect to be able to use a datetime.date to bind to the query. If I
convert to a string, it works (except while debugging, I will open another
issue for this).

What version of the product are you using? On what operating system?
Python 2.5, ibm_db 0.3.0, db2level returns:
DB21085I  Instance "user" uses "32" bits and DB2 code release "SQL08028" 
with level identifier "03090106".
Informational tokens are "DB2 v8.1.2.136", "s070720", "MI00189", and FixPak 
"15".
Product is installed at "/opt/IBM/db2/V8.1".

Original issue reported on code.google.com by fred.dub...@gmail.com on 30 Sep 2008 at 8:52

Attachments:

GoogleCodeExporter commented 9 years ago
Forgot to say that I'm on Ubuntu Hardy Heron 8.04.1.

Original comment by fred.dub...@gmail.com on 30 Sep 2008 at 9:07

GoogleCodeExporter commented 9 years ago
Hi Fred,
      Please use String for date. Python date format is not yet supported.

Thanks,
Abhigyan

Original comment by abhigyan...@in.ibm.com on 6 Oct 2008 at 12:09

GoogleCodeExporter commented 9 years ago

Original comment by abhigyan...@in.ibm.com on 7 Oct 2008 at 6:55