0

I want to load data from PostgreSQL to Elasticsearch and I write below code, but it gives me the error in for loop, my code is as below:

from elasticsearch import Elasticsearch

#res = requests.get('http://localhost:9200')
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
sql_conn = psycopg2.connect(host="localhost",database="priyanka",user="postgres",password="admin")
sql_cursor = sql_conn.cursor()
sql=('SELECT * FROM "Sales"')
sql_cursor=sql_cursor.execute(sql)
tempList=[]
tmp = {}
for row in sql_cursor.fetchall():
    print("1")
    Sales_id =str(row[0])
    Product_name =str(row[1])
    sales =str(row[2])

    tmp["Sales_id"]=Sales_id
    tmp["name"]=Product_name
    tmp["sales"]=sales

Error is:

Traceback (most recent call last):
  File "D:/Priyanka Pagar/Softwares/Team/Python37/elastic - Copy.py", line 16, in <module>
    for row in sql_cursor.fetchall():
AttributeError: 'NoneType' object has no attribute 'fetchall'
1
  • Connecting as the postgres user from an application is poor practice. Your SELECT may have failed because you did not specify a schema.
    – Vince
    Commented Jan 9, 2020 at 12:42

1 Answer 1

5

Change

sql_cursor=sql_cursor.execute(sql)

to

sql_cursor.execute(sql)

Because the execute method returns none, this re-assignment destroys the ability to later fetch the results from the cursor.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.