how to get connection information in BASE with a macro?

I want to use my Mysql connection info to create an external link in a macro. How to get current connection info? (ip, port, user, password)

LO 5.1.6.2, ubuntu 64

More descriptive. I have an odb that connects to a MySQL server. In the odb many buttons run several actions (ex.open php files in browser, using the connection information of the odb passing as GET/PUT parameters username and password). User is able to change MySQL credentials with the menu selection Edit->Database->Properties. But he buttons include code that user is not familiar with.

Hello,

Your question is too broad to give details on this answer in a forum. To establish a connection, there is an example in the “AndrewBase” document found on this page. See section 8.6.5 of that document for more information. Once you make a connection you need to send a request to the DB for what is wanted and the return is a ‘Result Set’. Information about Result Sets and using data returned can be found on this AOO Wiki starting here.

As for How to get current connection info? (ip, port, user, password), that is determined on the user side when MySQL was installed. If you don’t have this info you need to get it from the DB administrator.

Edit 10/08/2017:

Sorry for misunderstanding your original question but the answer does contain relevant information. The answer you provided has given better information and would be nice as part of the question.

From this it seems you want access to the same information which is under the menu selection Edit->Database->Properties. Three ways I can think of to get this information.

Locally, this information is stored within one of the files zipped in the .odb - content.xml file. You would need to extract the data from this portion (beginning of section sample):

From the connected .odb you can use a macro, as previously stated, by creating a connection and retrieving the Meta data:

	oContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
oDB = oContext.getByName(sDBName)
oCon = oDB.getConnection(sUserName,sUserPW) 

REM Use above three line for connection or the previously mentioned connection from section 8.6.5

oMeta = oCon.getMetaData()
connMeta = oMeta.getConnection().getMetaData()
print connMeta.URL
print connMeta.UserName

sDBName is either the registered name or the .odb URL.

The connMeta.URL you can retrieve will be something like: sdbc:mysql:mysqlc:192.135.214.251:3306/YOUR_DB

Don’t know of any way to retrieve the password as you request.

The last way ThisDatabaseDocument.DataSource.URL retrieve the same info as in the Meta data.

Even after all that, it is not clear as to why you need to change all the code. Changing the connection is all you should need. I’m not even certain what I presented is what you are looking for.

1 Like

After a lot of browsing, I finally found that you can access the current datasource with

data_source = ThisDatabaseDocument.DataSource

or with:

data_source = thisComponent.DataSource

This object has several attributes, incuding Name (the odb file URL), User, Password , URL (this contains the database driver, the host ip or domain name, the port and the database name). You can access these properties like this:

data_source.Name
data_source.Password
data_source.URL
data_source.User

I hope this helps

1 Like

The answer assumes I want to create an odb and not to use the connection for my own purposes. Ex. I want to use the same info to connect to the same server or to create a temporary document, not a BASE specific, that will use that info. Andrew’s document is somehow irrelevant since 8.6.5 is about how to use a known connection info for creating a new connection and get some data. More explanation on why I specifically want this info: Occasionally the connection specifics are changed (either the ip or the port or even the user or password) so I recreate the connection, after opening the odb with error, by using the menus. After that “starting” change I have to go to every piece of code for buttons, in Basic and in python, and change that info and rebind python code. (I will edit the question and remove the “use” part.)

Sorry, but the answer does not ‘assume’ what you state at all. The edit in my answer shows that by using the original answer, you would request the information wanted (in this case the Meta data), thus getting the connection info. My edit also shows two other ways to obtain this information.