Update step-3-proof-of-concept-connecting-to-sql-using-pyodbc.md#6081
Update step-3-proof-of-concept-connecting-to-sql-using-pyodbc.md#6081OrrinEdenfield wants to merge 1 commit intoMicrosoftDocs:livefrom
Conversation
I tried to use the INSERT code in my Python script and kept running into an issue where the record DOES get inserted into my table but I keep getting this error with pyodbc:
row = cursor.fetchone()
pyodbc.ProgrammingError: No results. Previous SQL was not a query.
In the case of this insert, I really don't expect anything to be returned - I just want to ensure the data is inserted into my table (which I've verified and can confirm). So I used a slightly different technique where I removed the cursor.fetchone() and instead have pyodbc return something that I can handle easier with my script. Using the code above I get a "0" returned which is good enough (it's not erroring anymore).
|
@OrrinEdenfield : Thanks for your contribution! The author(s) have been notified to review your proposed change. |
|
@v-makouz Can you validate the changes in the PR? Thanks! |
|
When I test this change I don't get a count of 0, but "<pyodbc.Cursor object at SOMEADDRESS>" |
David-Engel
left a comment
There was a problem hiding this comment.
It looks like the original sample probably also included a function that was trying to fetch the last PK ID that was created by the insert. But it was likely removed since that function is not reliable. We can go with your suggestion if you apply my suggestions to successfully return the inserted row count.
| count = cursor.execute(""" | ||
| INSERT INTO SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) | ||
| VALUES (?,?,?,?,?)""", | ||
| 'SQL Server Express New 20', 'SQLEXPRESS New 20', 0, 0, CURRENT_TIMESTAMP) |
There was a problem hiding this comment.
This will accomplish what you want.
| 'SQL Server Express New 20', 'SQLEXPRESS New 20', 0, 0, CURRENT_TIMESTAMP) | |
| 'SQL Server Express New 20', 'SQLEXPRESS New 20', 0, 0, CURRENT_TIMESTAMP).rowcount |
| while row: | ||
| print('Inserted Product key is ' + str(row[0])) | ||
| row = cursor.fetchone() | ||
| print(count) |
There was a problem hiding this comment.
And to clean things up a bit:
| print(count) | |
| print('Rows inserted: ' + str(count)) |
|
This change has been made via an internal PR. Thanks for the suggestion! #please-close |
I tried to use the INSERT code in my Python script and kept running into an issue where the record DOES get inserted into my table but I keep getting this error with pyodbc:
pyodbc.ProgrammingError: No results. Previous SQL was not a query.
In the case of this insert, I really don't expect anything to be returned - I just want to ensure the data is inserted into my table (which I've verified and can confirm). So I used a slightly different technique where I removed the cursor.fetchone() and instead have pyodbc return something that I can handle easier with my script. Using the code above I get a "0" returned which is good enough (it's not erroring anymore).