Monday, 4 May 2015

Connecting VFP To MySQL Database

Here's how to access a MySQL database from VFP.

First, you need to download an ODBC connector to MySQL from this link. Choose between 32 or 64 bit version.

Next is to connect to it via DSN or DSNless connection.
Here's an example of a connection string via a DSNLESS (ODBC) connection. Please note that the lines starting with an asterisk (*) are comments.

lcStringConnect = 'Driver=MySQL ODBC 5.2 Unicode Driver;Server=localhost;uid=root;pwd=password'

*---This part here sets connection errors hidden.

SQLSetprop(0, "DispLogin", 3)
SQLSetprop(0, "DispWarnings", .F.)

*---This is where the actual connection happens.

lnConnect = Sqlstringconnect(m.lcStringConnect)

*---The variable lnConnect holds the number returned by the function
*---SQLSTRINGCONNECT(), if the value is greater than zero (0) then
*---no error happened during the connection attempt. 

If m.lnConnect > 0
  *--- List the databases in MYSQL.
   SQLEXEC(m.lnconnect,'show databases','testcur')

=SQLDisconnect(m.lnConnect) && Close Connection

If the MySQL Database is remote (or online), you will need to specify the IP address as a server name in the connection string.

1 comment:

Related Posts Plugin for WordPress, Blogger...