Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将数据帧从jupyter notebook写入snowflake,而不定义表列类型_Python_Jupyter Notebook_Snowflake Cloud Data Platform - Fatal编程技术网

Python 将数据帧从jupyter notebook写入snowflake,而不定义表列类型

Python 将数据帧从jupyter notebook写入snowflake,而不定义表列类型,python,jupyter-notebook,snowflake-cloud-data-platform,Python,Jupyter Notebook,Snowflake Cloud Data Platform,我在jupyter笔记本中有一个数据框。我的目标是将此df作为新表导入snowflake 有没有办法直接将新表写入snowflake,而不定义任何表列的名称和类型 我正在使用 import snowflake.connector as snow from snowflake.connector.pandas_tools import write_pandas from sqlalchemy import create_engine import pandas as pd connection

我在jupyter笔记本中有一个数据框。我的目标是将此df作为新表导入snowflake

有没有办法直接将新表写入snowflake,而不定义任何表列的名称和类型

我正在使用

import snowflake.connector as snow
from snowflake.connector.pandas_tools import write_pandas
from sqlalchemy import create_engine
import pandas as pd

connection = snowflake.connector.connect(
    user='XXX',
    password='XXX',
    account='XXX',
    warehouse='COMPUTE_WH',
    database= 'SNOWPLOW',
    schema = 'DBT_WN'
    )


df.to_sql('aaa', connection, index = False)
它遇到了一个错误: DatabaseError:对sql“从sqlite_master中选择名称,其中type='table'和name=?;”执行失败:并非所有参数都在字符串格式化期间转换


有人能提供示例代码来解决这个问题吗?

这里有一种方法可以做到这一点——为我的代码格式与python的空格vs制表符模型结合在一起提前道歉。如果剪切粘贴,请检查选项卡/空格

由于Snowsql安全模型,请确保在连接参数中指定您正在使用的角色。通常默认角色为“PUBLIC”

既然你已经有了炼金术。。。这个想法没有使用雪花写熊猫,所以它不是一个大数据帧的好答案。。。炼金术和雪花的一些奇怪行为;确保数据帧列名为大写;但是在参数中使用小写的表名来表示sql

def df2sf_alch(target_df, target_table):
   # create a sqlAlchemy connection object
   engine = create_engine(f"snowflake://{your-sf-account-url}",
                        creator=lambda:connection)
   # re/create table in Snowflake
   try:
       # sqlAlchemy creates table based on a lower-case table name
       #      and it works to have uppercase df column names
       target_df.to_sql(target_table.lower(), con=engine, if_exists='replace', index=False)
       print(f"Table {target_table.upper()} re/created")
   except Exception as e:
       print(f"Could not replace table {target_table.upper()}", exc_info=1)
   nrows = connection.cursor().execute(f"select count(*) from {target_table}").fetchone()[0]
   print(f"Table {target_table.upper()} rows = {nrows}")

注意:为了创建sqlAlchemy连接对象,需要更改此函数以反映适当的“雪花帐户url”。另外,假设在df中处理了大小写命名的异常情况,以及您已经定义的连接,您只需通过df和表名调用此函数,如df2sf_alchmy_df,“MY_table”

df的模式与“DBT_WN”模式相同?@BlackRaven yea,表应另存为“SNOWPLOW”。DBT_WN。“aaa”