Wolfram mathematica 如何检查OpenSQLConnection是否成功(在Mathematica中)?

Wolfram mathematica 如何检查OpenSQLConnection是否成功(在Mathematica中)?,wolfram-mathematica,Wolfram Mathematica,如何检查DatabaseLink`OpenSQLConnection是否成功?我的代码如下 conn = OpenSQLConnection[JDBC["hsqldb", "file"], "Name"-> "test"]; 我可以使用Head[conn]之类的工具吗?这并不能完全回答您的问题,但我要做的是可靠地连接到数据库: Needs["DatabaseLink`"]; CloseSQLConnection[conn]; TimeConstrained[ conn=Ope

如何检查DatabaseLink`OpenSQLConnection是否成功?我的代码如下

conn = OpenSQLConnection[JDBC["hsqldb", "file"], "Name"-> "test"];

我可以使用Head[conn]之类的工具吗?

这并不能完全回答您的问题,但我要做的是可靠地连接到数据库:

Needs["DatabaseLink`"];

CloseSQLConnection[conn];

TimeConstrained[
    conn=OpenSQLConnection[ JDBC["mysql","localhost:3306/mydb"],
                            "Username"->"myuser",
                            "Password"->"mypw"],
    5,
    CloseSQLConnection[conn];
    conn=OpenSQLConnection[ JDBC["mysql","localhost:3306/mydb"],
                            "Username"->"myuser",
                            "Password"->"mypw"]
];

成功连接的返回值将具有head
SQLConnection
(在
DatabaseLink
上下文中)

更一般地说:

OpenSQLConnection
在连接因任何原因失败时返回
$Failed

In[25]:= OpenSQLConnection[JDBC["mysql", "localhost:3306/foo"], 
   "Username" -> "foo", "Password" -> "bar"]

During evaluation of In[25]:= JDBC::error: Access denied for user 'foo'@'localhost' (using password: YES) >>

Out[25]= $Failed 
。。。当其论点形式不正确时,不进行评估:

In[28]:= OpenSQLConnection[Sin[x]]

Out[28]= OpenSQLConnection[Sin[x]]
因此,您可以查找
$Failed
的返回值,还可以选择使用
Check[…]
捕获和处理生成的消息。正如您所猜测的,您可以使用
Head[returnvalue]
来确保返回值的Head不等于
OpenSQLConnection