Mariadb_connector_fortran


About

A Open Source Fortran interface for Mariadb-connector/C

It can use to connect to Mariadb, MySQL, Oracle

Get it here :

https://github.com/v-h-giang/Mariadb_connector_fortran/

Download newest Mariadb client connector/C (dll file) here for windows OS :

https://mariadb.com/kb/en/mariadb-connector-c/

How to use with sample

Remember to place libmariadb.dll to main program folder

Define server with object type DATABASE_CONNECT_INFO. In under code , it is mydatabase . Have a init function for this object is DATABASE that need input 3 characters : IP_ADDRESS, USERNAME ,PASSWORD , and 1 integer number for PORT_ID of database server . Here we have a server called mydatabase that have :

  • IP_ADDRESS : 127.0.0.1
  • USERNAME : giang
  • PASSWORD : (no need because server is not remote)
  • PORT_ID : 3306
TYPE(DATABASE_CONNECT_INFO)  :: mydatabase
mydatabase = DATABASE("127.0.0.1","giang","",3306)

DATABASE_CONNECT_INFO have derived type bound function RUN , that can run any Mariadb command (or MySQL)

Anyway , for someone is familiar with Modern Fortran. This is how DATABASE_CONNECT_INFO defined :

 TYPE ::  DATABASE_CONNECT_INFO
            CHARACTER(len = : ), ALLOCATABLE                :: IP_ADDRESS
            CHARACTER(len = : ), ALLOCATABLE                :: USERNAME
            CHARACTER(len = : ), ALLOCATABLE                :: PASSWORD 
            INTEGER                                         :: PORT_ID = 3306
            TYPE(db_field_in_row),DIMENSION(:,:),ALLOCATABLE:: DATA 
            CONTAINS 
            PROCEDURE :: RUN 
END TYPE DATABASE_CONNECT_INFO

Under example , library is used for send 3 command to MariaDB database server

  • SHOW DATABASES
  • USE test_group
  • SELECT * FROM test_group.employees

Year, may be someone notice , we do not need semicolon “;” at end of line to send command through code 🤧🤧

program mariadb_test
  use :: mariadb_fortran
  implicit none

  CHARACTER(len = :),ALLOCATABLE                    :: comm
  TYPE(DATABASE_CONNECT_INFO)                       :: mydatabase

  mydatabase = DATABASE("127.0.0.1","giang","",3306)
  
  comm = "SHOW DATABASES" 
  call mydatabase%RUN(comm)
  print *, "enter to go next"
  read(*,*) 

  comm = "USE test_group" 
  call mydatabase%RUN(comm)
  print *, "enter to go next"
  read(*,*)

  comm = "SELECT * FROM test_group.employees" 
  call mydatabase%RUN(comm)
  print *, "enter to go next"
  read(*,*)
end program mariadb_test

Compile test file with gfortran in MSYS2 for windows os (may be need some package for Mariadb-connector/C header)

gfortran -Wall -static-libgfortran -static-libgcc -static-libstdc++ -O3 mariadb_test.f90 libmariadb_fortran.f90 c_util.f90 -lWs2_32 -lmariadb -o mariadb_test

Freely to use this library , Thanks !


Leave a Reply

Your email address will not be published. Required fields are marked *