Reading sparse data from text files, spreadsheets and databases, and from memory
|  | ||
| Type: | Programming | |
| Rating: | 3 (intermediate) | |
| Description: | Suppose we want to read in data of the form 
 
 from a file, setting up a dynamic array A(range, range) with just the A(i,j)= value(i,j) for the pairs (i,j) which exist in the file. Here is an example which shows different ways of doing this. We read data from 
 Accessing spreadsheets and databases With initializations from data from spreadsheets or databases is read in a similar way as text files are accessed; switching between data sources can be done by replacing the I/O driver name and the data source in the extended filename, such as "mmsheet.excel:data.xls" to access the spreadsheet data.xls or "mmodbc.odbc:data.mdb" to read from the MS Access database data.mdb and the string "mmodbc.odbc:DSN=mysql;DB=data" could be used to access a mysql database named data. Instead of using an initializations block, we may read data from a database into a Mosel model by writing out the corresponding SQL statements (this format allows fine-tuning of the queries and gives access to a larger range of commands, in particular when working with databases). When developing applications with ODBC/SQL statements, the user is advised to use the following two parameter settings to obtain error messages and debugging information from ODBC:  setparam("SQLverbose", true)
 setparam("SQLdebug", true)The module mmoci (requires a separate license) defines a software-specific interface to Oracle databases that is used in a similar way as the ODBC connection.  OCIlogon('myname/mypassword@dbname')
 OCIexecute("select Index_i,Index_j,Value from MyDataTable", A5)
 OCIlogoffData input in memory using I/O drivers Using initializations from it is also possible to read data into a Mosel model from the calling application (C/C++, C#, Java) using a combination of the I/O drivers mem (data held in memory) and raw driver (data in binary format) in C, drivers dotnet and dotnetraw in C# programs, and drivers java and jraw in Java programs. Other options for data input Other possibilities of inputting data into a Mosel model include 
 | |
| File(s): | duo.mos, duosheet.mos, duoexc.mos, duodd.mos, duooci.mos | |
| Data file(s): | data.csv, data.xls, data.xlsx, data.mdb, data.sqlite, datadd.csv | |
|  | ||
| duo.mos | 
| (!******************************************************
   Mosel Example Problems
   ====================== 
   file duo.mos
   ````````````
   Two ways of reading sparse data tables from
   spreadsheets or databases via ODBC.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2006, rev. Sep. 2018
*******************************************************!)
model "Duo input (ODBC)"
 uses "mmodbc"
 parameters
                                 ! Use Excel spreadsheet `data.xls'
!  CNCT = 'DSN=Excel Files;DBQ=C:/xpress/examples/mosel/WhitePapers/MoselData/data.xls'
!  CNCT = 'data.xls'
!  CNCTIO = "data.xls"
                                 ! Use Access database `data.mdb'
!  CNCT = 'DSN=MS Access Database;DBQ=C:/xpress/examples/mosel/WhitePapers/MoselData/data.mdb'
  CNCT = 'data.mdb'
  CNCTIO = "debug;data.mdb"
                                 ! Use mysql database `data' (not provided)
!  CNCT = 'DSN=mysql;DB=data'
!  CNCTIO = "debug;DSN=mysql;DB=data"
                                 ! Use SQLite database `data.sqlite' via ODBC
!  CNCT = 'DSN=sqlite;DATABASE=data.sqlite'
!  CNCTIO = "debug;DSN=sqlite;DATABASE=data.sqlite"
                                 ! Use SQLite database `data.sqlite' directly
!  CNCT = 'data.sqlite'
!  CNCTIO = "debug;data.sqlite"
 end-parameters
 declarations
  A4, A5: dynamic array(range,range) of real
 end-declarations
! First method: use an initializations block with the odbc driver
 initializations from "mmodbc.odbc:"+CNCTIO
  A4 as 'MyDataTable'
 end-initializations
! Second method: use SQL statements 
 SQLconnect(CNCT)
 SQLexecute("select Index_i,Index_j,Value from MyDataTable", A5)
 SQLdisconnect
! Now let us see what we have
 writeln('A4 is: ', A4)
 writeln('A5 is: ', A5)
end-model
 | 
| duosheet.mos | 
| (!******************************************************
   Mosel Example Problems
   ====================== 
   file duosheet.mos
   `````````````````
   Using the generic spreadsheet drivers for reading 
   sparse data tables from a spreadsheet.
   
   (c) 2012 Fair Isaac Corporation
       author: S. Heipcke, Dec. 2012
*******************************************************!)
model "Duo input (generic spreadsheet)"
 uses "mmsheet"
 parameters
  CNCTIO1 = "data.xls"           ! Use Excel spreadsheet `data.xls'
  CNCTIO2 = "data.xlsx"          ! Use Excel spreadsheet `data.xlsx'
  CNCTIO3 = "data.csv"           ! Use spreadsheet `data.csv'
 end-parameters
 declarations
  A4,A5,A6: dynamic array(range,range) of real
 end-declarations
! Use the xls driver for reading the data
! (the spreadsheet ranges include a header line -> use option 'skiph')
 initializations from "mmsheet.xls:"+CNCTIO1
  A4 as 'skiph;MyDataTable'
 end-initializations
(! Alternatively:
 initializations from "mmsheet.xls:"+CNCTIO1
  A4 as 'MyDataTable2'
 end-initializations
! Or:
 initializations from "mmsheet.xls:"+CNCTIO1
  A4 as '[Sheet1$B3:D6]'
 end-initializations
!)
! Use the xlsx driver for reading the data
! (the spreadsheet ranges include a header line -> use option 'skiph')
 initializations from "mmsheet.xlsx:"+CNCTIO2
  A5 as 'skiph;MyDataTable'
 end-initializations
(! Alternatively:
 initializations from "mmsheet.xlsx:"+CNCTIO2
  A5 as 'MyDataTable2'
 end-initializations
! Or:
 initializations from "mmsheet.xlsx:"+CNCTIO2
  A5 as '[Sheet1$B3:D6]'
 end-initializations
!)
! Use the csv driver for reading the data
 initializations from "mmsheet.csv:"+CNCTIO3
  A6 as '[B3:D6]'
 end-initializations
! Now let us see what we have
 writeln('A4 is: ', A4)
 writeln('A5 is: ', A5)
 writeln('A6 is: ', A6)
end-model
 | 
| duoexc.mos | 
| (!******************************************************
   Mosel Example Problems
   ====================== 
   file duoexc.mos
   ```````````````
   Using the excel IO driver for reading 
   sparse data tables from a spreadsheet.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Jan. 2007, rev. Dec. 2012
*******************************************************!)
model "Duo input (Excel)"
 uses "mmsheet"
 parameters
  CNCTIO = "data.xls"            ! Use Excel spreadsheet `data.xls'
 end-parameters
 declarations
  A4: dynamic array(range,range) of real
 end-declarations
! Use the excel driver for reading the data
! (the spreadsheet ranges include a header line -> use option 'skiph')
 initializations from "mmsheet.excel:"+CNCTIO
  A4 as 'skiph;MyDataTable'
 end-initializations
(! Alternatively:
 initializations from "mmsheet.excel:"+CNCTIO
  A4 as 'MyDataTable2'
 end-initializations
! Or:
 initializations from "mmsheet.excel:"+CNCTIO
  A4 as '[Sheet1$B3:D6]'
 end-initializations
!)
! Now let us see what we have
 writeln('A4 is: ', A4)
end-model
 | 
| duodd.mos | 
| (!******************************************************
   Mosel Example Problems
   ====================== 
   file duodd.mos
   ``````````````
   Using the diskdata functionality for reading a simple
   CSV format file.
   
   (c) 2020 Fair Isaac Corporation
       author: S. Heipcke, Apr. 2020
*******************************************************!)
model "Duo input (diskdata)"
 uses "mmetc"
 parameters
  CNCTIO = "datadd.csv"           ! Use CSV file `datadd.csv'
 end-parameters
 declarations
  A7,A8: dynamic array(range,range) of real
 end-declarations
! Use the diskdata driver for reading the data
 initializations from "mmetc.diskdata:" 
  A7 as 'csv(Index_i,Index_j,Value),skiph,'+CNCTIO
 end-initializations
! Use the diskdata subroutine for reading the data
 diskdata(ETC_IN+ETC_CSV+ETC_SKIPH, CNCTIO, A8)
! Now let us see what we have
 writeln('A7 is: ', A7)
 writeln('A8 is: ', A8)
end-model
 | 
| duooci.mos | 
| (!******************************************************
   Mosel Example Problems
   ====================== 
   file duooci.mos
   ```````````````
   Two ways of reading sparse data tables from
   Oracle databases.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Jan. 2007, rev. Jul. 2010
*******************************************************!)
model "Duo input (OCI)"
 uses "mmoci"
 parameters
  CNCT = 'myname/mypassword@dbname'
 end-parameters
 declarations
  A4, A5: dynamic array(range,range) of real
 end-declarations
! First method: use an initializations block with the oci driver
 initializations from "mmoci.oci:debug;"+CNCT
  A4 as 'MyDataTable'
 end-initializations
! Second method: use SQL statements 
 OCIlogon(CNCT)
 OCIexecute("select Index_i,Index_j,Value from MyDataTable", A5)
 OCIlogoff
! Now let us see what we have
 writeln('A4 is: ', A4)
 writeln('A5 is: ', A5)
end-model
**************************************************
! Creation of the input table in an Oracle database:
 declarations
  tsucc: array ({false,true}) of string
 end-declarations
 tsucc(false):="failed"; tsucc(true):="succeeded"
 OCIexecute("create table MyDataTable (Index_i integer, Index_j integer, Value float)")
 writeln(" - Create MyDataTable (",tsucc(getparam("OCIsuccess")),")")
 declarations
  A: array(range,range) of real
 end-declarations
 A(1,1):=12.5; A(2,3):=5.6; A(10,9):=-7.1; A(3,2):=1 
 OCIexecute("insert into MyDataTable (Index_i, Index_j, Value) values (:1, :2, :3)", A)
 writeln(" - Insert values in MyDataTable (",tsucc(getparam("OCIsuccess")),",",
        getparam("OCIrowcnt"),'/',getparam("OCIrowxfr")," rows)")
 | 
© 2001-2023 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.
 
