Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit ff54d4c

Browse files
committed
execute() returns rows affected by operation if the lastrowid is not set.
1 parent e44fe3b commit ff54d4c

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

databases/backends/mysql.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ async def execute(self, query: ClauseElement) -> typing.Any:
131131
cursor = await self._connection.cursor()
132132
try:
133133
await cursor.execute(query, args)
134-
return cursor.rowcount
134+
if cursor.lastrowid == 0:
135+
return cursor.rowcount
136+
return cursor.lastrowid
135137
finally:
136138
await cursor.close()
137139

databases/backends/sqlite.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,14 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[typing.Mappin
111111
async def execute(self, query: ClauseElement) -> typing.Any:
112112
assert self._connection is not None, "Connection is not acquired"
113113
query, args, context = self._compile(query)
114-
cursor = await self._connection.execute(query, args)
115-
rowcount = cursor.rowcount
116-
await cursor.close()
117-
return rowcount
114+
cursor = await self._connection.cursor()
115+
try:
116+
await cursor.execute(query, args)
117+
if cursor.lastrowid == 0:
118+
return cursor.rowcount
119+
return cursor.lastrowid
120+
finally:
121+
await cursor.close()
118122

119123
async def execute_many(self, queries: typing.List[ClauseElement]) -> None:
120124
assert self._connection is not None, "Connection is not acquired"

0 commit comments

Comments
 (0)