Matlab's JDBC/ODBC SQL behaviour differs from Access 2010

时间:2015-07-28 17:04:53

标签: matlab ms-access

This is a hybrid question & public service announcement. The basic question is whether there are convenient and/or efficient workarounds to the limitations listed below. I spent half the morning discovering that one cannot just transplant SQL code from Access to Matlab. I've boiled it down to 3 points so far.

  1. Can't use double quotes in SQL statements to avoid collision with Matlab's string delimiter. The Matlab code for the SQL code strings can become quite complicated, especially if the SQL strings already use repeated single-quotes to represent a single quote within a string constant.
  2. Must always specify a source table from which to query. What won't work is "SELECT #2015-07-28#". One basically needs to create a 1-row dummy table.
  3. Must always select at least one field in the table being queried. An asterisk does not seem to suffice.

The above limitations do not exist when submitting SQL code using the Access Query Designer (either in SQL 92 mode or not), nor do these limitations exist when submitting SQL code using VBA via CurrentProject.Connection.Execute.

Hopefully, this saves someone else some time in learning about these differences. And if anyone has found a workaround, that would be appreciated. Note that the above is in the context of using the JDBC/ODBC bridge (3rd of 3 illustrated configurations in the drivers documentation. The database toolbox documentation for directly connecting to an Access file using code (rather than setting up a data source using the GUI) only describes a code pattern that uses the JDBC/ODBC bridge. This is described in Example "Connect to Microsoft Access Using a File DSN" in the "Connect to database" page. I'd like to stick to this approach because I want to quickly be able to directly specify the source *.accdb file without jumping through GUI hoops of setting up a data source.

I've posted this to:

1 个答案:

答案 0 :(得分:0)

虽然我没有将Matlab连接到Access,但我已将Access数据库连接到PHP,Python,SAS,R甚至Excel,而没有任何引号或星号问题。至于查询中所需的源表,仅仅是由于Jet / ACE SQL方言,因为每种方言都有其特定的规则。但是上面的查询应该适用于此类日期的单行,单列输出。 SQL Server不需要源表,但Oracle需要。访问日期采用MM/DD/YYYY格式,条件语句中需要#。 MySQL使用YYYY-MM-DD格式的日期,需要单引号。

您使用的是Windows安装的Jet / ACE ODBC驱动程序吗?请注意,这些驱动程序是任何应用程序外部的软件(不属于Access),并且可以由任何连接到任何数据源的客户端使用。除此之外 - 许多人都不知道Access'后端数据库引擎,Jet / ACE实际上是一种Windows技术,不限于Access;没有安装Access的PC用户仍然可以使用此引擎。因此,connection strings是ODBC调用的标准。正如我所见,你可以将相同的原则应用于Matlab database connections

据说,我的建议/解决方法:

  • 正确学习连接数据库的SQL方言并检查查询是否运行;甚至可以根据需要进行优化或重写
  • 尝试转义不符合Matlab字符串的各种标点符号或使用ASCII对应项:chr(34)进行双引号; chr(39)单引号;星号chr(42)
  • 如果在Matlab字符串中限制select语句,则从查询创建临时表(在Matlab外部使用make-table query SELECT * INTO newtable FROM query),然后连接到这个新表
  • 通过用户触发器或使用中间编码语言(VBA,Python,C#) 然后,命令行连接到数据库并导出所需的数据集 自动化Matlab导入
  • 将查询导出为平面文件格式(csv,txt等)以进行Matlab导入