Last active
November 7, 2019 10:23
-
-
Save edgarsanchez/43b372ad7f3c0a5d655dc5498f1e4a67 to your computer and use it in GitHub Desktop.
Loading a CSV file to a relational table using F# type providers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `payments` ( | |
`Id` INT NOT NULL, | |
`Name` VARCHAR(50) NOT NULL, | |
`Amount` DECIMAL(10,2) NOT NULL, | |
`Date` DATE NOT NULL, | |
PRIMARY KEY (`Id`) | |
) | |
COLLATE='utf8_general_ci' | |
ENGINE=InnoDB | |
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module LoadCsv | |
open FSharp.Data | |
open FSharp.Data.Sql | |
open System | |
let [<Literal>] ConnectionString = "Server=localhost;Database=sales;User=root;Password=YOUR-DB-PASSWORD-HERE" | |
let [<Literal>] DbVendor = Common.DatabaseProviderTypes.MYSQL | |
let [<Literal>] ResPath = __SOURCE_DIRECTORY__ + "/../packages/MySql.Data/lib/net45" | |
type Payments = CsvProvider<"./data/paymentsSample.csv"> | |
type DbProvider = SqlDataProvider<DbVendor, ConnectionString, ResolutionPath = ResPath> | |
let loadCsv (path : string) = | |
try | |
let payments = Payments.Load path | |
let context = DbProvider.GetDataContext() | |
payments.Rows | |
|> Seq.iteri ( fun i p -> | |
let row = context.Sales.Payments.``Create(Amount, Date, Name)`` (p.Amount, p.Date, p.Name) | |
row.Id <- uint32 p.Id | |
if i % 500 = 499 then context.SubmitUpdates () ) | |
context.SubmitUpdates () | |
printfn "File '%s' loaded successfully!" path | |
0 | |
with | |
| ex -> | |
printfn "Exception '%s' raised when trying to load file '%s'." ex.Message path | |
ex.HResult | |
[<EntryPoint>] | |
let main argv = | |
if Array.isEmpty argv then | |
printfn "You should give the full path to the CSV file to load: LoadCsv.exe path.csv" | |
-1 | |
else | |
loadCsv argv.[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Id | Name | Amount | Date | |
---|---|---|---|---|
101 | Sánchez Santiago | 130.45 | 2017-05-23 | |
103 | Sánchez Alejandra | 190 | 2017-05-23 | |
109 | Vélez Carolina | 201.50 | 2017-05-25 | |
115 | Sánchez Sebastián | 88.21 | 2017-05-25 | |
118 | Baca Daniela | 144.44 | 2017-05-25 | |
123 | Camacho Ruth | 198.23 | 2017-05-26 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment