简体   繁体   中英

'SnowflakeCursor' object has no attribute 'to_dict'

I am trying to execute a select * query on snowflake using snowflake connector for python and while trying to add response data to a list getting following error : 'SnowflakeCursor' object has no attribute 'to_dict'

This is my source code

conn = snowflake.connector.connect(
            user=****,
            password=****,
            account=****,
            warehouse=****,
            host=****
        )
cmd = conn.cursor()
        for i in self.queries:
                cmd.execute(i['query'])
                data_sets.append(list(cmd.to_dict()))

        return data_sets

Getting the exception while doing cmd.to_dict(). Can anyone help me fix this?

Calling to_dict on cmd (which is a cursor object) doesn't seem to make a lot of sense -- you'd probably want to use the cmd.fetchall() to return the query's results as a list, and then cast the list elements to dictionaries.

There is probably an easier approach, however, and it's in using the DictCursor variant of cursor (which represents rows as dicts, rather than as tuples).

Here's how your modified code would look:

from snowflake.connector import DictCursor

cmd = conn.cursor(DictCursor)
        for i in self.queries:
                cmd.execute(i['query'])
                data_sets.append(cmd.fetchall())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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