Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.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 使用dataframe中的值更新sql server中的表_Python_Sql Server_Pandas - Fatal编程技术网

Python 使用dataframe中的值更新sql server中的表

Python 使用dataframe中的值更新sql server中的表,python,sql-server,pandas,Python,Sql Server,Pandas,我在sql server中有一个表 id count 1 1 2 1 3 1 4 1 5 1 我在pandas dataframe(df)中有另一个表,带有更新的计数 id count 1 1 2 1 3 2 4 3 5 4 我想使用updatequery在数据库中进行更改,我正在考虑定义一个函数,它可以实现这一点 我正在使用PyODBC进行连接 con

我在sql server中有一个表

id    count
1       1
2       1
3       1
4       1
5       1
我在pandas dataframe(df)中有另一个表,带有更新的计数

id    count
1       1
2       1
3       2
4       3
5       4
我想使用updatequery在数据库中进行更改,我正在考虑定义一个函数,它可以实现这一点

我正在使用PyODBC进行连接

    conn = pypyodbc.connect("Driver={SQL Server};Server=<YourServer>;Database=<YourDatabase>;uid=<YourUserName>;pwd=<YourPassword>"

但是有没有更好的方法呢?

在你的问题中,你要做的是一个迭代更新,一行一行地循环。SQL数据库在这种操作中效率很低。不过,SQL数据库在基于集的更新方面非常高效

实际上,这意味着编写一个脚本,可以一次性应用于整个表,然后运行一次

在本例中,您可以在SQL Server Management Studio(SSMS)中的查询中的一些可变表变量中演示这一点:

首先,创建测试数据:

declare @t table(id int, [count] int);
insert into @t values(1,1),(2,1),(3,1),(4,1),(5,1);

declare @p table(id int, [count] int);
insert into @p values(1,1),(2,1),(3,2),(4,3),(5,4);
前两条
select
语句显示更新前数据的外观:

select *
from @t;

select *
from @p;
要更新SQL表中的所有数据行,您可以将其连接到Pandas表中的数据,然后再次
选择该数据,以便查看
更新中发生的更改:

update t
set [count] = p.[count]
from @t as t
    join @p as p
        on(t.id = p.id);

select *
from @t;

select *
from @p;


我不太熟悉Pandas Dataframe,因此不知道如何访问和查询这些数据。如果您正在处理大型数据集,我建议您将Pandas数据原样导入SQL Server数据库中的暂存表中,并在SQL Server中运行上述类型的查询。

那么您在这里执行SQL查询吗?到目前为止你都尝试了什么?@iamdave是的,请参见编辑
update t
set [count] = p.[count]
from @t as t
    join @p as p
        on(t.id = p.id);

select *
from @t;

select *
from @p;