2
我最近一直在学习F#和函数式编程。我发现一个非常有用的应用程序是使用一些关联的ID为CSV(或Excel表)生成数据加载的SQL插入。F#从CSV生成SQL
下面的代码是我的结果,我相信我会发现在未来非常方便。我想其他人也可以从中受益,我欢迎建议,人们对收藏已经找到宝贵的其它脚本:
// Returns some dataload SQL for area mapping.
open System
open System.IO
// Read and split CSV into lists
let map_ngo_area = File.ReadAllLines(@"P:\MY_TABLE.csv")
|> Array.to_list
|> List.map(fun x -> (x.Split([|','|])
|> Array.map(fun y -> y.Trim()))
|> Array.to_list)
// Output Formatting function
let format_sql_record = "INSERT INTO MyTable
(ID, REF1_ID, REF2_ID, CreatedUser, CreatedDateTime, LastModifiedUser, LastModifiedDateTime)
VALUES
({0}, {1}, {2}, 'system', getDate(), 'system', getDate())"
// Generate the SQL for the given list.
let generate_sql list = list |> List.mapi(fun index row ->
match row with
| [ngo_id; area_id] -> String.Format(format_sql_record, ((int index)+1), ngo_id, area_id) |> printfn "%s"
| _ -> printfn "")
// Main Execution
map_ngo_area |> generate_sql |> ignore
// End of program, pause to view console output.
System.Console.ReadKey() |> ignore
对提高我的F#代码或程序有什么建议?评论也很受欢迎,因为我在这个范例中是相当新的,转变的想法并不如我预期的那样。
谢谢:)
真棒,谢谢KVB他们是一些伟大的秘诀! :) – Russell 2009-12-17 05:24:19