简体   繁体   中英

Not able to insert data in AWS postgres RDS

I am unable to add pandas dataframe in aws postgres through pandas to_sql

I have tried adding one by one record and it works properly but when i try to add dataframe it doesn't shows any error and it is not committed

import pandas as pd 

import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

host="####.amazonaws.com"
port=5432
dbname="@@@@"
user="@@@@"
password="&&&&"

engine=create_engine("@@@@://@@@@:&&&&@####.amazonaws.com:5432/@@@@",
                       echo = True)

pd.read_sql("select * from Cities", engine)

id cityname latitude longitude

0 1 New York City 40.73 -73.93

1 100 abcdef 12.33 14.11

2 100 abcdef -12.33 14.11

tempdf = {'id': 1000, 'cityname': 'Delhi', 'latitude': '120', 'longitude': '110'}

tempdf = pd.DataFrame(list(tempdf.items()))

tempdf.to_sql(name='Cities', con=engine, index=False, if_exists='append')

pd.read_sql("select * from Cities", engine)

id cityname latitude longitude

0 1 New York City 40.73 -73.93

1 100 abcdef 12.33 14.11

2 100 abcdef -12.33 14.11

Answer linked below appears to suggest adding schema argument to your to_sql statement:

Pandas to_sql doesn't insert any data in my table

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