Python数据帧值插入到数据库表列
是否可以将python数据帧值插入数据库表列 我使用雪花作为我的数据库 通勤时间是包含StudentID列的表。“add_col”是python数据帧。我需要将df值插入StudentID列 下面是我试图将df值插入表列的代码Python数据帧值插入到数据库表列,python,sql,snowflake-cloud-data-platform,Python,Sql,Snowflake Cloud Data Platform,是否可以将python数据帧值插入数据库表列 我使用雪花作为我的数据库 通勤时间是包含StudentID列的表。“add_col”是python数据帧。我需要将df值插入StudentID列 下面是我试图将df值插入表列的代码 c_col = pd.read_sql_query('insert into "SIS_WIDE"."PUBLIC"."CommuteTime" ("StudentID") VALUES ("add_col")', engine) 当我执行上述命令时,它不接受数据帧。它
c_col = pd.read_sql_query('insert into "SIS_WIDE"."PUBLIC"."CommuteTime" ("StudentID") VALUES ("add_col")', engine)
当我执行上述命令时,它不接受数据帧。它抛出了下面的错误
ProgrammingError: (snowflake.connector.errors.ProgrammingError) 000904 (42000): SQL compilation error: error line 1 at position 68
invalid identifier '"add_col"' [SQL: 'insert into "SIS_WIDE"."PUBLIC"."CommuteTime" ("StudentID") VALUES ("add_col")']
(Background on this error at: http://sqlalche.me/e/f405)
请提供解决此问题的建议。您无法使用
pd。请阅读\u sql\u query
首先,您需要创建
e、 g
一旦有了游标,就可以这样查询:cursor.execute(“从“通勤时间”中选择*)
要将数据插入表中,需要使用
请提供有关数据帧的更多信息,以进一步帮助您。我只能使用SQL Alchemy而不是Snowflake Python连接器来实现这一点
from sqlalchemy import create_engine
# Establish the connection to the Snowflake database
sf = 'snowflake://{}:{}@{}{}'.format(user, password, account, table_location)
engine = create_engine(sf)
# Write your data frame to a table in database
add_col.to_sql(table_name, con=engine, if_exists='replace', index=False)
请参阅以了解如何通过传递用户名
、密码
、帐户
和表位置
来建立与雪花的连接
如果函数
to_sql()
中存在参数,则可以通过使用pd.read\u sql\u query
来探索以形式传递给函数的参数。这当然不是通过使用pd.read\u sql\u query
来实现的。这是用于从SELECT语句的结果创建数据帧的。在数据帧上有一个方法,但会(尝试)将数据框作为表插入。如果您确实希望按照查询建议将整个数据框作为单个属性插入到单个记录中,则必须手动执行此操作。
from sqlalchemy import create_engine
# Establish the connection to the Snowflake database
sf = 'snowflake://{}:{}@{}{}'.format(user, password, account, table_location)
engine = create_engine(sf)
# Write your data frame to a table in database
add_col.to_sql(table_name, con=engine, if_exists='replace', index=False)