Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation that pymssql requires an explicit commit #68

Open
ncryer opened this issue Sep 23, 2022 · 4 comments
Open

Add documentation that pymssql requires an explicit commit #68

ncryer opened this issue Sep 23, 2022 · 4 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@ncryer
Copy link

ncryer commented Sep 23, 2022

I'm testing this with pymssql against a MSSQL instance. Following the examples given by the documentation you'd assume this was the way forward

from pydapper import connect
import os
from dataclasses import dataclass 

@dataclass
class Table:
    Name: str
    Description: str

test_model = Table("Test", "Test")

conn_string = os.getenv("connection_string")

with connect(conn_string) as commands:
    rows = commands.execute("INSERT INTO dbo.Table (Name, Description) VALUES (?Name?, ?Description?)", param=test_model.__dict__)
    assert rows == 1 # This is true

rows will always be 1 here, but the results are never committed to the database. If, however, you do:

with connect(conn_string) as commands:
    rows = commands.execute("INSERT INTO dbo.Table (Name, Description) VALUES (?Name?, ?Description?)", param=test_model.__dict__)
    assert rows == 1 # This is true
    commands.connection.commit()

Now the item will be written to the database.

If memory serves, I've seen the first approach work with SQLite in some earlier testing I did, so I'm not sure what the way forwards is:

Either this should be added to Commands.execute like so:

    def execute(self, sql: str, param: Union["ParamType", "ListParamType"] = None) -> int:
        handler = self.SqlParamHandler(sql, param)
        with self.cursor() as cursor:
            rowcount = handler.execute(cursor)
        self.connection.commit()
        return rowcount

Or the documentation should be updated to reflect the necessity of committing yourself. I'll happily submit a PR either way, but I'd be interested in your opinion on this first.

@zschumacher
Copy link
Owner

Thanks for the issue! I’ll look into this and get back to you.

@zschumacher
Copy link
Owner

Looking at the pymssql docs, you do in fact have to call commit explicitly. I can add a blurb in the docs similar to what I have for pymysql (or if you want to PR that would be awesome 😄 ).

Thanks for finding this.

@ncryer
Copy link
Author

ncryer commented Sep 26, 2022

Awesome, I'll make a PR :)

@ncryer ncryer closed this as completed Sep 26, 2022
@ncryer ncryer reopened this Sep 26, 2022
@zschumacher zschumacher changed the title Commands requiring cursor.commit() are never sent to the database Add documentation that pymssql requires an explicit commit Feb 12, 2023
@zschumacher
Copy link
Owner

closed the PR as there hasn't been any activity, I can take this one when I have a moment

@zschumacher zschumacher self-assigned this Feb 12, 2023
@zschumacher zschumacher added the documentation Improvements or additions to documentation label Feb 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants