Data output to text files, spreadsheets and databases, and to memory
This example shows different ways of writing out data from a Mosel model, namely to
Accessing spreadsheets and databases
Using initializations to data is written to spreadsheets or databases through the ODBC connector in a similar way as text files are accessed; all necessary SQL commands are generated automatically. With the extended filename to "mmodbc.odbc:data.mdb" the data will be written to 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 write out data from a Mosel model directly with the corresponding SQL statements (this makes it possible, for example, to clear the data table before writing to it - this functionality is not available with spreadsheets)
SQLconnect("data.sqlite") SQLexecute("delete from MyOutTable") SQLexecute( "insert into MyOutTable (Index1,Index2,AValue) values (?,?,?)", A) SQLdisconnect
When working with Excel spreadsheets it is recommended to use one of the dedicated spreadsheet drivers from the mmsheet module: with the excel driver, the spreadsheet may remain open while writing to it from a Mosel model, in this case the data written out to the spreadsheet does not get saved, making it possible to re-run the application with different data sets without causing any lasting modifications to your spreadsheet. All other spreadsheet drivers require the spreadsheet file to be closed. The xls/xlsx drivers do not require an installation of Excel and are available for certain non-Windows platforms.
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 provided through mmmodbc. With initializations to an extended filename will take a form like "mmoci.oci:debug;myname/mypassword@dbname" and it is equally possible to use PL/SQL statements directly.
Data output in memory using I/O drivers
Using initializations to it is also possible to send data from a Mosel model in memory to the calling application (C/C++, C# or Java). This is done 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 output
Other possibilities of outputting data from a Mosel model include
Further explanation of this example: Whitepapers 'Using ODBC and other database interfaces with Mosel' and 'Generalized file handling in Mosel'
(!****************************************************** Mosel Example Problems ====================== file duo_out.mos ```````````````` Two ways of writing data to spreadsheets or databases via ODBC. (c) 2008 Fair Isaac Corporation author: S. Heipcke, 2006, rev. Sep. 2018 *******************************************************!) model "Duo output (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 A: array(-1..1,5..7) of real end-declarations A :: [ 2, 4, 6, 12, 14, 16, 22, 24, 26] ! First method: use an initializations block with the odbc driver ! ATTENTION: results from previous runs must be removed previously; ! otherwise the new results will either be appended to the existing ones ! or, if one of the fields has been defined as a key field in a database, ! the insertion will fail. initializations to "mmodbc.odbc:"+CNCTIO A as "MyOutTable1" end-initializations ! Second method: use SQL statements SQLconnect(CNCT) SQLexecute("delete from MyOutTable2") ! Cleaning up previous results: works ! only for databases, cannot be used ! with spreadsheets (instead, delete ! previous solutions directly in the ! spreadsheet file) SQLexecute("insert into MyOutTable2 (Index1,Index2,AValue) values (?,?,?)", A) SQLdisconnect end-model
|© Copyright 2021 Fair Isaac Corporation.|