2
$\begingroup$

When creating a SQLConnection you can give it a name with the "Name" option. The connections are also added to the output of the SQLConnections function.

OpenSQLConnection[ 
  JDBC["HSQL(Standalone)", ToFileName[{$TemporaryDirectory, "SE Question"}, "Ex1"] ], 
  	"Name" -> "Example1"];
OpenSQLConnection[ 
  JDBC["HSQL(Standalone)", ToFileName[{$TemporaryDirectory, "SE Question"}, "Ex2"] ], 
    "Name" -> "Example2"];
SQLConnections[]

Mathematica graphics

Later on I would like to select a particular connection from SQLConnections based on its name. However, SQLConnectionInformation does not have "Name" as supported property

SQLConnectionInformation[#, "Name"] & /@ SQLConnections[]

DatabaseLink`JDBC::error

{$Failed, $Failed}

and there does not seem to be a name property on the connections

Through@SQLConnections[]["Name"]

Mathematica graphics

and SQLConnections does not take a name parameter

SQLConnections["Example1"]
SQLConnections["Example1"]

How do I select a connection from SQLConnections by its "Name" property?

Clean-up:

CloseSQLConnection /@ SQLConnections[];

Update

WRI Support have provided a method to get at the "Name". I have also requested that a SQLConnection object support "Name" and "Id" as properties.

"Name" is an option. Therefore,

"Name" /. Options[conn, "Name"]

will provide conn's "Name" option.

For the "Id" property WRI said it is the third part of its FullForm. Therefore,

conn[[3]]

will provide conn's "Id" property. This method seems particularly risky since there is no guarantee that this position will contain this property in future versions.

Since you can see these values in the Format form of a SQLConnection in a notebook I think it is reasonable to expect to be able to get at these values without workarounds. Particularly since there is no other means of selecting a connection out of the SQLConnections list. Fingers are optimistically crossed.

$\endgroup$
2
$\begingroup$

The Name option is not unique, so we could be more than one result, if we ask all connections with a particular name. Try this:

connName = "Example2";
Select[SQLConnections[],
 StringMatchQ["Name" /. Cases[Flatten[List @@ #], _Rule],
   connName] &
 ]

The Select should give us all of connections with name "Example2".

How it works: We take a single connection and replace the head with List, then Flatten the list. We are interested only in the Rules associated with connection. We apply all of the replacement rules to "Name", which gives us the string name of the connection, assuming there is one. If that string matches the one we want, we select the connection.

$\endgroup$
  • $\begingroup$ Nice work. I didn't think to deconstruct the connection. +1 while we wait for other solutions. $\endgroup$ – Edmund Apr 13 '17 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.