Knowledgebase
[QODBC-ALL] Troubleshooting - Getting Error [HY004] [Microsoft][ODBC Driver Manager] SQL data type out of range (0) (SQLBindParameter) when running Python script
Posted by Jack - QODBC Support on 28 June 2017 02:54 PM
|
|
Troubleshooting - Getting Error [HY004] [Microsoft][ODBC Driver Manager] SQL data type out of range (0) (SQLBindParameter) when running Python scriptProblem Description:I am using Python script to access QuickBooks Data. I can select QuickBooks Data using Python without any issues. However, I can successfully insert data into QuickBooks using Python with static values. But, I am getting the below error when I use parameterized insert statement. See the code below: [HY004] [Microsoft][ODBC Driver Manager] SQL data type out of range (0) (SQLBindParameter) import pyodbc stmt = pyodbc.connect("DSN=QuickBooks Data QRemote", autocommit=True) CustomerRefListID = input("CustomerRefListID: ") ClassRefFullName = input("ClassRefFullName: ") TemplateRefListID = input("TemplateRefListID: ") RefNumber = input("RefNumber: ") CreditMemoLineItemRefListID = input("CreditMemoLineItemRefListID: ") CreditMemoLineDesc = input("CreditMemoLineDesc: ") Rate = input("CreditMemoLineRate: ") Quantity = input("CreditMemoLineQuantity: ") Amount = input("CreditMemoLineAmount: ") P1 = CustomerRefListID P2 = ClassRefFullName P3 = TemplateRefListID P4 = RefNumber P5 = CreditMemoLineItemRefListID P6 = CreditMemoLineDesc P7= Rate P8= Quantity P9= Amount cursor = stmt.cursor() sql = "INSERT INTO CreditMemoLine (CustomerRefListID,ClassRefFullName, TemplateRefListID, RefNumber, CreditMemoLineItemRefListID, CreditMemoLineDesc, CreditMemoLineRate,CreditMemoLineQuantity, CreditMemoLineAmount) values (?,?,?,?,?,?,?,?,?)" cursor.execute(sql,(P1),(P2),(P3),(P4),(P5),(P6),(P7),(P8),(P9)) cursor.close() stmt.close()
Solution:For the parameterized query, you may have to apply an encoding to the variable values. Please refer to the sample script. import pyodbc from decimal import * stmt = pyodbc.connect("DSN=QuickBooks Data QRemote", autocommit=True) CustomerRefListID = input("CustomerRefListID: ") ClassRefFullName = input("ClassRefFullName: ") TemplateRefListID = input("TemplateRefListID: ") RefNumber = input("RefNumber: ") CreditMemoLineItemRefListID = input("CreditMemoLineItemRefListID: ") CreditMemoLineDesc = input("CreditMemoLineDesc: ") Rate = input("CreditMemoLineRate: ") Quantity = input("CreditMemoLineQuantity: ") Amount = input("CreditMemoLineAmount: ") encoding = 'utf-8' P1 = CustomerRefListID.encode(encoding) P2 = ClassRefFullName.encode(encoding) P3 = TemplateRefListID.encode(encoding) P4 = RefNumber.encode(encoding) P5 = CreditMemoLineItemRefListID.encode(encoding) P6 = CreditMemoLineDesc.encode(encoding) P7= Decimal(Rate) P8= Decimal(Quantity) P9= Decimal(Amount) cursor = stmt.cursor() sql = "INSERT INTO CreditMemoLine (CustomerRefListID,ClassRefFullName, TemplateRefListID, RefNumber, CreditMemoLineItemRefListID, CreditMemoLineDesc, CreditMemoLineRate,CreditMemoLineQuantity, CreditMemoLineAmount) values (?,?,?,?,?,?,?,?,?)" cursor.execute(sql,(P1),(P2),(P3),(P4),(P5),(P6),(P7),(P8),(P9)) cursor.close() stmt.close()
After changing the script, you can run the parameterized script without any issues.
CreditMemo was created in QuickBooks.
| |
|
Comments (0)