如何使用 F# 更新 SQL 表

发布于 2024-12-09 19:00:46 字数 552 浏览 1 评论 0原文

我想通过 F# 查询更新 MySQL 表。 基本上我现在可以将 MySql 表导入到 F# 矩阵中。 我正在通过 F# 对该矩阵进行一些计算,计算后我想用获得的新值更新主 MySQL 表。 例如,让我们采用来自 MySQL 表的简单矩阵:

let m  = matrix [[1.;2.;3.];[1.;1.;3.];[3.;3.;3.]]

现在我想设计一个更新 mySQL 表的查询。

let query() =
  seq { use conn = new SqlConnection(connString)
        do conn.Open()
        use comm = new SqlCommand("UPDATE MyTAble SET ID = "the first column of the matrix",
                                  conn)
       }

我需要将矩阵转换为序列吗? 对于这个迭代更新,我是否需要使用T-SQL(我通过阅读其他一些答案找到了这种方法?

I would like to update a MySQL table from an F# query.
Basically I am now able to import a MySql table into a F# matrix.
I am doing some calculation on this matrix through F# and after the calculation I would like to update the primary MySQL table with the new values obtained.
For example let's take a simple matrix coming from a MySQL table :

let m  = matrix [[1.;2.;3.];[1.;1.;3.];[3.;3.;3.]]

Now I would like to design a query which updates the mySQL table.

let query() =
  seq { use conn = new SqlConnection(connString)
        do conn.Open()
        use comm = new SqlCommand("UPDATE MyTAble SET ID = "the first column of the matrix",
                                  conn)
       }

Do I need to convert the matrix into a sequence?
For this iterative update, do I need to use T-SQL (I found this way by reading some others answers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有内置功能可以更轻松地更新数据库中的 F# matrix,因此您必须自己编写更新。我认为最好的方法是简单地使用 for 迭代矩阵,并在值发生变化时运行更新命令。类似于:

let m1 = matrix [[1.;0.]; [0.;2.]] // The original value from DB
let m2 = m1 + m1                   // New updated matrix 

for i in 0 .. (fst m1.Dimensions) - 1 do
  for j in 0 .. (snd m1.Dimensions) - 1 do
    if m1.[i, j] <> m2.[i, j] then
      // Here you need to do the actual update
      printfn "SET %d, %d = %f" i j m2.[i, j]

您的函数已经展示了如何运行单个 UPDATE 命令 - 只需注意,如果您不返回结果序列(在这里,您只想执行一些操作)。

如果更改值的数量很大,那么使用一些批量更新功能可能会很有用(这样你就不会运行太多可能很慢的 SQL 命令),但我不确定 MySQL 是否有类似的功能。

There is no built-in functionality that would make it easier to update F# matrix in a database, so you'll have to write the update yourself. I think the best approach is to simply iterate over the matrix using for and run the update command if the value has changed. Something like:

let m1 = matrix [[1.;0.]; [0.;2.]] // The original value from DB
let m2 = m1 + m1                   // New updated matrix 

for i in 0 .. (fst m1.Dimensions) - 1 do
  for j in 0 .. (snd m1.Dimensions) - 1 do
    if m1.[i, j] <> m2.[i, j] then
      // Here you need to do the actual update
      printfn "SET %d, %d = %f" i j m2.[i, j]

Your function already shows how to run a single UPDATE command - just note that you don't need to wrap it in seq { .. } if you're not returning a sequence of results (here, you just want to perform some action).

If the number of changed values is big, then it may be useful to use some bulk update functionality (so that you don't run too many SQL commands which may be slow), but I'm not sure if MySQL has anything like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文