gpt4 book ai didi

python - Bcp 命令进入 Microsoft Azure

转载 作者:太空宇宙 更新时间:2023-11-03 21:33:04 24 4
gpt4 key购买 nike

我正在尝试使用 bcp 命令 将数据附加到 azure 数据库中的现有表中。

当我直接从命令行导出 csv\txt 文件时 - 它看起来像一个二进制文件,然后从此文件的导入顺利进行。

但是,如果我尝试获取具有相同数据的 python pandas dataframe 并将其保存为 csv\txt,则内容会有所不同 - 没有二进制数据 - 只是正常值。

但是,从该文件导入后 - 没有错误,但导入了 0 行...

如何从数据帧创建与 bcp csv 相同的 csv?

这是我的工作代码(直接来自命令行):

--export
bcp "select * from tbl where val=1" queryout "Test\tbl1.csv" -S <server> -d <db> -U <user> -P <password> -q -n -t,

--import
bcp tbl2 in "Test\tbl1.csv" -S <server> -d <db> -U <user> -P <password> -q -n -t,

这是我的Python代码:

import subprocess as sp
...
df0.to_csv('tm2.csv',encoding='utf8',index=None,sep=',',header=None)

cmd2='bcp tbl2 in "Test\tm2.csv" -S <server> -d <db> -U <user> -P <password> -q -t, -n'

sp.call(cmd2)

但是表tbl2没有变化。

最佳答案

您的 bcp 命令行存在问题。根据官方文档关于bcp Utility-n选项表示使用数据的 native (数据库)数据类型,将数据导出为二进制格式,请参见下文。

-n Performs the bulk-copy operation using the native (database) data types of the data. This option does not prompt for each field; it uses the native values.

For more information, see Use Native Format to Import or Export Data (SQL Server).

但是,在您的场景中,导入/导出 csv 格式数据文件必须使用 -c 选项,请参阅下面的说明。

-c Performs the operation using a character data type. This option does not prompt for each field; it uses char as the storage type, without prefixes and with \t (tab character) as the field separator and \r\n (newline character) as the row terminator. -c is not compatible with -w.

For more information, see Use Character Format to Import or Export Data (SQL Server).

所以如果你想导入从 pandas dataframe 导出的 csv 数据文件,正确的方法是使用 -c 而不是 -n,那么它适用于在SQL数据库上通过bcp导入和导出CSV格式数据文件。

关于python - Bcp 命令进入 Microsoft Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53419871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com