简体   繁体   中英

Cannot use parameter with pyodbc when dealing with 4D ODBC Server

I cannot get parametrized query working with pyodbc and 4D. Here an example of simple and basic code (working on mySQL database). The table B has two columns without any constraint.

import pyodbc

con2 = pyodbc.connect('dsn=odbc_source');
cur2 = con2.cursor();

KeyMap = {
    1: ('Aaaahaa',datetime.date(1990,1,1)),
    2: ('Bcdefgh',datetime.date(1990,2,1)),
    3: ("Aaaah'aa",datetime.date(1990,3,1))
    };

for k in KeyMap:
    v = (k,KeyMap[k][0]);
    cur2.execute("INSERT INTO B(Num,Text) VALUES(?,?);",v);

I always get this error message:

Traceback (most recent call last):
File "D:\jlandercy\src\python\cripi\transfert\000_TestInsertParam.py", line 33, in <module>
cur2.execute("INSERT INTO B(Num,Text) VALUES(?,?);",v);
pyodbc.ProgrammingError: ('The SQL contains 1 parameter markers, but 2 parameters were supplied', 'HY000')

Or, that the connection failed, but if I issue a SELECT it works. What am I doing wrong.

Have you tried enumerating the mapping instead of making the () object?

for k in KeyMap:
    cur2.execute("INSERT INTO B(Num,Text) VALUES(?,?);",k,KeyMap[k][0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM