0

I am having trouble using ConfigParser() and an ini file with pandas.

I get this error:

UserWarning: pandas only support SQLAlchemy connectable(engine/connection) ordatabase string URI or sqlite3 DBAPI2 connectionother DBAPI2 objects are not tested, please consider using SQLAlchemy

I have this defined in my ini file:

[options]
myQuery = SELECT TOP (1000) * FROM [Books].[dbo].[springBooks]

In my Python script, I assign it the dbQuery variable like this:

dbQuery = config['options']['myQuery']

And I use it like this:

mydb = pyodbc.connect(Driver=driver,
                      Server=server,
                      Database=db,
                      uid=user, pwd=pw) 

print(dbQuery)

df = pd.read_sql_query(
    dbQuery, mydb)

When I print the query out, it looks fine like this:

SELECT TOP (1000) * FROM [Books].[dbo].[springBooks]

If I hard-code the query into the statement it works fine:

df = pd.read_sql_query(
    '''SELECT TOP (1000) * FROM [Books].[dbo].[springBooks]''', mydb)

What could I be messing up so bad?

Thanks!

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185
  • 3
    Have you seen [this related](https://stackoverflow.com/questions/71082494/getting-a-warning-when-using-a-pyodbc-connection-object-with-pandas)? And is it just the `UserWarning` that you're concerned about or is there a functional difference between the behavior when using `ConfigParser` parsed string versus your hardcoded string shown in the last block? – jedwards Jul 18 '22 at 23:46
  • @jedwards thanks, from that related link, I changed from pd.read_sql_query to `sqlalchemy`. I can print out the results of the `sqlalchemy` query to the screen, but how would I write it to a `dataframe` like I did with `read_sql_query`? Thanks – SkyeBoniwell Jul 19 '22 at 17:07

0 Answers0