{"html_url": "https://github.com/simonw/sqlite-utils/issues/290#issuecomment-868021624", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/290", "id": 868021624, "node_id": "MDEyOklzc3VlQ29tbWVudDg2ODAyMTYyNA==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-06-24T23:17:38Z", "updated_at": "2021-06-24T23:17:38Z", "author_association": "OWNER", "body": "The new documentation: https://sqlite-utils.datasette.io/en/latest/python-api.html#executing-queries", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 926777310, "label": "`db.query()` method (renamed `db.execute_returning_dicts()`)"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/290#issuecomment-865511810", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/290", "id": 865511810, "node_id": "MDEyOklzc3VlQ29tbWVudDg2NTUxMTgxMA==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-06-22T04:07:34Z", "updated_at": "2021-06-22T18:26:21Z", "author_association": "OWNER", "body": "That documentation section is pretty weak at the moment - here's the whole thing:\r\n\r\n> ### Executing queries\r\n>\r\n> The `db.execute()` and `db.executescript()` methods provide wrappers around `.execute()` and `.executescript()` on the underlying SQLite connection. These wrappers log to the tracer function if one has been registered.\r\n> ```python\r\n> db = Database(memory=True)\r\n> db[\"dogs\"].insert({\"name\": \"Cleo\"})\r\n> db.execute(\"update dogs set name = 'Cleopaws'\")\r\n> ```\r\n> You can pass parameters as an optional second argument, using either a list or a dictionary. These will be correctly quoted and escaped.\r\n> ```python\r\n> # Using ? and a list:\r\n> db.execute(\"update dogs set name = ?\", [\"Cleopaws\"])\r\n> # Or using :name and a dictionary:\r\n> db.execute(\"update dogs set name = :name\", {\"name\": \"Cleopaws\"})\r\n> ```\r\n\r\n- Talks about `.execute()` - I want to talk about `.query()` instead\r\n- Doesn't clarify that `.execute()` returns a `Cursor` - and assumes you know what to do with one\r\n- Doesn't show an example of a `select` query at all\r\n- The \"tracer function\" bit is confusing (should at least link to docs further down)\r\n- For `UPDATE` should show how to access the number of rows modified (probably using `.execute()` there)\r\n\r\nIt does at least cover the two types of parameters, though that could be bulked out.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 926777310, "label": "`db.query()` method (renamed `db.execute_returning_dicts()`)"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/290#issuecomment-865510796", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/290", "id": 865510796, "node_id": "MDEyOklzc3VlQ29tbWVudDg2NTUxMDc5Ng==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-06-22T04:04:40Z", "updated_at": "2021-06-22T04:04:48Z", "author_association": "OWNER", "body": "Still needs documentation, which will involve rewriting the whole [Executing queries](https://sqlite-utils.datasette.io/en/3.11/python-api.html#executing-queries) section.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 926777310, "label": "`db.query()` method (renamed `db.execute_returning_dicts()`)"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/290#issuecomment-865497846", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/290", "id": 865497846, "node_id": "MDEyOklzc3VlQ29tbWVudDg2NTQ5Nzg0Ng==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-06-22T03:21:38Z", "updated_at": "2021-06-22T03:21:38Z", "author_association": "OWNER", "body": "The Python docs say: https://docs.python.org/3/library/sqlite3.html\r\n\r\n> To retrieve data after executing a SELECT statement, you can either treat the cursor as an iterator, call the cursor\u2019s `fetchone()` method to retrieve a single matching row, or call `fetchall()` to get a list of the matching rows.\r\n\r\nLooking at the C source code, both `fetchmany()` and `fetchall()` work under the hood by assembling a Python list: https://github.com/python/cpython/blob/be1cb3214d09d4bf0288bc45f3c1f167f67e4514/Modules/_sqlite/cursor.c#L907-L972 - see calls to `PyList_Append()`\r\n\r\nSo it looks like the most efficient way to iterate over a cursor may well be `for row in cursor:` - which I think calls this C function: https://github.com/python/cpython/blob/be1cb3214d09d4bf0288bc45f3c1f167f67e4514/Modules/_sqlite/cursor.c#L813-L876", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 926777310, "label": "`db.query()` method (renamed `db.execute_returning_dicts()`)"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/290#issuecomment-865495370", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/290", "id": 865495370, "node_id": "MDEyOklzc3VlQ29tbWVudDg2NTQ5NTM3MA==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-06-22T03:14:30Z", "updated_at": "2021-06-22T03:14:30Z", "author_association": "OWNER", "body": "One small problem with the existing method:\r\nhttps://github.com/simonw/sqlite-utils/blob/8cedc6a8b29180e68326f6b76f249d5e39e4b591/sqlite_utils/db.py#L362-L365\r\n\r\nIt returns a full list, but what if the user would rather have a generator they can iterate over without loading the results into memory in one go?", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 926777310, "label": "`db.query()` method (renamed `db.execute_returning_dicts()`)"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/290#issuecomment-865491922", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/290", "id": 865491922, "node_id": "MDEyOklzc3VlQ29tbWVudDg2NTQ5MTkyMg==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-06-22T03:05:35Z", "updated_at": "2021-06-22T03:05:35Z", "author_association": "OWNER", "body": "Potential names:\r\n\r\n- `db.query(sql)` - it's weird to have both this and `db.execute()` but it is at least short and memorable\r\n- `db.sql(sql)`\r\n- `db.execute_d(sql)` - ugly\r\n- `db.execute_dicts(sql)` - confusing\r\n- `db.execute_sql(sql)` - easily confused with `db.execute(sql)`\r\n\r\nI think `db.query(sql)` may be the best option here.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 926777310, "label": "`db.query()` method (renamed `db.execute_returning_dicts()`)"}, "performed_via_github_app": null}