diff --git a/aioodbc/cursor.py b/aioodbc/cursor.py index b525dea..9ab82b9 100644 --- a/aioodbc/cursor.py +++ b/aioodbc/cursor.py @@ -163,6 +163,16 @@ def fetchone(self): fut = self._run_operation(self._impl.fetchone) return fut + def fetchval(self): + """Returns the first column of the first row if there are results. + + A ProgrammingError exception is raised if no SQL has been executed + or if it did not return a result set (e.g. was not a SELECT + statement). + """ + fut = self._run_operation(self._impl.fetchval) + return fut + def fetchall(self): """Returns a list of all remaining rows. diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 9b537f5..9e2535d 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -172,6 +172,18 @@ async def test_fetchone(conn, table): await cur.close() +@pytest.mark.parametrize("db", pytest.db_list) +@pytest.mark.asyncio +async def test_fetchval(conn, table): + cur = await conn.cursor() + await cur.execute("SELECT * FROM t1;") + resp = await cur.fetchval() + expected = 1 + + assert expected == resp + await cur.close() + + @pytest.mark.parametrize("db", ["sqlite"]) @pytest.mark.asyncio async def test_tables(conn, table):