Skip to content

pd.read_sql_query doesn't find existing table(s) #22206

@ghost

Description

Code Sample

from tqdm import tqdm
import sqlite3
import pandas as pd

def fins(tickers, dbname):
    
    db = sqlite3.connect(f'dbname')
    
    dataset = []
    
    tables = ['_balance_sheet', '_cash_flow', '_income_statement']
    
    for i in tqdm(tickers): 
        for j in tqdm(tables):
            SQL = f'SELECT * FROM {i}{j}'
            print(SQL)
            pd.read_sql_query(SQL, db)

# executing the function throws an error
fins(tickers, 'financials.db')

Problem Description

SQL tables are not recognized in nested for loop. Similar to #9262. Query works fine when passed directly in the function. The code snippet is not reproducible as I'm working on my own database

Expected Output

A list of datasets for each ticker selected, like shown below.

screen shot 2018-08-05 at 20 39 17

Error Message

A DatabaseError is thrown, although the dataset exists, as shown in the picture above.

---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
~/.local/share/virtualenvs/pynance-C6oRUGpU/lib/python3.7/site-packages/pandas/io/sql.py in execute(self, *args, **kwargs)
   1377             else:
-> 1378                 cur.execute(*args)
   1379             return cur

OperationalError: no such table: NLY_balance_sheet

During handling of the above exception, another exception occurred:

DatabaseError                             Traceback (most recent call last)
<ipython-input-34-1ccd252d2593> in <module>()
----> 1 fins(tickers, 'financials.db')

<ipython-input-33-22a9339f800f> in fins(tickers, dbname)
     11             SQL = f'SELECT * FROM {i}{j}'
     12             print(SQL)
---> 13             pd.read_sql_query(SQL, db)

~/.local/share/virtualenvs/pynance-C6oRUGpU/lib/python3.7/site-packages/pandas/io/sql.py in read_sql_query(sql, con, index_col, coerce_float, params, parse_dates, chunksize)
    312     return pandas_sql.read_query(
    313         sql, index_col=index_col, params=params, coerce_float=coerce_float,
--> 314         parse_dates=parse_dates, chunksize=chunksize)
    315 
    316 

~/.local/share/virtualenvs/pynance-C6oRUGpU/lib/python3.7/site-packages/pandas/io/sql.py in read_query(self, sql, index_col, coerce_float, params, parse_dates, chunksize)
   1411 
   1412         args = _convert_params(sql, params)
-> 1413         cursor = self.execute(*args)
   1414         columns = [col_desc[0] for col_desc in cursor.description]
   1415 

~/.local/share/virtualenvs/pynance-C6oRUGpU/lib/python3.7/site-packages/pandas/io/sql.py in execute(self, *args, **kwargs)
   1388             ex = DatabaseError(
   1389                 "Execution failed on sql '%s': %s" % (args[0], exc))
-> 1390             raise_with_traceback(ex)
   1391 
   1392     @staticmethod

~/.local/share/virtualenvs/pynance-C6oRUGpU/lib/python3.7/site-packages/pandas/compat/__init__.py in raise_with_traceback(exc, traceback)
    402         if traceback == Ellipsis:
    403             _, _, traceback = sys.exc_info()
--> 404         raise exc.with_traceback(traceback)
    405 else:
    406     # this version of raise is a syntax error in Python 3

~/.local/share/virtualenvs/pynance-C6oRUGpU/lib/python3.7/site-packages/pandas/io/sql.py in execute(self, *args, **kwargs)
   1376                 cur.execute(*args, **kwargs)
   1377             else:
-> 1378                 cur.execute(*args)
   1379             return cur
   1380         except Exception as exc:

DatabaseError: Execution failed on sql 'SELECT * FROM NLY_balance_sheet': no such table: NLY_balance_sheet
INSTALLED VERSIONS ------------------ commit: None python: 3.7.0.final.0 python-bits: 64 OS: Darwin OS-release: 17.4.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: en_US.UTF-8 LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.23.3
pytest: None
pip: 10.0.1
setuptools: 39.2.0
Cython: None
numpy: 1.15.0
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: 4.2.3
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: 0.5.0
pandas_datareader: 0.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    IO SQLto_sql, read_sql, read_sql_queryNeeds InfoClarification about behavior needed to assess issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions