Skip to content

Update step-3-proof-of-concept-connecting-to-sql-using-pyodbc.md#6081

Closed
OrrinEdenfield wants to merge 1 commit intoMicrosoftDocs:livefrom
OrrinEdenfield:patch-1
Closed

Update step-3-proof-of-concept-connecting-to-sql-using-pyodbc.md#6081
OrrinEdenfield wants to merge 1 commit intoMicrosoftDocs:livefrom
OrrinEdenfield:patch-1

Conversation

@OrrinEdenfield
Copy link
Copy Markdown

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).

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).
@PRMerger7
Copy link
Copy Markdown
Contributor

@OrrinEdenfield : Thanks for your contribution! The author(s) have been notified to review your proposed change.

@David-Engel
Copy link
Copy Markdown
Contributor

@v-makouz Can you validate the changes in the PR? Thanks!

@v-makouz
Copy link
Copy Markdown
Contributor

When I test this change I don't get a count of 0, but "<pyodbc.Cursor object at SOMEADDRESS>"

Copy link
Copy Markdown
Contributor

@David-Engel David-Engel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will accomplish what you want.

Suggested change
'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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And to clean things up a bit:

Suggested change
print(count)
print('Rows inserted: ' + str(count))

@David-Engel
Copy link
Copy Markdown
Contributor

This change has been made via an internal PR. Thanks for the suggestion!

#please-close

@PRMerger6 PRMerger6 closed this Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants