Reputation: 103
I converted visual foxpro DBF to mysql and I need to connect the vfp code directly to the mysql database.Please help.thanks
Upvotes: 1
Views: 8209
Reputation: 3937
There are a number of ways to access data from a server database from VFP, but I'm not sure you'd call any of them connecting directly to the server database. Specifically, you can't use commands like USE and REPLACE directly against a server database, nor can you bind form controls directly to the server data.
Whichever approach you use, you pull some data from the server into a cursor in VFP, operate on the cursor, and then, if appropriate, save changes back to the server.
The three main approaches are:
1) Remote views--with this approach, you store SQL queries in a database. To run the query and pull data from the server, you USE the remote view. 2) SQL Pass-Through (SPT)--with this approach, you use the SQLEXEC() command to send SQL commands to the server, and get results. 3) CursorAdapter class--with this approach, you set up a class that describes how you want to get data from the server, and when you call the CursorFill() method, you get a cursor full of data.
You should choose one of these approaches and use it throughout your application. They each have pros and cons.
To get you started, since you'll probably want to use SPT for testing purposes (like in the Command Window) anyway, here's the basics of that approach:
First, you have to connect to the database. You do that with either the SQLConnect() or SQLStringConnect() function. For example:
A positive value for nHandle indicates success. Once you have a handle, you use it to send additional commands. For example:
nSuccess = SQLEXEC(m.nHandle, "SELECT First, Last FROM Customers WHERE State='PA'", "csrPACustomers")
That tells MySQL to execute the query you pass in and put the results in csrPACustomers. nSuccess indicates success or failure.
When you're done, use SQLDisconnect() to close your connection:
SQLDisconnect(m.nHandle)
You can read about all three approaches to remote data in the VFP Help file and on the VFP Wiki (http://fox.wikis.com). Once you decide which approach you want to use, you can ask specific questions.
Upvotes: 2