我正在尝试将熊猫的数据帧写入redshift。
这是代码
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
'num_wings': [2, 0, 0, 0],
'num_specimen_seen': [10, 2, 1, 8]},
index=['falcon', 'dog', 'spider', 'fish'])
from sqlalchemy import create_engine
import sqlalchemy
sql_engine = create_engine('postgresql://username:password@host:port/dbname')
conn = sql_engine.raw_connection()
df.to_sql('tmp_table', conn, index = False, if_exists = 'replace')
但是,出现以下错误
---------------------------------------------------------------------------
UndefinedTable Traceback (most recent call last)
~/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pandas/io/sql.py in execute(self, *args, **kwargs)
1594 else:
-> 1595 cur.execute(*args)
1596 return cur
UndefinedTable: relation "sqlite_master" does not exist
...
...
...
1593 cur.execute(*args, **kwargs)
1594 else:
-> 1595 cur.execute(*args)
1596 return cur
1597 except Exception as exc:
DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': relation "sqlite_master" does not exist
我尝试使用pandas_redshift
用户,但是似乎第一个用户必须上传到s3存储桶,然后再上传到redshift。我想直接上传。同样,Here我认为答案建议先上传到s3,然后再上传到redshift
我可以使用相同的连接读取数据库并进行查询。
答案 0 :(得分:3)
我只是遇到了同样的问题,使用 engine 可以解决问题,请尝试以下操作:
import sqlalchemy
engine = sqlalchemy.create_engine('postgres://username:password@url:5439/db_name')
print(bool(engine)) # <- just to keep track of the process
with engine.connect() as conn:
print(bool(conn)) # <- just to keep track of the process
df.to_sql(name=table_name, con=engine)
print("end") # <- just to keep track of the process