Home > Blockchain >  unable to read data using dask
unable to read data using dask

Time:08-22

I want to get sql data using dask.read_sql.

My code is

from dask.dataframe import read_sql_query, read_sql, read_sql_table

username='uid'
password='pid'
database='myDB'
host='sql_server'
port='1433'
driver='ODBC Driver 17 for SQL Server'

conn_str = f"mssql pyodbc://{username}:{password}@{host}:{port}/{database}?driver={driver}"

ddf = read_sql_query(sql="select * from data", con=conn_str, index_col='id')

However, I got an error

File "C:\Program Files\Python38\lib\site-packages\dask\dataframe\io\sql.py", line 118, in read_sql_query
    q = sql.limit(head_rows)
AttributeError: 'str' object has no attribute 'limit'

How to solve this problem?

Thank you very much.

CodePudding user response:

conn_str = f"mssql pyodbc://{username}:{password}@{host}:{port}/{database}?driver={driver}"

Should be this:

conn_str = "mssql pyodbc://{username}:{password}@{host}:{port}/{database}?driver={driver}"

CodePudding user response:

The documentation for read_sql_query() says that the sql parameter must be an SQLAlchemy Selectable, and can not be a string.

The SQLAlchemy documentation suggests something like this:

from dask.dataframe import read_sql_query
from sqlalchemy import table, column

data = table("data",
        column("id"),
        ..., # all the other columns in your table
)

conn_str = ... # constructed as before
ddf = read_sql_query(sql=data.select(), con=conn_str, index_col='id')
  • Related