声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 5727|回复: 7

[Fortran] Fortran中怎么读入Excel数据

[复制链接]
发表于 2007-5-28 15:26 | 显示全部楼层 |阅读模式
30体能
Fortran中怎么读入Excel数据?

[ 本帖最后由 风花雪月 于 2007-5-28 15:28 编辑 ]

回复
分享到:

使用道具 举报

发表于 2007-5-28 16:52 | 显示全部楼层
提供一种包含手动的方法。



1.第一部打开excel文件,点击另存为,选择文件类型为逗号分隔符csv,将其另存为csv格式
2.fortran可以直接读入csv文件
程序例子如下:
program batchout
integer aaa(3,11),bbb(11,3)

open(10,file='c:/myfile.csv')
read(10,250) aaa
250 format(3f16.5)
close(10)
bbb=transpose(aaa)
write(*,250) aaa
print *,'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
print *,'导入数据成功'
pause 1

end

http://www.programfan.com/club/showtxt.asp?id=213180

评分

1

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2007-5-29 09:29 | 显示全部楼层
毕竟这是手动的方法,看看有没有自动的方法
回复

使用道具 举报

发表于 2007-5-29 13:03 | 显示全部楼层

回复 #3 风花雪月 的帖子

下文来源:http://www.programfan.com/club/s ... 25&action=print
我个人没有尝试,感觉讨论的结论很不错,感兴趣的可以看看。

主题:[原创]实现在fortran 中调用数据库和电子表格读写的理想方法  
作者:nosper      发表时间:2007-4-23 23:16:00  

楼主  

实现在fortran 中调用数据库和电子表格读写的理想方法


感谢这段时间里 编成爱好者fortran版的各位热心的老师给予我无私的帮助!!

我以前只是简单的学习了一些c语言的内容,对程序一直不是太懂。只是因为毕业设计的原应才与两个月前开始学习fortran。我毕设的内容是要模拟理想的电力市场,并且预测发电厂的发电量和电力市场里面其他的量,里面关于计算的部分已经存在了,我要做的就是让fortran 从数据库里进行读写数据,这样是便于和java 结合,使得可以让用户从网上实时输入数据。

    我开始一直以为fortran 不能从数据库里读写数据的,而且网上也有相似的问题,但并没有答案,或者说是实现不了。所以我就准备将已由的程序编译成dll文件,让C++调用。但也有的程序太多了,而且在c++里调用数据库网上提到比较多的就是mfc方法,这样的话或产生界面,而我是不需要界面(界面会有java处理),在win32用c++直接调用数据库我有没有找到具体的例子。(大家有的话可以提供给我,谢谢了)

   这时候我都准备放弃了,还好,在编成网上有人提出了用f90SQL可以。

   然后我就在实验室的机子上开始尝试,刚开始时用cvf6.5 的,好像感觉并不是很行。实验室刚好这个时候新买了ivf9.1 ,幸运的是f90SQl在那个上面可以,直到这个时候我的毕设才真正的开始,在大家的帮助下,我进展的很快,现在已经差不多完成了。

我想把我在编成的一些经验和问题提出来,和大家一起分享一下:)


首先fortran是可以在数据库和电子表之间进行读写的。而且数据库与电子表的操作完全是一样的

1    调用F90SQL,
http://softwarecommunity.intel.c ... /thread/117268.aspx
这里可以免费下载f90SQL-lite版
安装:
复制f90SQL library (f90SQL.lib) 到fortran compiler 的LIB路径下, 我是直接在编译路径里面添加的。
复制  f90SQL dynamic link library (f90SQLxxx.DLL) (共2个)到系统目录下,如:WinNT\SYSTEM32 in Windows NT
       复制f90SQL modules (f90SQL.f90, f90SQL.mod,共6个) 到fortran compiler 的include/module路径下,我是直接在编译路径里面添加的。


之后就可以调用f90SQL语言进行数据库操作了。 因为使用的是lite版,所以数据库在使用前必须先到Data Sources (ODBC) 里面注册,另外数据库必须是2003或以前的,不支持2007(已测试过)

选用的是access database 和excel表格

从数据库中读取数据:

program main



!load f90SQL modules
use f90SQLConstants
use f90SQL

implicit none

integer(SQLHENV_KIND)::    EnvHndl
integer(SQLHDBC_KIND)::    ConnHndl
integer(SQLHSTMT_KIND)::   StmtHndl
integer(SQLRETURN_KIND)::  iRet
character(len=20)::             AuthorsStr
integer::                  i


!allocate an environment handle
call f90SQLAllocHandle(SQL_HANDLE_ENV, 0, EnvHndl, iRet)

!Set ODBC version, we will be using 3.x in this case
call f90SQLSetEnvAttr(EnvHndl, SQL_ATTR_ODBC_VERSION, &
                      SQL_OV_ODBC3, iRet)

!Allocate a connection handle
call f90SQLAllocHandle(SQL_HANDLE_DBC,EnvHndl, ConnHndl, iRet)

!Open DSN input
call f90SQLConnect(ConnHndl,'input','Admin','',iRet)  ! 数据库DSN名字为input

if (iRet.eq.SQL_SUCCESS .or. iRet.eq.SQL_SUCCESS_WITH_INFO) then

    !Allocate statement handdle
    call f90SQLAllocHandle(SQL_HANDLE_STMT,ConnHndl, StmtHndl, iRet)

    if (iRet.eq.SQL_SUCCESS .or. iRet.eq.SQL_SUCCESS_WITH_INFO) then

        !Bind AuthorStr variable to the only column returned by SQL stmt
        call f90SQLBindCol(StmtHndl,int(1,SQLSMALLINT_KIND),SQL_F_CHAR, &
                           AuthorsStr,0,iRet)

        !Instruct driver to execute statement
        call f90SQLExecDirect(StmtHndl,'SELECT TOP 10 Author from Authors',iRet) !sql的调用语句,Authors为其中的一个表,Author为该表中的栏

        print *,'Name of first 10 authors in input database'

        !loop through result set and print results to screen
        do i=1, 10
            call f90SQLFetch(StmtHndl, iRet)
            if (iRet.ne.SQL_SUCCESS .and. iRet.ne.SQL_SUCCESS_WITH_INFO) &
                exit
            !reformat string in AuthorsStr to make it fortran-compatible
            call f90SQLStrFormat(AuthorsStr,AuthorsStr)
            !print authors
            print *, i, ' ', trim(AuthorsStr)
        enddo

    else

        print *,'Error preparing SQL statement'
        call ShowDiags(SQL_HANDLE_STMT,StmtHndl)

    endif

    !release statement handle
    call f90SQLFreeHandle(SQL_HANDLE_STMT, StmtHndl, iRet)

    !disconnect
    call f90SQLDisconnect(ConnHndl,iRet)

else

   print *,'Error connecting to data source'
   call ShowDiags(SQL_HANDLE_DBC,ConnHndl)

endif


!release connection handle
call f90SQLFreeHandle(SQL_HANDLE_DBC, ConnHndl, iRet)

!release environment handle
call f90SQLFreeHandle(SQL_HANDLE_ENV, EnvHndl, iRet)

stop
end



subroutine ShowDiags(HndlType,Hndl)

!This subroutine prints error diagnostics

!load f90SQL modules
use f90SQLConstants
use f90SQL

implicit none

integer(SQLHANDLE_KIND)::Hndl
integer(SQLSMALLINT_KIND)::HndlType


character(len=6):: SqlState
character(len= SQL_MAX_MESSAGE_LENGTH)::Msg
integer(SQLINTEGER_KIND)::NativeError
integer(SQLSMALLINT_KIND):: iDiag, MsgLen
integer(SQLRETURN_KIND):: DiagRet

iDiag = 1
do while (.true.)
   call f90SQLGetDiagRec(HndlType, Hndl, iDiag, SqlState, NativeError, Msg, MsgLen, DiagRet)
   if (DiagRet.ne.SQL_SUCCESS.and.DiagRet.ne.SQL_SUCCESS_WITH_INFO) exit
   print *,trim(SqlState),',', NativeError,',', Msg(1:MsgLen)
   iDiag=iDiag+1
enddo

end subroutine ShowDiags


如果input是电子表格,只需要将call f90SQLExecDirect(StmtHndl,'SELECT TOP 10 Author from Authors',iRet)换成SELECT TOP 10 Y FROM [Sheet1$] 就可以 了(后面会贴出一个例子)




作者:nosper      发表时间:2007-4-23 23:24:00  

 第1楼  

从电子表格里读取(必须先到Data Sources (ODBC) 里面注册)

program main

  
!load f90SQL modules
use f90SQLConstants
use f90SQLStructures
use f90SQL
  
implicit none
  
integer(SQLINTEGER_KIND),parameter:: MaxStringLen=255
  
integer(SQLHENV_KIND):: EnvHndl
integer(SQLHDBC_KIND):: ConnHndl
integer(SQLHSTMT_KIND):: StmtHndl
integer(SQLRETURN_KIND)::iRet
integer(SQLSMALLINT_KIND)::ColNumber,i
double precision X,Y
character(len=MaxStringLen)  SQLStmtStr
  


  
!allocate an environment handle
call f90SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, EnvHndl, iRet)
  
!Set ODBC version to use (3.x in this case)
call f90SQLSetEnvAttr(EnvHndl, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3, iRet)
  
!Allocate a connection handle
call f90SQLAllocHandle(SQL_HANDLE_DBC,EnvHndl, ConnHndl, iRet)
  

!open a connection to the excel workbook
call f90SQLConnect(ConnHndl, input, ’ ’, ’ ’, iRet)
  
if (iRet.eq.SQL_SUCCESS .or. iRet.eq. SQL_SUCCESS_WITH_INFO) then
  
    !Allocate a statement handle
    call f90SQLAllocHandle(SQL_HANDLE_STMT,ConnHndl, StmtHndl, iRet)

    !Create a select query
    !to retrieve the top 10 records
    SQLStmtStr='SELECT TOP 10 X,Y FROM [Sheet1$]'
  
    !Prepare the SQL query and execute the query
    call f90SQLExecDirect(StmtHndl,trim(SQLStmtStr),iRet)

    if (iRet.eq.SQL_SUCCESS .or. iRet.eq.SQL_SUCCESS_WITH_INFO) then
  
        !Retrieve data

        !bind SQL statement parameters to fortran variables
        ColNumber=1
        call f90SQLBindCol (StmtHndl, ColNumber, SQL_F_DOUBLE,X, f90SQL_NULL_PTR, iRet)
        ColNumber=ColNumber+1
        call f90SQLBindCol (StmtHndl, ColNumber, SQL_F_DOUBLE,Y, f90SQL_NULL_PTR, iRet)
  
        do while (.true.)
            call f90SQLFetch(StmtHndl,iRet)
            if (iRet.ne.SQL_SUCCESS .and. iRet.ne.SQL_SUCCESS_WITH_INFO) then
                if (iRet.eq.SQL_NO_DATA .or. iRet.eq.SQL_NO_IMPLEMENTED) then
                    print *,'End of data set reached'
                else
                    print *,'Error fetching data'
                    call ShowDiags(SQL_HANDLE_STMT,StmtHndl)
                endif
                exit
            endif
            print *,X,Y
        enddo
    else
        print *,'Error executing SQL query'
        call ShowDiags(SQL_HANDLE_STMT,StmtHndl)
    endif

    !release statement handle
    call f90SQLFreeHandle(SQL_HANDLE_STMT,StmtHndl,iRet)

    !Free connection
    call f90SQLDisconnect(ConnHndl,iRet)

else

    print *,'Error opening connection to workbook'
    call ShowDiags(SQL_HANDLE_DBC,ConnHndl)

endif
  
!release connection handle
call f90SQLFreeHandle(SQL_HANDLE_DBC,ConnHndl,iRet)
!release environment handle
call f90SQLFreeHandle(SQL_HANDLE_ENV, EnvHndl, iRet)
  
stop
end


subroutine ShowDiags(HndlType,Hndl)

!This subroutine prints error diagnostics

!load f90SQL modules
use f90SQLConstants
use f90SQL

implicit none

integer(SQLHANDLE_KIND)::Hndl
integer(SQLSMALLINT_KIND)::HndlType


character(len=6):: SqlState
character(len= SQL_MAX_MESSAGE_LENGTH)::Msg
integer(SQLINTEGER_KIND)::NativeError
integer(SQLSMALLINT_KIND):: iDiag, MsgLen
integer(SQLRETURN_KIND):: DiagRet

iDiag = 1
do while (.true.)
   call f90SQLGetDiagRec(HndlType, Hndl, iDiag, SqlState, NativeError, Msg, MsgLen, DiagRet)
   if (DiagRet.ne.SQL_SUCCESS.and.DiagRet.ne.SQL_SUCCESS_WITH_INFO) exit
   print *,trim(SqlState),',', NativeError,',', Msg(1:MsgLen)
   iDiag=iDiag+1
enddo

end subroutine ShowDiags


在input电子表格中[Sheet1$]里有X,Y 两栏




作者:sjohn      发表时间:2007-4-23 23:32:00  

 第2楼  

非常感谢楼主分享,呵,问一下入门级的问题,经过了ODBC是不是会慢一些?
我最近用MySQL自带的C语言接口,还挺好用的。




作者:nosper      发表时间:2007-4-23 23:39:00  

 第3楼  

向数据库中写入数据:
一般有两种方法:
1    INSERT INTO 表名 (X,Y) VALUES ({fn CONVERT(',X ,',SQL_DOUBLE)},{fn CONVERT(',Y ,',SQL_DOUBLE)})   这是向里面插入数据,即每运行一次,向里面增加数据,这样不符合要求,我用的下一种方法
2    Updata,下面有实例 ,这要求数据库开始要存在数据,该操作只是更新

program ExcelUpdateLight



!load f90SQL modules
use f90SQLConstants
use f90SQL

implicit none

double precision, parameter::pi=3.14159d0

!integer(SQLUINTEGER_KIND),parameter::RowsetSize=10
integer(SQLINTEGER_KIND),parameter:: MaxStringLen=255
integer(SQLHENV_KIND):: EnvHndl
integer(SQLHDBC_KIND):: ConnHndl
integer(SQLHSTMT_KIND):: StmtHndl
integer(SQLRETURN_KIND)::iRet
integer(SQLSMALLINT_KIND)::ColNumber,i
double precision X,Y
character(len=MaxStringLen)  SQLStmtStr

!Request from user the name and location of the excel file  
Print *,'Enter a starting value for X (e.g. 0.5):'
read(*,*) X

!allocate an environment handle
call f90SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, EnvHndl, iRet)
  
!Set ODBC version to use (3.x in this case)
call f90SQLSetEnvAttr(EnvHndl, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3, iRet)
  
!Allocate a connection handle
call f90SQLAllocHandle(SQL_HANDLE_DBC,EnvHndl, ConnHndl, iRet)

!open a connection to the excel workbook
call f90SQLConnect(ConnHndl, input,‘’,’’,iRet) !input为数据库

if (iRet.eq.SQL_SUCCESS .or. iRet.eq. SQL_SUCCESS_WITH_INFO) then

     !Set connection attributes to allow read/Update
     !(usually this is set before the connection is established, but there seems to be a bug
     !in the excel ODBC driver that does not recognize this setting if done before connecting)
     call f90SQLSetConnectAttr (ConnHndl, SQL_ATTR_ACCESS_MODE, SQL_MODE_READ_WRITE, iRet)

    !Allocate a statement handle
    call f90SQLAllocHandle(SQL_HANDLE_STMT,ConnHndl, StmtHndl, iRet)

    !loop to generate values of X and Y and insert them into workbook

    do i=1,23
        
        Y=sin(2.0*pi*X)
!book 为表名, X,Y为其中两个栏
        Write(SQLStmtStr,*) 'UPDATE book SET X=',X,', Y=',Y,' WHERE ID=',i
        print *,trim(SQLStmtStr)
        call f90SQLExecDirect(StmtHndl,trim(SQLStmtStr),iRet)
        
        !Check for error when adding rows
        if (iRet.ne.SQL_SUCCESS .and. iRet.ne.SQL_SUCCESS_WITH_INFO) then
            print *,'Error adding new records'
            call ShowDiags(SQL_HANDLE_STMT,StmtHndl)
            exit
        endif

        X=X+0.1D0

    enddo

    !release statement handle
    call f90SQLFreeHandle(SQL_HANDLE_STMT,StmtHndl,iRet)

    !Free connection
    call f90SQLDisconnect(ConnHndl,iRet)

else
    print *,'Error opening connection to workbook'
    call ShowDiags(SQL_HANDLE_DBC,ConnHndl)
endif

!release connection handle
call f90SQLFreeHandle(SQL_HANDLE_DBC,ConnHndl,iRet)
!release environment handle
call f90SQLFreeHandle(SQL_HANDLE_ENV, EnvHndl, iRet)

stop
end


subroutine ShowDiags(HndlType,Hndl)

!This subroutine prints error diagnostics

!load f90SQL modules
use f90SQLConstants
use f90SQL

implicit none

integer(SQLHANDLE_KIND)::Hndl
integer(SQLSMALLINT_KIND)::HndlType


character(len=6):: SqlState
character(len= SQL_MAX_MESSAGE_LENGTH)::Msg
integer(SQLINTEGER_KIND)::NativeError
integer(SQLSMALLINT_KIND):: iDiag, MsgLen
integer(SQLRETURN_KIND):: DiagRet

iDiag = 1
do while (.true.)
   call f90SQLGetDiagRec(HndlType, Hndl, iDiag, SqlState, NativeError, Msg, MsgLen, DiagRet)
   if (DiagRet.ne.SQL_SUCCESS.and.DiagRet.ne.SQL_SUCCESS_WITH_INFO) exit
   print *,trim(SqlState),',', NativeError,',', Msg(1:MsgLen)
   iDiag=iDiag+1
enddo

end subroutine ShowDiags




作者:nosper      发表时间:2007-4-23 23:40:00  

 第4楼  

向电子表格里写入同上  



作者:nosper      发表时间:2007-4-23 23:47:00  

 第5楼  

目前好像网站上只能下载到lite版,而做这个的canaimasoft好像2002年已经不做了,所以prof版很难找的到。
lite版会有限制,但基本的读写还是可以的

还有一点要注意,就是lite版里面用的1 个字节的指针,一次读写不能超过128个数据

但是如果想要读多于128个数据,可以:

query(1)='SELECT * FROM MyTable WHERE RID<=100'
query(2)='SELECT * FROM MyTable WHERE RID>100 and RID<=200'
query(3)='SELECT * FROM MyTable WHERE RID>200 and RID<=300'
Open an environment handle
do i=1,3
Open a database connection (Using f90SQLConnect)
Open a statement handle (Using f90SQLAllocHndle)
Execute query(i) (Using f90SQLExecDirect)
Fetch the result set of query(i)
Close the statement handle
Close the Connection
enddo
Close the environment handle




作者:nosper      发表时间:2007-4-23 23:49:00  

 第6楼  

以上就是在fortran中直接调用数据库和电子表格的方法。


期待大家给出其他的方法!  



作者:f2003      发表时间:2007-4-23 23:51:00  

 第7楼  

本帖收藏中
      



作者:nosper      发表时间:2007-4-24 0:03:00  

 第8楼  

在补充一点:
在project link的时候,必须要把f90SQL.lib加入到项目中,否则会出错  



作者:nosper      发表时间:2007-4-24 0:10:00  

 第9楼  

引用:
非常感谢楼主分享,呵,问一下入门级的问题,经过了ODBC是不是会慢一些?
我最近用MySQL自带的C语言接口,还挺好用的。



    这个好像差别不是很大,但具体怎么样,我也不清楚,f90SQL -prof有这个可以这样,在程序中调用drive,在读数据库,不需要注册,好像没得下,
如果谁有的话,可以传我一个:)


  MySQL自带的C语言接口,是在c语言中直接调用数据库么?这比较有意思,可以具体讲一下么?  



作者:臭石头雪球      发表时间:2007-4-24 8:10:00  

 第10楼  

引用:
非常感谢楼主分享,呵,问一下入门级的问题,经过了ODBC是不是会慢一些?
我最近用MySQL自带的C语言接口,还挺好用的。



我想可能会慢一些。

但是经过了 ODBC ,可以方便以后更换数据库类型。我个人是这么认为的。。  



作者:htczhy      发表时间:2007-4-24 10:40:00  

 第11楼  

在vf6.5所带的例子中有一个是关于向EXCEL中写入数据的,默认如下目录。
C:\Program Files\Microsoft Visual Studio\DF98\SAMPLES\ADVANCED\COM\AUTODICE
关键是要写一个与excel关联的模块文件,例子中的模块说明如下:
! This module contains the Automation interfaces of the objects defined in
! D:\Program Files\Microsoft Office\Office\EXCEL8.OLB
! Generated by the Fortran Module Wizard on xx/xx/xx
如果需要其他功能也可以通过vf6.5自带的Fortran Module Wizard自动生成。  



作者:臭石头雪球      发表时间:2007-4-24 11:37:00  

 第12楼  

楼上的办法也是可行的。

我的 IVF 一直没有找到相关的模块向导。。。  



作者:mltx      发表时间:2007-4-24 12:55:00  

 第13楼  

赞搂主的钻研精神和大气态度!!!  



作者:sjohn      发表时间:2007-4-24 17:03:00  

 第14楼  

如果是在Linux下安装二进制的MySQL,那么需要加上devel包。如果是windows下安装,要怎么处理我不能确定,但肯定能行。非商业版免费,可开源。

帮助文件里面提供了C语言的API接口,网上也有很多例子。在C语言中不需要显式地和ODBC连接。

初始化函数的原型似乎有两个版本,我下载的版本刚好跟官方帮助的不一致。官方例子是这样的。(呵呵,应楼主要求,虽然在这里发大段的C代码不太合适)

/* Copyright (C) 2000 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"

static void change_user(MYSQL *sock,const char *user, const char *password,
            const char *db,my_bool warning)
{
  if (mysql_change_user(sock,user,password,db) != warning)
  {
    fprintf(stderr,"Couldn't change user to: user: '%s', password: '%s', db: '%s':  Error: %s\n",
        user, password ? password : "", db ? db : "",
        mysql_error(sock));
  }
}


int main(int argc, char **argv)
{
  MYSQL *sock;

  if (!(sock=mysql_init(0)))
  {
    fprintf(stderr,"Couldn't initialize mysql struct\n");
    exit(1);
  }
  mysql_options(sock,MYSQL_READ_DEFAULT_GROUP,"connect");
  if (!mysql_real_connect(sock,NULL,NULL,NULL,NULL,0,NULL,0))
  {
    fprintf(stderr,"Couldn't connect to engine!\n%s\n",mysql_error(sock));
    perror("");
    exit(1);
  }
  sock->reconnect= 1;

  if (mysql_select_db(sock,"test"))
  {
    fprintf(stderr,"Couldn't select database test: Error: %s\n",
        mysql_error(sock));
  }

  change_user(sock,"test_user","test_user","test",0);
  change_user(sock,"test",NULL,"test",0);
  change_user(sock,"test_user",NULL,"test",1);
  change_user(sock,"test_user",NULL,NULL,1);
  change_user(sock,"test_user","test_user","mysql",1);

  mysql_close(sock);
  exit(0);
  return 0;
}


另外Fortran语言可以和C语言混编——虽然不成ISO标准,但是windows,Linux/Unix下都已经在事实上支持了,可移植性还挺好——那么尽管MySQL没有提供Fortran接口,楼主也可以用这个方案了。

呵呵,人帮我,我帮人,分享中进步,进步中分享。




作者:sjohn      发表时间:2007-4-24 17:14:00  

 第15楼  

上面那个例子光连接了数据库,还什么都没有做
来一个插入和查询的例子。
数据库的配置很简单,尤其windows底下,有可视化的wizard.

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"

#define SELECT_QUERY "select name from test where num = %d"
#define INSERT_QUERY "insert into test (name,num) values ('item %d', %d)"

int main(int argc, char **argv)
{
  int    count, num;
  MYSQL mysql,*sock;
  MYSQL_RES *res;
  char    qbuf[160];

  if (argc != 3)
  {
    fprintf(stderr,"usage : select_test <dbname> <num>\n\n");
    exit(1);
  }

  mysql_init(&mysql);
  if (!(sock = mysql_real_connect(&mysql,NULL,0,0,argv[1],0,NULL,0)))
  {
    fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql));
    perror("");
    exit(1);
  }
  mysql.reconnect= 1;

  num = atoi(argv[2]);
  count = 0;
  while (count < num)
  {
    sprintf(qbuf,INSERT_QUERY,count,count);
    if(mysql_query(sock,qbuf))
    {
      fprintf(stderr,"Query failed (%s)\n",mysql_error(sock));
      exit(1);
    }
    count++;
  }
  count = 0;
  num = atoi(argv[2]);
  while (count < num)
  {
    sprintf(qbuf,SELECT_QUERY,count);
    if(mysql_query(sock,qbuf))
    {
      fprintf(stderr,"Query failed (%s)\n",mysql_error(sock));
      exit(1);
    }
    if (!(res=mysql_store_result(sock)))
    {
      fprintf(stderr,"Couldn't get result from %s\n",
          mysql_error(sock));
      exit(1);
    }
#ifdef TEST
    printf("number of fields: %d\n",mysql_num_fields(res));
#endif
    mysql_free_result(res);
    count++;
  }
  mysql_close(sock);
  exit(0);
  return 0;                    /* Keep some compilers happy */
}




作者:nosper      发表时间:2007-4-24 18:24:00  

 第16楼  

谢谢sjohn 先生了

我在好好研究下mysql:)




作者:nosper      发表时间:2007-4-24 18:58:00  

 第17楼  

引用:
在vf6.5所带的例子中有一个是关于向EXCEL中写入数据的,默认如下目录。
C:\Program Files\Microsoft Visual Studio\DF98\SAMPLES\ADVANCED\COM\AUTODICE
关键是要写一个与excel关联的模块文件,例子中的模块说明如下:
! This module contains the Automation interfaces of the objects defined in
! D:\Program Files\Microsoft Office\Office\EXCEL8.OLB
! Generated by the Fortran Module Wizard on xx/xx/xx
如果需要其他功能也可以通过vf6.5自带的Fortran Module Wizard自动生成。


  我刚才在vf6.5里面好像并没有找到这个文件
   能把该文件上传到该网站上么? 这样的话,大家可以下载::)

  顺便问下,Fortran Module Wizard 这个怎么用呀,
  在ivf9.1  怎么没找到这个亚?
回复

使用道具 举报

发表于 2007-5-29 13:03 | 显示全部楼层

回复 #4 心灯 的帖子

作者:南洋云贵      发表时间:2007-4-30 17:49:00  

 第18楼  

顶  



作者:htczhy      发表时间:2007-5-8 11:51:00  

 第19楼  

你装的版本可能是简化版,如果标准版或专业版都有示例程序。
不知道本站能否上传文件,放到mofile里。
匿名提取文件连接 http://pickup.mofile.com/5105075293636900  
     或登录Mofile,使用提取码 5105075293636900 提取文件
以下为 vf6.5里帮助里的Fortran Module Wizard帮助

To run the Visual Fortran Module Wizard, choose the Tools menu item Fortran Module Wizard. The module wizard asks a series of questions, including the name and type of the object as well as certain characteristics. If you have not already obtained the object's characteristics, see The Role of the Module Wizard.

The Visual Fortran Module Wizard presents a series of dialog boxes that allow you to select the type of information needed.

An object's type information contains programming language independent descriptions of the object's interfaces. Depending on the implementation of the object, type information can be obtained from the running object (see Automation Object below) or from a type library.

A type library is a collection of type information for any number of object classes, interfaces, and so on. A type library can also be used to describe the routines in a DLL. You can store a type library in a file of its own (usually with an extension of .TLB) or it can be part of another file. For example, the type library that describes a DLL can be stored in the DLL itself.

After you start the Module Wizard (Tools menu, Fortran Module Wizard), a dialog box requests the source of the type information that describes the object you need to use. You need to determine what type of object it is (or DLL) and how it makes its type information available. The choices are:

Automation Object
Type Library containing Automation information
Type Library containing COM interface information
Type Library containing DLL information
DLL containing type information
Object servers typically contain the type library in the same file (either .dll or .exe) as the object implementation.

Many objects implement dual interfaces, which supports both COM and Automation. For an object that supports dual interfaces, you can choose either of these options:

Type Library containing Automation information
Type Library containing COM interface information
The COM object interfaces tend to be more efficient (better run-time performance).

ActiveX controls implement an Automation interface. When using an ActiveX control, choose the Type Library containing Automation interface information option.

Most DLLs do not provide a type library that describes the programming interfaces in the DLL, and therefore cannot be used by the Module Wizard.

For these reasons, the Automation Information (second option) or COM Interface Information (third option) are the most commonly used.

The following initial screen appears after you select the Visual Fortran Module Wizard:

Initial Module Wizard Screen



After you select one of the five choices, one of two different screens will appear depending on the selection made. The "Module Name" in the initial Module Wizard screen is used as the name of the Fortran module being generated. It is also used as the default file name of the generated source file.

If You Select Automation Object
If you select Automation Object, the following screen appears:

Application Object Screen







作者:htczhy      发表时间:2007-5-8 11:52:00  

 第20楼  

Microsoft recommends that object servers provide a type library. However some applications do not, but do provide type information dynamically when running. Use this option for such an application. Enter the name of the application, name of the object, and version number. The version number is optional. If you do not specify it, you will get the latest version of the object. Note that this method only works for objects that provide a programmatic identifier (ProgID). ProgIDs are entered into the system registry and identify, among other things, the executable program that is the object's server.

After entering the information and pressing the Generate button, the Fortran Module Wizard asks you for the name of the source file to be generated. It then asks COM to create an instance of the object identified by the ProgID that the wizard constructs using the supplied information. COM starts the object's server if it needs to do so. The wizard then asks the object for its type information and generates a file containing Fortran modules.

If You Select Other Options
After selecting any of the remaining options in the initial screen and press the "Next" button, the Module Wizard displays the following screen:

Type Library Screen



Choose the type library (or file containing the type library), and optionally specific components of the type library.

At the top of the dialog box is a combo box that lists all of the type libraries that have been registered with the system. You will notice a number of different file extensions, for example, .OLB (object libraries) and .OCX (ActiveX controls). Select a type library from the list or press Browse to find the file using the standard "Open" dialog box. Once you have selected a type library press the Show button to list the components you can select from the type library. By default, the Fortran Module Wizard will use all of the components. Optionally, you can select the ones desired from the list.

After entering the information and pressing the "Generate" button, the Fortran Module Wizard asks you for the name of the source file to be generated. It then asks COM to open the type library and generates a file containing Fortran modules.

The Fortran Module Wizard also has a command-line interface. The MODWIZ command has the following form:

MODWIZ [options] typeinfo-name
For a list of MODWIZ command options and an explanation of typeinfo-name, type the following command in a Fortran Command Prompt (available from the Visual Fortran program folder):

   MODWIZ /?





作者:f2003      发表时间:2007-5-8 12:45:00  

 第21楼  

赞楼主的黑客精神。也赞那位用C语言举例的朋友。我也有一个想法,嘿嘿:

跟sql数据库交互最好还是编出一个客户机程序,通过mysql的服务器进程(名为mysqld_safe)来操作数据库,而不是直接读写数据库文件。但mysql有php,C接口,没有Fortran接口。

fortran2003引入一项功能叫“ISO_C_BINDING”,即与C交互。在声明后一个函数为C函数后,fortran2003源程序中可以调用。用这种方法可以让fortran程序直接与mysqld_safe进程通信,创立/删除,查询,读写数据库。

几周之内将公布的intel fortran 10.0版本将完全实现“ISO_C_BINDING”。目前的9.1版本也已经部分实现。

至于非mysql的其他数据库,可以利用mysql的强大的导入能力。  



作者:nosper      发表时间:2007-5-8 12:54:00  

 第22楼  


黑客精神?

过了,没那么严重! 只是研究了一下fortran与数据库的连接问题。

现在看来这方法愈来愈多了

呵呵
回复

使用道具 举报

 楼主| 发表于 2007-5-30 16:41 | 显示全部楼层
按照数据库的方式操作的话是否涉及到odbc的问题?
回复

使用道具 举报

发表于 2007-5-30 19:18 | 显示全部楼层
我也没有仔细看上面的讨论,不过搜索了一下上面的帖子,odbc有关的讨论是有的。
回复

使用道具 举报

 楼主| 发表于 2007-5-31 09:15 | 显示全部楼层


很多对数据库的操作都是通过odbc来实现的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-17 19:37 , Processed in 0.060908 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表