databases icon indicating copy to clipboard operation
databases copied to clipboard

new 0.8 version for sqlite connection with '?mode=ro' not working, Error can't open database

Open Pythonbeginer2014 opened this issue 1 year ago • 2 comments

from contextlib import asynccontextmanager
from databases import Database


@asynccontextmanager
async def async_sqlite_conn(db_name):
    database = Database(f'sqlite+aiosqlite:///example.db?mode=ro', uri=True)
    try:
        await database.connect()
        yield database
    finally:
        await database.disconnect()

when call this function and query data, get error " can't open database"

Thanks ,

Pythonbeginer2014 avatar Mar 01 '24 16:03 Pythonbeginer2014

Hi @Pythonbeginer2014,

I tried to reproduce your problem using python 3.12 with databases 0.8 and for me it works perfectly: image

Try again but make sure you have write/read access to directory where you want to create file with database, remove file (it might be somehow corrupted)

If still this won't work can you please show us your exception logs, and pip freeze

Sebcio03 avatar Nov 20 '24 23:11 Sebcio03

thanks for your reply, here is the test code, sorry for codes above has some typo

database = Database('sqlite+aiosqlite:///library.db?mode=ro', uri=True)
 await database.connect()

  query = "SELECT * FROM book"
  rows = await database.fetch_all(query=query)
  print('books:', rows)

the exception errors:

File "P:\envs\pgwlkpy312\Lib\site-packages\aiosqlite\core.py", line 382, in connector return sqlite3.connect(loc, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sqlite3.OperationalError: unable to open database file

Pythonbeginer2014 avatar Apr 08 '25 22:04 Pythonbeginer2014