简体   繁体   English

Dask / Python:将read_sql_table中的索引列的char转换为int

[英]Dask/Python: Converting char to int for index column in read_sql_table

I have a difficult time wrapping my head around the Dask read_sql_table method. 我很难绕过Dask的read_sql_table方法。 I manage to connect to the database just fine, but the column I wish to use as my index column is a char type holding only integers. 我设法很好地连接到数据库,但是我希望用作索引列的列是仅容纳整数的char类型。

I have tried: 我努力了:

from urllib.parse import quote_plus
server = 'SERVER_NAME'
db = 'DB_NAME'
table = 'TABLE_NAME'
connection_string = 'DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + db + ';Trusted_Connection=yes'
connection_string = quote_plus(connection_string)
connection_string = 'mssql+pyodbc:///?odbc_connect='+connection_string
df = dd.read_sql_table(table,connection_string,'sql.cast(sql.column("ID"),int).label("ID")')

(The server, db, table and column names have all been replaced with dummies here, as it is the company database) (服务器,数据库,表和列的名称在此处都已全部替换为虚拟变量,因为它是公司数据库)

The error I get is: 我得到的错误是:

KeyError                                  Traceback (most recent call last)
<ipython-input-25-8e261dcd8696> in <module>()
  6 connection_string = quote_plus(connection_string)
  7 connection_string = 'mssql+pyodbc:///?odbc_connect='+connection_string
----> 8 df = dd.read_sql_table(table,connection_string,'sql.cast(sql.column("ID"),int).label("ID")')

~\AppData\Local\Continuum\Anaconda3\lib\site-packages\dask\dataframe\io\sql.py in read_sql_table(table, uri, index_col, divisions, npartitions, limits, columns, bytes_per_chunk, **kwargs)
 73                          schema=schema)
 74 
---> 75     index = (table.columns[index_col] if isinstance(index_col, six.string_types)
 76              else index_col)
 77     if not isinstance(index_col, six.string_types + (elements.Label,)):

~\AppData\Local\Continuum\Anaconda3\lib\site-packages\sqlalchemy\util\_collections.py in __getitem__(self, key)
192 
193     def __getitem__(self, key):
--> 194         return self._data[key]
195 
196     def __delitem__(self, key):

KeyError: 'sql.cast(sql.column("ID"),int).label("ID")'

Anyone knows how to fix it? 有人知道如何解决吗?

OK... All it took was a bit more mucking about. 好吧...所要做的只是一点点的消沉。 The error was that I tried to pass the SQLalchemy expression as a string. 错误是我尝试将SQLalchemy表达式作为字符串传递。 I should have loaded the SQLalchemy modules and written it as a proper expression: 我应该已经加载了SQLalchemy模块并将其写为正确的表达式:

from urllib.parse import quote_plus
from sqlalchemy import sql, types
server = 'SERVER_NAME'
db = 'DB_NAME'
table = 'TABLE_NAME'
connection_string = 'DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + db + ';Trusted_Connection=yes'
connection_string = quote_plus(connection_string)
connection_string = 'mssql+pyodbc:///?odbc_connect='+connection_string
df = dd.read_sql_table(table,connection_string,sql.cast(sql.column("ID"),types.BigInteger).label("ID"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM