Link to home
Start Free TrialLog in
Avatar of formadmirer
formadmirerFlag for United States of America

asked on

VFP can connect to MySQL, yes?

Hi all. Have a form where I enter the connection details for mysql. Everything works great and I can query the db fine.

I will have a need from time to time to connect to a different db and I was wondering if there is a simple way to verify that I can actually connect to the db after entering new connection details - a 'test connection' button that will execute a simple query and return 1 if connected, 0 if the connection failed - something like that.

I just need the query details.
SOLUTION
Avatar of peterhupp
peterhupp
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Cyril Joudieh
Cyril Joudieh
Flag of Lebanon image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of formadmirer

ASKER

I'm splitting the points between you two.
peterhupp I wasn't able to get your code to work with MySQL, could be a difference between it and MS SQL, I don't know, but had it worked your code was what I was looking for.

CaptainCyril, I already have a working connection, I was just looking for a simple 1 or 0 if I was connected successfully or not. But, that's good code so you've got to get some points for it.

Anyway, I found out that by simply sending
lcQuery = "SELECT VERSION()"
into my connection string I can easily verify if I have successfully connected or not, which is what I wound up using. Thanks though for the help.
the reason why I used a stored procedure is that it returns a value instead of a cursor.
Actually you don't need to test. SQLStringConnect will tell you if it succeeded connecting. So Cyrils code is totally sufficient. If the database doesn't exist or is shut down you will not get a connection, if you have a connection, you have a connection.

Bye, Olaf.
You get a failed/successful return on each call. It could be that the database is not there, server is down or whatever. Also when you run a query there could be a bug in the syntax. VFP can return you the error in details if you wish or if you are satisfied with True/False it can do that as well.
As a side note: Most any other Database also allows a syntax of SELECT expression without any FROM clause. This looks a bit like VFPs SELECT command to select a workarea, but that' VFP exclusive. What it does is, it returns a scalar value in a single record cursor to VFP.

This way you can also get Server system time for example in T-SQL (MS SQL SERVER) with SELECT Getdate() and in MySQL you can get it via SELECT NOW(),CURDATE(),CURTIME()
You can also SELECT VERSION() in MySQL to get it's version number.

Bye, Olaf.