简体   繁体   English

从 Pandas Dataframe 到 pyodbc (Azure SQL DB) 使用 ZF3D386AADEEB6063D016E4DFEE 转换字符串时从字符串转换失败:

[英]From Pandas Dataframe to pyodbc (Azure SQL DB) using sqlalchemy: Conversion failed when converting date and/or time from character string

I'm trying to load Salesforce data to Azure SQL Database incrementally by launching a Python script on Azure Databricks. I'm trying to load Salesforce data to Azure SQL Database incrementally by launching a Python script on Azure Databricks.

Since I'm not able to install Devart ODBC in Azure Databricks, I'm trying to use simple_salesforce to get data from salesforce:由于我无法在 Azure Databricks 中安装 Devart ODBC,因此我正在尝试使用 simple_salesforce 从 salesforce 获取数据:

import pandas as pd
import pyodbc
from simple_salesforce import Salesforce, SalesforceLogin, SFType
from sqlalchemy.types import Integer, Text, String, DateTime
from sqlalchemy import create_engine
import urllib

sf = Salesforce(password = password, username=username, security_token=jeton)
rep_qr = "SELECT SOMETHING FROM Account WHERE CONDITION"
soql = prep_qr.format(','.join(field_names))
results = sf.query_all(soql)['records']

I get the following result (an example):我得到以下结果(一个例子):

[OrderedDict([('attributes', OrderedDict([('type', 'Account'), ('url', '/services/data/v42.0/sobjects/Account/0014K000009aoU3QAI')])), ('Id', XY1), (Name, Y), (Date, 2020-11-24T09:16:17.000+0000)])]

Then I converted the output to a pandas Dataframe:然后我将 output 转换为 pandas Dataframe:

results = pd.DataFrame(sf.query_all(soql)['records'])
results.drop(columns=['attributes'], inplace=True) #to keep only the columns

I got something like this (just an example):我得到了这样的东西(只是一个例子):

Id ID Name姓名 Date日期
XY1 XY1 Y是的 2020-11-24T09:16:17.000+0000 2020-11-24T09:16:17.000+0000

In order to ingest this data into Azure SQL Database I have used "sqlalchemy" to convert the Dataframe into sql, then pyodbc will take in charge the insertion part into the destination (Azure SQL Database), as shown bellow: In order to ingest this data into Azure SQL Database I have used "sqlalchemy" to convert the Dataframe into sql, then pyodbc will take in charge the insertion part into the destination (Azure SQL Database), as shown bellow:

df = pd.DataFrame(results)
df.reset_index(drop=True, inplace=True) #just to remove the index from dataframe

#Creating the engine from and pyodbc which is connected to Azure SQL Database:
params = urllib.parse.quote_plus \
                (r'DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + server + ';DATABASE=' + database + ';UID=' + username + ';PWD=' + password)
conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params)
engine_azure = create_engine(conn_str, echo=True)
df.to_sql('account',engine_azure,if_exists='append', index=False)

But I get the following error:但我收到以下错误:

sqlalchemy.exc.DataError: (pyodbc.DataError) ('22007', '[22007] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting date and/or time from character string. (241) (SQLExecDirectW)')

I think the problem is the library simple_salesforce brigns the date/time in this format:我认为问题是库 simple_salesforce 以这种格式设置日期/时间:

2020-11-24T09:16:17.000+0000

But in Azure SQL Database it should be something like this:但是在 Azure SQL 数据库中应该是这样的:

2020-11-24T09:16:17.000

The problem here is I'm loading the tables dynamically (I don't even know the tables nor the columns that I'm loading) the reason why I can't cast these data type, I need a way to pass datatype to pyodbc automatically.这里的问题是我正在动态加载表(我什至不知道我正在加载的表和列)我无法转换这些数据类型的原因,我需要一种将数据类型传递给 pyodbc 的方法自动地。

What can you recommend please?请问有什么可以推荐的?

Thanks,谢谢,

If the date/time values are consistently returned as strings of the form 2020-11-24T11:22:33.000+0000 then you can use pandas' .apply() method to convert the strings to the 2020-11-24 11:22:33.000 format that SQL Server will accept:如果日期/时间值始终作为2020-11-24T11:22:33.000+0000形式的字符串返回,那么您可以使用 pandas 的.apply()方法将字符串转换为2020-11-24 11:22:33.000 SQL 服务器将接受的格式:

df = pd.DataFrame(
    [
        (1, "2020-11-24T11:22:33.000+0000"),
        (2, None),
        (3, "2020-11-24T12:13:14.000+0000"),
    ],
    columns=["id", "dtm"],
)
print(df)
"""console output:
   id                           dtm
0   1  2020-11-24T11:22:33.000+0000
1   2                          None
2   3  2020-11-24T12:13:14.000+0000
"""

df["dtm"] = df["dtm"].apply(lambda x: x[:23].replace("T", " ") if x else None)
print(df)
"""console output:
   id                      dtm
0   1  2020-11-24 11:22:33.000
1   2                     None
2   3  2020-11-24 12:13:14.000
"""

df.to_sql(
    table_name,
    engine,
    index=False,
    if_exists="append",
)

with engine.begin() as conn:
    pprint(conn.execute(sa.text(f"SELECT * FROM {table_name}")).fetchall())
"""console output:
[(1, datetime.datetime(2020, 11, 24, 11, 22, 33)),
 (2, None),
 (3, datetime.datetime(2020, 11, 24, 12, 13, 14))]
"""

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:SQL 服务器使用日期时间插入多个值(从字符串转换日期和/或时间时转换失败) - Python: SQL Server insert multiple values with datetime (Conversion failed when converting date and/or time from character string) 将生成器从 Pandas 中的 read_sql 转换为数据帧失败 - Converting generator from read_sql in pandas to dataframe has failed Python pandas 数据帧缩短了从十六进制字符串到整数的转换时间 - Python pandas dataframe shorten the conversion time from hex string to int 如何使用SQLAlchemy从数据库获取当前日期和时间 - How to get current date and time from DB using SQLAlchemy Python-使用Pandas数据框中的sqlalchemy写入SQL Server数据库 - Python - writing to SQL server database using sqlalchemy from a pandas dataframe 将熊猫数据框转换为字典时,将日期从字符串更改为日期时间对象 - Change in date from string to datetime object when converting pandas dataframe to dictionary 使用 Dask pyodbc 和 SQLAlchemy 从 SQL Server 中提取数据 - Pulling data from SQL Server using Dask pyodbc, and SQLAlchemy 从 UNIX 转换时,Pandas 数据框不包括一天中的时间 - Pandas dataframe not including time of day when converting from UNIX 使用 pyodbc 从 SQLAlchemy 连接到 Oracle 数据库 - Connect to Oracle database from SQLAlchemy using pyodbc pyodbc 中的 Pandas DataFrame 结果中缺少 1 行 - Pandas DataFrame from pyodbc missing 1 row in result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM