datasette / datasette-write-ui

A Datasette plugin that adds UI elements to edit, insert, or delete rows in SQLite tables
Apache License 2.0
15 stars 2 forks source link

Bug with tables with weird names - `123_starts_with_digits` #27

Open simonw opened 4 months ago

simonw commented 4 months ago

Spotted this while trying out this plugin against https://latest.datasette.io/fixtures (download as https://latest.datasette.io/fixtures.db)

ERROR: conn=<sqlite3.Connection object at 0x105867a40>, sql = 'select content from 123_starts_with_digits where rowid = ?', params = ['1']: unrecognized token: "123_starts_with_digits"
Traceback (most recent call last):
  File "/private/tmp/datasette-venv/lib/python3.10/site-packages/datasette/app.py", line 1668, in route_path
    response = await view(request, send)
  File "/private/tmp/datasette-venv/lib/python3.10/site-packages/datasette/app.py", line 1861, in async_view_fn
    response = await async_call_with_supported_arguments(
  File "/private/tmp/datasette-venv/lib/python3.10/site-packages/datasette/utils/__init__.py", line 1020, in async_call_with_supported_arguments
    return await fn(*call_with)
  File "/private/tmp/datasette-venv/lib/python3.10/site-packages/datasette_write_ui/__init__.py", line 105, in edit_row_details
    results = await db.execute(
  File "/private/tmp/datasette-venv/lib/python3.10/site-packages/datasette/database.py", line 336, in execute
    results = await self.execute_fn(sql_operation_in_thread)
  File "/private/tmp/datasette-venv/lib/python3.10/site-packages/datasette/database.py", line 282, in execute_fn
    return await asyncio.get_event_loop().run_in_executor(
  File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/private/tmp/datasette-venv/lib/python3.10/site-packages/datasette/database.py", line 280, in in_thread
    return fn(conn)
  File "/private/tmp/datasette-venv/lib/python3.10/site-packages/datasette/database.py", line 306, in sql_operation_in_thread
    cursor.execute(sql, params if params is not None else {})
sqlite3.OperationalError: unrecognized token: "123_starts_with_digits"
simonw commented 4 months ago

Relevant code: https://github.com/datasette/datasette-write-ui/blob/b8a461fd0a06b5ac47b14b5ff61d682753315593/datasette_write_ui/__init__.py#L103-L108

Needs to be select {column_list} from "{table_name}" or f"select {column_list} from [{table_name}]