Problem : VFP to MySQL – How To Insert, Update, etc. Data

Problem : VFP to MySQL – How To Insert, Update, etc. Data

I have established a working connection using the ODBC 3.15 driver between VFP 9 and a MySQL db, both residing on my machine.
Now that I have the connection what I need are a few specific examples to help me actually make use of the connection.

Code-wise what I have to establish the connection is this (which I did not create but copied from a forum post) :
lcUser = “root”
lcPassword = “1234”
lcStringConn=”Driver={MySQL ODBC 3.51 Driver};Port=3306″+;
*** Don’t prompt for login
IF lnHandle  > 0
browse last nocaptions
MESSAGEBOX(“Error at Connecting”+CHR(13)+;
In the testdb resides ‘table1’ and contained within table1 is ‘field1’ (varchar 40).

Can someone please working examples for the following:
code necessary to initially populate field1 with “sample data”.
code necessary to update field1 with “new sample data”.

There will be more, but this will get me started working for now. Many thanks.


Solution: VFP to MySQL – How To Insert, Update, etc. Data

SQLEXEC( ) Function

Here are different help options on the command

You can find more information here

This is from the help on SQLEXEC in VFP 9.0
LOCAL lnConn
LOCAL lnPercent AS Int  && Input parameters must be typed.
LOCAL lnOutput
lnPercent = 50
lnOutput = 0

* Make connection, assuming a local trusted connection.
lnConn = SQLCONNECT(‘local’)
IF m.lnConn > 0  && Success.

* Set the active database to PUBS.
SQLEXEC(m.lnConn, ‘use pubs’)

* Execute SELECT statement.
SQLEXEC(m.lnConn, ‘SELECT * FROM authors’, ‘PubAuthors’)

* Execute INSERT statement, get value of identity field.
SQLEXEC(m.lnConn, “INSERT INTO JOBS (job_desc, min_lvl, max_lvl);
VALUES (‘Developer’,75,150)”)
SQLEXEC(m.lnConn, “SELECT SCOPE_IDENTITY()”, “job_id”)
? “ID for added Job is ” + LTRIM(STR(job_id.exp))

* Execute DELETE statement. Get number of records affected.
SQLEXEC(m.lnConn, “DELETE FROM JOBS WHERE job_desc =’Developer'”)
SQLEXEC(m.lnConn, “SELECT @@ROWCOUNT”, ‘rowcount’)
? rowcount.exp, “record(s) deleted”

* Call a stored procedure with no parameters.
SQLEXEC(m.lnConn, ‘sp_who’, ‘activeusers’)

* Execute stored procedure with an INPUT parameter.
SQLEXEC(m.lnConn, ‘exec byroyalty ?lnPercent’,’HalfOffAuthors’)

* Create temp stored procedure with OUTPUT parameter and call it.
SQLEXEC(m.lnConn, “CREATE PROCEDURE #MyProc @outparam int OUTPUT AS;
SELECT @outparam=100”)
SQLEXEC(m.lnConn, “exec #myProc ?@lnOutput”)
? m.lnOutput

* Create a temp stored procedure with INPUT and OUTPUT parameters
* and call it.
SQLEXEC(m.lnConn, “CREATE PROCEDURE #MyProc2 ” + ;
“@inputparam INT, ” + ;
“@outparam int OUTPUT ” + ;
“AS SET @outparam=@inputparam*10”)
SQLEXEC(m.lnConn, “exec #myProc2 ?lnPercent, ?@lnOutput”)
? m.lnOutput

* Get version information.
SQLEXEC(m.lnConn, ‘SELECT @@VERSION’,’SQLVersion1′)
? STRTRAN(SQLVersion1.Exp,CHR(0))

* Disconnect.
? “Unable to connect to SQL Server”