flibs/sqlite - Interface to SQLite
TABLE OF CONTENTS
SYNOPSIS
DESCRIPTION
DATA TYPES
ROUTINES
EXAMPLE
LIMITATIONS
IMPLEMENTATION NOTES
COPYRIGHT
The sqlite module provides a Fortran interface to the SQLite database management system (SQLite 3 to be more precise). The interface has been implemented in such a way, that you can use a high-level interface for common tasks, such as inserting data into a database and querying the contents, as well as lower-level functionality, accessible via SQL statements, for instance.
To this end the module defines a set of routines and functions as well as several derived types to hide the low-level details.
In its current form, it does not provide a full Fortran API to all the functionality offered by SQLite, but it should be quite useable.
Note: When in doubt, consult the SQLite documentation at http://www.sqlite.org - most routines merely prepare SQL statements or are a simple interface to the original C functions.
The following derived types are defined:
The third type is rather crucial to the working of the implementation: By setting the properties of an SQLITE_COLUMN variable you put data into the database or you can retrieve data from the database. See the example below for how this works.
There are a number of routines that are meant to make this easier:
The sqlite module currently provides the following functions:
To illustrate the usage of the library, here is a small example:
allocate( column(4) ) call sqlite3_column_props( column(1), name(1), SQLITE_CHAR, 10 ) call sqlite3_column_props( column(2), name(2), SQLITE_CHAR, 10 ) call sqlite3_column_props( column(3), name(3), SQLITE_REAL ) call sqlite3_column_props( column(4), name(4), SQLITE_REAL ) call sqlite3_create_table( db, 'measurements', column ) |
call sqlite3_begin( db )
do
read( lun, *, iostat=ierr ) station, date, salin, temp
if ( ierr .ne. 0 ) exit
call sqlite3_set_column( column(1), station )
call sqlite3_set_column( column(2), date )
call sqlite3_set_column( column(3), salin )
call sqlite3_set_column( column(4), temp )
call sqlite3_insert( db, 'measurements', column )
enddo
close( lun )
call sqlite3_commit( db )
|
The last part retrieves the data by constructing an SQL query that will actually look like:
select station, avg(salinity), avg(temperature) from measurements
grouped by station order by station;
|
deallocate( column )
allocate( column(3) )
call sqlite3_column_query( column(1), 'station', SQLITE_CHAR )
call sqlite3_column_query( column(2), name(3), SQLITE_REAL, function='avg' )
call sqlite3_column_query( column(3), name(4), SQLITE_REAL, function='avg' )
call sqlite3_prepare_select( db, 'measurements', column, stmt, &
'group by station order by station' )
write( *, '(3a20)' ) 'Station', 'Mean salinity', 'Mean temperature'
do
call sqlite3_next_row( stmt, column, finished )
if ( finished ) exit
call sqlite3_get_column( column(1), station )
call sqlite3_get_column( column(2), salin )
call sqlite3_get_column( column(3), temp )
write( *, '(a20,2f20.3)' ) station, salin, temp
enddo
|
! csvtable.f90 --
! Program to read a simple CSV file and put it into a
! SQLite database, just to demonstrate how the Fortran
! interface works.
!
! To keep it simple:
! - The first line contains the names of the four columns
! - All lines after that contain the name of the station
! the date and the two values.
!
! $Id: fsqlite.man,v 1.2 2007/04/16 20:00:18 arjenmarkus Exp $
!
program csvtable
use sqlite
implicit none
type(SQLITE_DATABASE) :: db
type(SQLITE_STATEMENT) :: stmt
type(SQLITE_COLUMN), dimension(:), pointer :: column
integer :: lun = 10
integer :: i
integer :: ierr
character(len=40), dimension(4) :: name
real :: salin
real :: temp
character(len=40) :: station
character(len=40) :: date
logical :: finished
!
! Read the CSV file and feed the data into the database
!
open( lun, file = 'somedata.csv' )
read( lun, * ) name
call sqlite3_open( 'somedata.db', db )
allocate( column(4) )
call sqlite3_column_props( column(1), name(1), SQLITE_CHAR, 10 )
call sqlite3_column_props( column(2), name(2), SQLITE_CHAR, 10 )
call sqlite3_column_props( column(3), name(3), SQLITE_REAL )
call sqlite3_column_props( column(4), name(4), SQLITE_REAL )
call sqlite3_create_table( db, 'measurements', column )
!
! Insert the values into the table. For better performance,
! make sure (via begin/commit) that the changes are committed
! only once.
!
call sqlite3_begin( db )
do
read( lun, *, iostat=ierr ) station, date, salin, temp
if ( ierr .ne. 0 ) exit
call sqlite3_set_column( column(1), station )
call sqlite3_set_column( column(2), date )
call sqlite3_set_column( column(3), salin )
call sqlite3_set_column( column(4), temp )
call sqlite3_insert( db, 'measurements', column )
enddo
close( lun )
call sqlite3_commit( db )
!
! We want a simple report, the mean of salinity and temperature
! sorted by the station
!
deallocate( column )
allocate( column(3) )
call sqlite3_column_query( column(1), 'station', SQLITE_CHAR )
call sqlite3_column_query( column(2), name(3), SQLITE_REAL, function='avg' )
call sqlite3_column_query( column(3), name(4), SQLITE_REAL, function='avg' )
call sqlite3_prepare_select( db, 'measurements', column, stmt, &
'group by station order by station' )
write( *, '(3a20)' ) 'Station', 'Mean salinity', 'Mean temperature'
do
call sqlite3_next_row( stmt, column, finished )
if ( finished ) exit
call sqlite3_get_column( column(1), station )
call sqlite3_get_column( column(2), salin )
call sqlite3_get_column( column(3), temp )
write( *, '(a20,2f20.3)' ) station, salin, temp
enddo
call sqlite3_close( db )
end program
|
The module is not complete yet:
While the module is fairly straightforward Fortran 95 code, building a library out of it may not be straightforward due to the intricacies of C-Fortran interfacing.
This section aims to give a few guidelines:
Copyright © 2005 Arjen Markus <arjenmarkus@sourceforge.net>