I’m trying to grab data from a MySQL database.
Approach 2 – apply/map style
I’m using the MySQL ADO Reference to try to build this system. In particular, the example found at 21.2.3.1.7.
(using a pseudo code)
let table = build_sequence(query.read)
Where query.read returns a row in the table(Or rather, a list of elements that happen to be a row in the table). And the table variable is a list of lists that will represent a table returned from the query.
I’ve stared at the code given below, and it’s syntax is over my head, I’m afraid.
Approach 1 – looping.
Problem 1: It’s inelegant, requiring a mutable.
Problem 2: This just feels wrong, based on my prior experience with Prolog & Lisp. There’s gotta be a more…functional way to do this.
I’m not sure where to begin though. Comments & thoughts?
let reader : MySql.Data.MySqlClient.MySqlDataReader = command.ExecuteReader() let arr = [] let mutable rowIter = 0 let readingLoop() = while(reader.Read()) do rowIter = rowIter + 1 for i = 0 to reader.FieldCount do //set arr[someiterator, i] = reader.GetValue[i].ToString())
The Seq type has a neat function for handling database cursors called generate_using (see F# Manual and the Data Access chapter in Foundations of F#). This is a higher order function that takes one function to open the cursor and another (called repeatedly) to process records from the cursor. Here is some code that uses generate_using to execute a sql query:
For example to list all the tables in an Oracle database we need to define a column definition type and invoke executeSqlReader as follows: