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) :
*************************************************************************************************************
lcServer=”localhost”
lcDatabase=”testdb”
lcUser = “root”
lcPassword = “1234”
lcStringConn=”Driver={MySQL ODBC 3.51 Driver};Port=3306″+;
“;Server=”+lcServer+;
“;Database=”+lcDatabase+;
“;Uid=”+lcUser+;
“;Pwd=”+lcPassWord
*** Don’t prompt for login
SQLSETPROP(0,”DispLogin”,3)
lnHandle=SQLSTRINGCONNECT(lcStringConn)
IF lnHandle  > 0
? SQLTABLES(lnHandle,”TABLES”)
browse last nocaptions
SQLDISCONNECT(lnHandle)
ELSE
=AERROR(laError)
MESSAGEBOX(“Error at Connecting”+CHR(13)+;
“Description:”+laError[2])
ENDIF
**********************************************************************************************
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
http://msdn2.microsoft.com/en-us/library/aa978475(VS.71).aspx

Here are different help options on the command
http://search.msdn.microsoft.com/search/Default.aspx?query=sqlexec%20foxpro&brand=msdn&locale=en-us&refinement=00&lang=en-us

You can find more information here
http://support.microsoft.com/search/default.aspx?catalog=LCID%3D1033&1033comm=1&spid=1111&query=sqlexec&pwt=false&title=false&kt=ALL&mdt=0&res=20&ast=1&ast=2&ast=3&mode=a&adv=1

This is from the help on SQLEXEC in VFP 9.0
CLEAR
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’)
BROWSE

* 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’)
BROWSE

* 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.
SQLDISCONNECT(m.lnConn)
ELSE
? “Unable to connect to SQL Server”
ENDIF
RETURN