Note that there are some explanatory texts on larger screens.

plurals
  1. POTransfer a MySQL table into a F# matrix
    primarykey
    data
    text
    <p>I would like to transfer a SQL table (let say i. 2 columns : one containing users ID and one containing users age and ii. n rows) containing only integers into a F# matrix (same dimensions). I manage to do so with the following F# code, but I am convinced it is not the most efficient way to do so.</p> <p>Indeed, the only way I found to dimensionate the F# matrix was to create 2 tables with a single value (number of rows and number of columns respectively) using MySQL and transfer these value into F#.</p> <p>Is it possible to import a mySQL table into a F# matrix with a F# code which "recognize" the dimension of the matrix. Basically I would like a function which take a table address as an argument and return a matrix.</p> <p>Here is my code : </p> <pre><code>#r "FSharp.PowerPack.dll" #r "Microsoft.Office.Interop.Excel" open System open System.Data open System.Data.SqlClient open Microsoft.Office.Interop open Microsoft.FSharp.Math open System.Collections.Generic //Need of three types : User, number of rows and number of columns type user = { ID : int; Age : int;} type nbrRows = {NbreL : int ;} type nbrCol = {NbreC : int ;} // I. Import the SQL data into F# // I.1. Import the number of rows of the table into F# let NbrRows = seq { use cnn = new SqlConnection(@"myconnection; database=MyDataBase; integrated security=true") use cmd1 = new SqlCommand("Select * from theTablesWhichContainsTheNumberOfRows", cnn) cnn.Open() use reader = cmd1.ExecuteReader() while reader.Read() do yield { NbreL = unbox(reader.["Expr1"]) } } let NbrRowsList = NbrRows |&gt; Seq.toList // convert the sequence into a List // I.2. Same code to import the number of columns of the table let NbrCol = seq { use cnn = new SqlConnection(@"MyConnection; database=myDatabase; integrated security=true") use cmd1 = new SqlCommand("Select * from theTablesWhichContainsTheNumberOfColumns", cnn) cnn.Open() use reader = cmd1.ExecuteReader() while reader.Read() do yield { NbreC = unbox(reader.["Expr1"]) } } let NbrColsList = NbrCol |&gt; Seq.toList // Initialisation of the Matrix let matrixF = Matrix.create NbrRowsList.[0].NbreL NbrColsList.[0].NbreC 0. //Transfer of the mySQL User table into F# through a sequence as previously let GetUsers = seq { use cnn = new SqlConnection(@"myConnection, database=myDatabase; integrated security=true") use cmd = new SqlCommand("Select * from tableUser ORDER BY ID", cnn) cnn.Open() use reader = cmd.ExecuteReader() while reader.Read() do yield { ID = unbox(reader.["ID"]) Age = unbox(reader.["Age"]) } } // Sequence to list let UserDatabaseList = GetUsers |&gt; Seq.toList // Fill of the user matrix for i in 0 .. (NbrRowList.[0].NbreL - 1) do matrixF.[0,i] &lt;- UserDatabaseList.[i].ID |&gt; float matrixF.[1,i] &lt;- UserDatabaseList.[i].Age|&gt; float matrixUsers </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload