Home > Software engineering >  open .db-file from python script returns empty file
open .db-file from python script returns empty file

Time:08-13

I want to open a .db-file from python to inspect it. I can open it from a terminal,

    % sqlite3 scrapy_quotes.db
    sqlite> .tables
    author     quote      quote_tag  tag      
    sqlite> select * from quote limit 3
    ...

but from a python-script it only shows an empty file,

    from sqlalchemy import create_engine, inspect
    engine = create_engine('sqlite:///scrapy_quotes.db')
    insp = inspect(engine)
    print(insp.get_table_names())
    []

while this script does work for another .db-file.

    from sqlalchemy import create_engine, inspect
    engine = create_engine('sqlite:///chinook.db')
    insp = inspect(engine)
    print(insp.get_table_names())
    ['albums', 'artists', 'customers', 'employees', 'genres',                 
    'invoice_items', 'invoices', 'media_types', 
    'playlist_track', 'playlists', 'sqlite_sequence', 
    'sqlite_stat1', 'tracks']

What is going on?

CodePudding user response:

I cannot reproduce your error, are you sure the paths are right ?

import sqlite3

con = sqlite3.connect("/tmp/test.db")
con.execute("CREATE TABLE test (id INT)")
con.commit()
con.close()
from sqlalchemy import create_engine, inspect

engine = create_engine("sqlite:////tmp/test.db")
inspect(engine).get_table_names()  # gives ['test']
% sqlite3 /tmp/test.db
SQLite version 3.32.3 2020-06-18 14:16:19
Enter ".help" for usage hints.
sqlite> .tables
test
sqlite> CREATE TABLE reverse (id INT);
from sqlalchemy import create_engine, inspect

engine = create_engine("sqlite:////tmp/test.db")
inspect(engine).get_table_names()  # gives ['reverse', 'test']
  • Related