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

发布于 2024-08-26 14:51:51 字数 183 浏览 2 评论 0原文

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

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

我可以使用类似 Head[conn] 的东西吗?

How can I check if DatabaseLink`OpenSQLConnection was successful? My code is as follows

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

Can I use something like Head[conn]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恬淡成诗 2024-09-02 14:51:51

成功连接的返回值将有头 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 的返回值,也可以选择使用 检查[...]以捕获并处理生成的消息。正如您所猜测的,您可以使用 Head[returnvalue] 来确保返回值的头部不等于 OpenSQLConnection

The return value for successful connection will have head SQLConnection (in the DatabaseLink context)

More generally:

OpenSQLConnection returns $Failed when the connection failed for whatever reason:

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 

... and unevaluated when its arguments were not of the proper form:

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

Out[28]= OpenSQLConnection[Sin[x]]

Therefore, you can look for a return value of $Failed and optionally also use Check[...] to trap and handle messages that were generated. As you guessed, you can use Head[returnvalue] to make sure the head of the return value does not equal OpenSQLConnection.

酸甜透明夹心 2024-09-02 14:51:51

这并不完全是您问题的答案,但这是我为了可靠地连接到我的数据库所做的事情:

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"]
];

This is not exactly an answer to your question, but this is what I do to connect reliably to my database:

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"]
];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文