Matlab 创建新的 Microsoft Access 数据库文件 *.accdb

Matlab create new Microsoft Access Database file *.accdb

我已使用以下代码模式访问我的 *.accdb 文件:

accdb_path='C:\path\to\accdb\file\wbe3.accdb';
accdb_url= [ 'jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='''';DBQ=' accdb_path ];
conn = database('','','','sun.jdbc.odbc.JdbcOdbcDriver',accdb_url);

如果我想 创建 一个新的 *.accdb 文件,我该怎么做?网上有很多关于如何连接的内容,但我还没有找到如何创建 *.accdb 文件本身。

以防万一,我希望能够执行 SQL 92 语法。我正在使用 Matlab 2015b。我不想使用 Matlab GUI 来探索数据库。

实际上,您尝试做的事情可能很难实现。它可能需要通过 ActiveX 控件访问的直接接口,我什至不确定它是否可以完成。网络似乎缺乏有关 Access 互操作性的可靠信息库。

我可以建议您的一个快速解决方法是手动创建一个空的 ACCDB 文件,您可以将其用作模板,然后在必须创建新数据库时复制它:

conn = CreateDB('C:\PathB\wbe3.accdb');

function accdb_conn = CreateDB(accdb_path)
    status = copyfile('C:\PathA\template.accdb',accdb_path,'f');

    if (status)
        accdb_url = ['jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=' accdb_path];
        accdb_conn = database('','','','sun.jdbc.odbc.JdbcOdbcDriver',accdb_url);
    else
        accdb_conn = [];
        error(['Could not duplicate the ACCDB template to the directory "' accdb_path '".']);
    end
end

以下示例基于 Tommaso 的回答,它提供了用于复制空 *.accdb 文件并连接到副本的代码。基于一个下午的试验、错误和对 web/help 的细读,我对其进行了扩展以创建数据库 table 并将 Matlab table 导出到其中。我还嵌入了注释,显示需要修改的地方,大概是由于我的 2015b 旧版 Matlab、错误捕获结构和文件副本中的警告。

srcPath = [pwd '/emptyFile.accdb'];    % Source
tgtPath = [pwd '/new.accdb'];          % Target
cpyStatOk = copyfile( srcPath, tgtPath );
   % No warning B4 clobber target file

if cpyStatOk
   accdb_url= [ ...
      'jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='''';DBQ=' ...
      tgtPath ];
   conn = database('','','','sun.jdbc.odbc.JdbcOdbcDriver',accdb_url);
else
   error('Couldn''t copy %s to %s',srcPath,tgtPath);
end % if cpyStatOk

try

   % conn.Execute(['CREATE TABLE tstMLtbl2accdb ' ... Not for 2015b
   curs = conn.exec(['CREATE TABLE tstMLtbl2accdb ' ...
                     '( NumCol INTEGER, StrCol VARCHAR(255) );']);

   if ~isempty( curs.Message )
      % fprintf(2,'%s: %s\n',mfilename,curs.Message);
      error('%s: %s\n',mfilename,curs.Message);
         % Trigger `catch` & close(conn)
   end %if

   % sqlwrite( conn, 'tstMLtbl2accdb', ...Not supported in 2015b
   datainsert( conn, 'tstMLtbl2accdb', {'NumCol','StrCol'}, ...
      table( floor(10*rand(5,1)), {'abba';'cadabra';'dog';'cat';'mouse'}, ...
             'VariableNames',{'NumCol','StrCol'} ) );

catch xcptn

   close(conn)
   fprintf(2,'Done `catch xcptn`\n');
   rethrow(xcptn);

end % try

%
%  Other database manipulations here
%

close(conn)
disp(['Done ' mfilename]);

这对我自己有很大的教育意义,我希望它对其他考虑使用 SQL 作为代码量更大的 Matlab 对应关系数据库操作的替代方法的人有用。有了这么多的开销,我不得不说对驻留在 Matlab 工作区中的数据执行 SQL 操作没有吸引力,除非确实需要关系数据库查询引擎的超级优化。

对于那些精通 Access 接口的人,您对 datainsert 函数的字段名称参数的目的的评论将不胜感激。 它被称为 colnamesdocumentation 中。根据测试,字段名称和列数必须在 Access 中的现有目标 table 和 Matlab 中的源 table 之间匹配。所以字段名称参数似乎没有任何作用。帮助文档并不是那么有用。

AFTERNOTE: 我已经根据 TMW 中的示例为 colnams 参数编写了一个 "specification"。 TMW 已确认此解释:

The colnames argument tells the external database environment the names and order of fields of the data container supplied via the data argument. These field names are used to match the fields of the transferred data with fields in the table tablename residing within the external database environment. Because of this explicit name matching, the order of the fields in data do not have to match the order of the fields in tablename.

如果我发现上述经验行为有任何偏差 "specification",我将更新此答案。