Contenu connexe Similaire à perl usage at database applications (20) perl usage at database applications6. Back to reporting The man in breau The game they played The suggestion I once made on OLAP 9. Give me Excel Copy HTML table into Excel Or, name HTML as .xls Use some IDE tool? 10. Toolset basic $ echo 'select SESSION_ID, HIT_ID from hit where USERNAME=? and ACCESS_TIME between to_date(?,?) and to_date(?,?)' > session.sql ↵ 11. It worked $ bofc < session.sql user-123 201106122345 YYYYMMDDHH24MI 201106122357 YYYYMMDDHH24MI | wc -l ↵ SESSION_ID HIT_ID 12 12. And ... $ bofc < session.sql user-123 201106122345 YYYYMMDDHH24MI 201106122357 YYYYMMDDHH24MI | sort ↵ SESSION_ID HIT_ID 20110612234046 1772475805 20110612234046 1772475807 20110612234046 1772475809 20110612234046 1772475811 20110612234046 1772475813 ... 16. Camel coding PERL5LIB=/proj/Web/EMIS/Modules perl -MEMIS::Database -le '($s= do{$d=EMIS::Database::gethandle(q(CDB)); $d->do(q(begin; nls_format_setting; end;)); $d}->prepare_cached(join q(),<STDIN>))->execute(@ARGV); $,=qq(); print STDERR @{$s->{NAME}}; print @r while @r=$s->fetchrow_array; $d->disconnect' $* 19. $s = do { $d=...; ...; $d } -> prepare_cached( join q(),<STDIN>) 20. ( $s = ... ) -> excute ( @ARGV ); 24. emp #!/usr/bin/perl -l use DBI; $s = do { DBI-> connect ( q(DBI:mysql:database=employees) , q() , q() ); }->prepare_cached( join ( q() ,<>) ); $s->execute ( @ARGV ); $, = qq( ) ; print STDERR @{$s->{ NAME }} ; print @r while @r = $s->fetchrow_array ; 25. Work Horse But I want query more than one DB ... Can you send me another column? Make a comma seperated list in one column ... 34. emp-for #!/usr/bin/perl -wlan use DBI; use IO::Handle; BEGIN { $s = do { DBI-> connect ( q(DBI:mysql:database=employees) , q() , q() ); }->prepare_cached( do { open ( SQL , q(<) , shift ); join ( q() , <SQL> ) }); autoflush STDOUT 1 ; $, = qq( ) ; } END { $s->finish ; $d->disconnect ; close SQL ; } $s->execute ( @F ); if ( @r = $s->fetchrow_array ) { print @r ; @x = map { q() } @r unless @x } else { print @x } 35. How to list last deptartment? $ echo 'select emp_no from employees limit 3'|emp 2>/dev/null ↵ 10001 10002 10003 $ echo 'select emp_no from employees limit 3'|emp 2>/dev/null| emp-for emp_dept.sql ↵ d005 d007 d004 54. Demo usage How to describe a wide table? With sample data attached? 55. Columns $ echo 'select DATA_TYPE, CHARACTER_MAXIMUM_LENGTH from information_schema.columns where table_name=?'| emp employees | tee emp-desc ↵ DATA_TYPE CHARACTER_MAXIMUM_LENGTH int date varchar 14 varchar 16 enum 1 date 56. Demo Row $ echo 'select * from employees limit 1' | emp 2>&1 | row2col | tee emp-data ↵ emp_no 10001 birth_date 1953-09-02 first_name Georgi last_name Facello gender M hire_date 1986-06-26 57. And now? Run merge-tab over the two files $ merge-tab emp.data emp.desc ↵ emp_no 10001 int birth_date 1953-09-02 date first_name Georgi varchar 14 last_name Facello varchar 16 gender M enum 1 hire_date 1986-06-26 date