efransiscus personal blog

Tutorial : Using MySQL ODBC Connector with Foxpro 9 and MySQL

Posted by: Erwin Fransiscus on: March 15, 2009

Yesterday, I got a project to handle with MySQL and Visual Foxpro. I’m used to MySQL, and so does with Visual Foxpro. The trouble is that I’ve never work on both of them in the same time. Usually I’m using Foxpro integrated DBMS when using Visual Foxpro, and MySQL used to be a DBMS when I’m using PHP, C#, or ASP.Net.
I got a little problem on connecting those software, but after multiple tries, it did works.
How that happen? here’s the simple tutorial:

Note : I’m using Microsoft Windows XP SP 3, Visual Foxpro 9, and WAMP server 1.7.3, I’m working on a single desktop pc. I’m sorry i didn’t include the images example on doing this, hope with just the texts you would understand how it works.

First, create a database on MySQL. The database is quite important, because when you install and configure ODBC, there’s an option of which databases you want to use. I’m also using PHPmyadmin for the GUI of the database application. here’s for example.

> CREATE DATABASE dbfarmasi
> CREATE TABLE `dbfarmasi`.`tb_brg` (`kdbrg` CHAR( 6 ) NOT NULL ,`nmbrg` VARCHAR( 50 ) NOT NULL , PRIMARY KEY ( `kdbrg` )) ENGINE = INNODB
> INSERT INTO `dbfarmasi`.`tb_brg` (`kdbrg` ,`nmbrg`) VALUES ( ‘00001′, ‘First item’ );
> INSERT INTO `dbfarmasi`.`tb_brg` (`kdbrg` ,`nmbrg`) VALUES ( ‘00002′, ‘Second item’ );

i’m named the database “dbfarmasi” and the table “tb_brg”. So here, we got a database and a table, rock!

Then you download a software called MySQL ODBC Connector. I’m using mysql-connector-odbc-3.51.25-win32.exe, which is free, and can be downloaded by visit mysql site. You can try to google it.

Install. After installation has done, you should go to “Control Panel” > “Administrative Tools” > “Data Sources (ODBC)”. And then there’s pop-up window of ODBC. In “User DSN” tab, right side, press “Add” button”, and select “MySQL ODBC 3.51 Driver” as the data source, and “Finish”.

Another windows will appear, it is a configuration window for ODBC. Try this :

Data source name : koneksi
Description : connect foxpro and mysql
Server : localhost
User : root
Database : dbfarmasi

The DSN name is up to you, so does the description.
The default Server is localhost, ’cause I’m not using remote database.
Default user name is root, as the first super user name in mysql.
I’m using no password on mysql (it is set to none by default when you’re install WAMP)
and the database I’m using is dbfarmasi, as i’ve created before.

Ta-da. The connection has been made.

Now, for the visual foxpro. here is a simple one :
Create a project, whatever the name is, save it. on “Project Manager” window, choose “Code” tab.
Press “New” on Program.
Here is the code:

PUBLIC P1
STORE SQLCONNECT(‘koneksi’, ‘root’) TO P1

close and save it to name :  ”setdatabase

now, back to “Project Manager” window, choose “Document” tab, press “New” button to create a new form, and pick the “New Form”, not the “Form Wizard” thing.
After that, an empty form will appear. create a two Textboxs.
named the first textbox to : txtkdbrg and the second textbox to : txtnmbrg.
Add a grid to form. named it as gridContainer. 

Now double-click the form,  select the object form1 and the procedure init, and add this code:

DO setdatabase
thisform.gridContainer.RecordSource=”"
SQLEXEC(P1,”select * from tb_brg”,”tb_brg”)
IF NOT USED(“tb_brg”)
USE tb_brg ALIAS tb_brg
ENDIF 
SELECT tb_brg
thisform.gridContainer.RecordSource=”tb_brg”

Save the form and run it. TA-DA. It works, its connected. The content of table “tb_brg” is showed by the form by the gridContainer.

It means that the ODBC works. Here the connection thing :
do setdatabase
SQLEXEC(P1,”select * from tb_brg”,”tb_brg”)
means :
run program code “setdatabase” (which we’ve created before, for connection)
select P1 as ODBC connection DNS, (connection name variable in setdatabase (replace SQLCONNECT(bla bla bla))
“select * from tb_brg”, the sql query, select all from table tb_brg
“tb_brg”, is a cursor in visual foxpro, that used for alias.

So, with SQLEXEC([connection name],”[sql query]“,[cursor name]), you can do sql command: SELECT. Don’t forget to add cursorname/alias when you’re using SELECT.

You don’t have to add cursorname if you’re using INSERT or DELETE query.
example :

SQLEXEC(P1,”insert into tb_brg (kdbrg,nmbrg) values (‘”+thisform.txtkdbrg.value+”‘,’”+thisform.txtnmbrg.value+”‘)”)

means you insert value from textbox (txtkdbrg and txtnmbrg).
for delete command example :

SQLEXEC(P1,”delete from tb_brg where kdbrg =’”+thisform.txtkdbrg.Value+”‘”)

Once again, i’m sorry i’m not using images for this tutorial.
Hope you understand what i meant in this simple tutorial, and hope it help you.

7 Responses to "Tutorial : Using MySQL ODBC Connector with Foxpro 9 and MySQL"

hi Erwin, it did helped me…kindly give more examples for grid…:)

Glad it helped you. What kind operation for grid do you want ?

Note that VFP 9 added a 4th [optional] parameter to SQLEXEC() to get the number of rows returned in the result set by Select or the number of rows affected by Update, Insert, Delete.

Handy if you want to know the success/failure of your query.

I’m not used to use the 4th optional parameter. Would you mind to give me an example? it will be nice. thanks :)

i wonder why this update command didnt work…

SQLEXEC(CONN, “UPDATE TBLMESSAGE SET THEMESSAGE = VMESSAGE, FROMWHOM = VFROM, TOWHOM = VTO, RECEIVINGOFFICER = VRECEIVINGOFFICER,
TIMERECEIVED = TIMEREC1, DATERECEIVED = DATEREC WHERE MESSAGEID = CURRENTID”)

does the VMESSAGE, VFROM, VTO, TIMEREC1, DATEREC, and CURRENTID is a memory variables ?

So far as I know, and i hope it would help you, if I were you, i would do this :

SQLEXEC(CONN, “UPDATE TBLMESSAGE SET THEMESSAGE = ‘”+VMESSAGE+”‘, FROMWHOM = ‘”+VFROM+”‘, TOWHOM = ‘”+VTO+”‘, RECEIVINGOFFICER = ‘”+VRECEIVINGOFFICER+”‘,
TIMERECEIVED = ‘”+TIMEREC1+”‘, DATERECEIVED = ‘”+DATEREC+”‘ WHERE MESSAGEID = ‘”+CURRENTID+”‘”)

Good Luck :)

thank you Erwin Fransiscus for visual foxpro sample “grid” connecting to mysql

Leave a Reply

Efransiscus blog

Hello, welcome to my blog. My name is Erwin Fransiscus. I lived and study at Yogyakarta, Indonesia. If you want to contact me, want me to add something in my blog, you can send me an email to efransiscus@gmail.com or add my yahoo! messenger id, efransiscus@ymail.com. Hopefully you enjoy my blog. Feel free to comment my post, it will be a honor for me.. Enjoy ^^

Efransiscus Blog Stats

  • 16,408 hits

Archives