Problem Solution Discussion Exchanging Data Between MySQL and FileMaker Pro

use DBI; use Spreadsheet::ParseExcel::Simple; use Spreadsheet::WriteExcel::FromDB; ... process command-line options not shown ... ARGV == 2 or die Usage: 0 [options] db_name tbl_name\n; my db_name = shift ARGV; my tbl_name = shift ARGV; ... connect to database not shown ... my ss = Spreadsheet::WriteExcel::FromDB-read dbh, tbl_name; print ss-as_xls ; exit 0; Each of t he t hree ut ilit ies writ es t o it s st andard out put , which you can redirect t o capt ure t he result s in a file: from_excel.pl data.xls data.txt to_excel.pl data.txt data.xls mysql_to_excel.pl cookbook profile profile.xls

10.41 Exchanging Data Between MySQL and FileMaker Pro

10.41.1 Problem

You want t o exchange inform at ion bet ween MySQL and FileMaker Pro.

10.41.2 Solution

Under Windows, you can m ake an ODBC connect ion from FileMaker Pro t o t he MySQL server. Alt ernat ively, you can export t ables int o files from MySQL and im port t hem int o FileMaker Pro, or vice versa. But wat ch out for conversion issues like incom pat ible dat e colum n t ypes.

10.41.3 Discussion

I f you can connect from FileMaker Pro t o your MySQL server over an ODBC connect ion, you can access MySQL t ables t hat way. The procedure is sim ilar t o t hat for connect ing t o MySQL from Access. See Recipe 10.39 . Anot her opt ion is t o export dat a from one program int o files and t hen im port t hem int o t he ot her program . The t ransfer direct ory of t he recipes dist ribut ion cont ains a m ysql_t o_filem aker.pl ut ilit y t hat export s a MySQL t able t o a file t hat you can im port int o FileMaker Pro. This script is designed t o handle t he following FileMaker Pro- specific issues: • FileMaker Pros default dat e form at is MM-DD-CCYY . The script rewrit es dat es t he cont ent s of any colum ns in t he MySQL t able t hat cont ain dat es so t hat t hey m at ch FileMaker Pros dat e form at . • FileMaker Pro has dat e and t im e colum n t ypes, but not a com bined dat e-and- t im e t ype. m ysql_t o_filem aker.pl export s DATETIME and TIMESTAMP colum ns as separat e DATE and TIME values. For exam ple, a t able colum n nam ed c is export ed as t wo colum ns nam ed c_date and c_time . • Any int ernal carriage ret urns or linefeeds in colum n values are m apped t o Ct rl- K, which FileMaker Pro uses t o represent line separat ors wit hin dat a values. To process dat e values, m ysql_t o_filem aker.pl uses a t echnique sim ilar t o t hat shown earlier in Recipe 10.34 for const ruct ing a SELECT st at em ent t hat export s t able dat a wit h t he dat es rewrit t en. That is, it reads t he t able m et adat a t o det ect dat e- based colum ns and export s t hem using calls t o DATE_FORMAT t hat rewrit e t he colum n values int o FileMaker Pro form at . m ysql_t o_filem aker.pl writ es out put in what FileMaker Pro calls m erge form at , which is essent ially CSV form at wit h an init ial row of colum n labels. Merge files are useful wit h FileMaker Pro for a couple of reasons: • When creat ing a new t able from a dat afile in m erge form at , FileMaker Pro aut om at ically uses t he labels for t he colum n nam es. This m akes it easy t o carry along t he MySQL t able colum n nam es int o t he FileMaker Pro dat abase. • When im port ing a m erge file int o an exist ing t able, having a row of colum n labels m akes it easier t o m at ch up dat afile colum ns wit h t able colum ns. m ysql_t o_filem aker.pl requires dat abase nam e and t able nam e argum ent s on t he com m and line. For exam ple, t o export t he cont ent s of t he mail t able t o a m erge file m ail.m er, youd invoke t he script like t his: mysql_to_filemaker.pl cookbook mail mail.mer The mail t able has a colum n t t hat cont ains DATETIME values. I f you exam ine m ail.m er, youll see t hat m ysql_t o_filem aker.pl export s t as t wo separat e colum ns, t_date and t_time , wit h t he order of t he dat e part s rearranged from I SO t o MM-DD-CCYY form at : t_date,t_time,srcuser,srchost,dstuser,dsthost,size 05-11-2001,10:15:08,barb,saturn,tricia,mars,58274 05-12-2001,12:48:13,tricia,mars,gene,venus,194925 05-12-2001,15:02:49,phil,mars,phil,saturn,1048 05-13-2001,13:59:18,barb,saturn,tricia,venus,271 05-14-2001,09:31:37,gene,venus,barb,mars,2291 ... m ysql_t o_filem aker.pl also underst ands t he usual opt ions for specifying connect ion param et ers such as - - user or - - host . Any opt ions m ust precede t he dat abase nam e argum ent . Aft er you creat e t he m erge file, you can t ell FileMaker Pro t o open it direct ly which will creat e a new t able or t o im port it int o an exist ing dat abase. To go t he ot her direct ion and im port a FileMaker Pro dat abase int o MySQL, use t he following procedure: 1. Export t he dat abase in som e t ext form at . I f you w ant t he file t o include a row of colum n labels, export t he dat abase in m er ge for m at . You m ay t hen w ant t o r un t he file t hr ough cvt _file.pl t o pr oduce t ab- delim it ed linefeed- t er m inat ed lines. This w ill be useful if you need t o t r ansfor m t he file w it h ot her ut ilit ies w hich assum e t hat for m at . 2. I f t he dat abase cont ains dat es, it m ay be necessar y t o conver t t hem for MySQL, depending on w hat for m at FileMaker Pr o uses for expor t ing t hem . FileMaker Pr o w ill use it s default MM-DD-CCYY for m at if you select dont for m at out put dur ing t he expor t pr ocedur e. I f you select display using cur r ent layout , dat es w ill be export ed using t he form at in w hich FileMaker Pro displays t hem . You m ay be able t o use cvt _dat e.pl t o r ew r it e t he dat es int o I SO for m at . 3. I f t he MySQL t able int o w hich you w ant t o im port t he FileMaker Pro dat a does not exist , creat e it . The guess_t able.pl ut ilit y m ight be helpful at t his point for generat ing a CREATE TABLE st at em ent . 4. I m port t he dat afile int o MySQL w it h LOAD DATA or m ysqlim por t .

10.42 Exporting Query Results as XML