如何编写从CSV文件导入数据并填充表的存储过程?
我想导入一个大的 csv 文件,而不需要先将该 csv 文件创建表到 postgresql 中。在 stackoverflow 上搜索时,一位用户建议使用以下代码:
from sqlalchemy import create_engine
engine = create_engine('postgresql://username:password@localhost:5432/mydatabase')
df.to_sql('table_name', engine)
Run Code Online (Sandbox Code Playgroud)
基于此,我根据我的 postgresql 的信息编写了以下代码,其中用户名 =“postgres”,密码 =“Katherine@412”:
from sqlalchemy import create_engine
engine = create_engine('postgresql://[postgres]:Katherine@412@postgres:5432/covid_deaths.csv')
df.to_sql('covid_deaths_owid', engine)
Run Code Online (Sandbox Code Playgroud)
但是它给了我以下错误:
OperationalError: (psycopg2.OperationalError) could not translate host name "412@postgres" to address: Unknown server error
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我我做错了什么吗?