简体   繁体   English

使用 Python 将 CSV 从远程机器导入 Azure SQL Server

[英]Using Python to import CSV from remote machine into Azure SQL Server

I am trying to find a way to import a CSV file into Azure SQL Server from a remote machine using Python.我正在尝试找到一种使用 Python 从远程计算机将 CSV 文件导入 Azure SQL Server 的方法。 I wrote a script to GET data from a third-party API and then flatten JSON into a CSV file.我编写了一个脚本来从第三方 API 获取数据,然后将 JSON 压缩为 CSV 文件。 It works great.它工作得很好。 Now, I'm trying to improve the script to import that CSV into Azure SQL Server so that I don't have to manually upload the file using the Import Flat File tool.现在,我正在尝试改进脚本以将该 CSV 导入 Azure SQL Server,这样我就不必使用导入平面文件工具手动上传文件。

with pyodbc.connect(f'''DRIVER={dw_driver};SERVER={dw_server};PORT={dw_port};DATABASE={dw_database};UID={dw_user};PWD={dw_password}''') as dw_connection:

    with dw_connection.cursor() as dw_curs:

        with open(out_file, 'r') as csv_file:
            for row in csv_file:
                dw_curs.execute('insert into DataOperations.StageTable (Id, CustomerName, CustomerEmail, CustomerOrganization, AssistedSalesPerson) values (%s, %s, %s, %s, %s)', row)

This code returns the following error:此代码返回以下错误:

ProgrammingError: ('The SQL contains 0 parameter markers, but 1 parameters were supplied', 'HY000') ProgrammingError: ('SQL 包含 0 个参数标记,但提供了 1 个参数','HY000')

When I print the generated SQL statement, it looks this:当我打印生成的 SQL 语句时,它看起来像这样:

insert into DataOperations.StageTable (Id, CustomerName, CustomerEmail, CustomerOrganization, AssistedSalesPerson) values (%s, %s, %s, %s, %s) 12345678|Test Name|testemail@test.com| Test Company |

Can anyone help me modify this script into a working SQL insert statement?谁能帮我将此脚本修改为有效的 SQL 插入语句? I've trawled other similar questions on here but most recommend usin BULK INSERT which will not work because this file is not available on the database server.我在这里搜索了其他类似的问题,但大多数人建议使用BULK INSERT ,这将不起作用,因为该文件在数据库服务器上不可用。

The csv.reader() function must be used to create an object that can be used to iterate over lines in the target CSV file.必须使用csv.reader()函数来创建可用于迭代目标 CSV 文件中的行的对象。 The SQL parameters within the values() statement must use ? values() 语句中的 SQL 参数必须使用? instead of %s to be valid syntax.而不是%s是有效的语法。 The solution below is confirmed as working.确认以下解决方案有效。

with pyodbc.connect(f'''DRIVER={dw_driver};SERVER={dw_server};PORT={dw_port};DATABASE={dw_database};UID={dw_user};PWD={dw_password}''') as dw_connection:

    with dw_connection.cursor() as dw_curs:

        with open(out_file, 'r', encoding='utf-8') as csv_file:
            next(csv_file, None)  # skip the header row
            reader = csv.reader(csv_file, delimiter='|')
            for row in reader:
                dw_curs.execute('insert into DataOperations.StageTable (Id, CustomerName, CustomerEmail, CustomerOrganization, AssistedSalesPerson) values (?, ?, ?, ?, ?)', row)

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

相关问题 使用Python将CSV文件导入SQL Server - Import CSV file into SQL Server using Python 我可以将数据从本地SQL Server数据库导入到Azure机器学习虚拟机吗? - Can I import data from On-Premises SQL Server Database to Azure Machine Learning virtual machine? 连接到远程机器中的数据库 - python - SQL Server - Connect to database in Remote machine - python - SQL Server 使用python将文件从本地计算机传输到远程Windows服务器 - Transfer file from local machine to remote windows server using python 使用python脚本从远程服务器到同一远程服务器的mysqldump来自本地机器! - mysqldump from remote server to same remote serveritself from a local machine using python script! 从远程计算机将变量的值导入本地python脚本 - Import a varible's value from remote machine to local python script python将软件包运送到远程计算机并从内存导入 - python shipping package to remote machine and import from memory 使用 Z23EEEB4347BDD26BDDFC6B7EE 从 Azure 机器学习服务连接 Azure SQL 数据库时出错 - Error in connecting Azure SQL database from Azure Machine Learning Service using python 使用Python将CSV导入SQL Express - Import CSV to SQL Express using Python 在Azure笔记本中使用远程Azure自动机器学习模型时导入错误 - Import error when using remote Azure Automated Machine Learning model in Azure notebook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM