加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl 与MySQL交互示例代码

发布时间:2020-12-16 00:43:26 所属栏目:大数据 来源:网络整理
导读:示例代码(一) 示例代码(二) #!/bin/env?perl? ? use?strict;? use?Getopt::Std;? use?DBI;? ? my?%options;? ? getopts('u:p:d:h:help',%options); //冒号代表其后需要跟一个参数 ? if?(scalar(keys?%options)? ? 4 )?{? ????????printf?"USAGEn";? ??

示例代码(一)

示例代码(二)

 
 
  1. #!/bin/env?perl?
  2. ?
  3. use?strict;?
  4. use?Getopt::Std;?
  5. use?DBI;?
  6. ?
  7. my?%options;?
  8. ?
  9. getopts('u:p:d:h:help',%options); //冒号代表其后需要跟一个参数
  10. ?
  11. if?(scalar(keys?%options)?<?4)?{?
  12. ????????printf?"USAGEn";?
  13. ????????printf?"t%s?-u?username?-p?password?-d?database?-h?hostname/ipn","$0";?
  14. ????????exit?0;?
  15. }?
  16. ?
  17. my?$username?=?$options{u}?if?defined?$options{u};?
  18. my?$password?=?$options{p}?if?defined?$options{p};?
  19. my?$database?=?$options{d}?if?defined?$options{d};?
  20. my?$hostname?=?$options{h}?if?defined?$options{h};?

  21. my?$dsn?=?"DBI:mysql:database=$database;host=$hostname;port=3306";?
  22. my?$dbh?=?DBI->connect($dsn,"$username","$password",{PrintError=>0,RaiseError=>1})?
  23. ???????????????or?die?"Can't?connect?to?mysql"?.?DBI->errstr;?
  24. ?
  25. my?$table?=?qq/?
  26. ??????????????CREATE?TABLE?IF?NOT?EXISTS?test?(?
  27. ??????????????order_id?int(5)?not?null?auto_increment,?
  28. ??????????????name?varchar(10)?not?null?default?'',?
  29. ??????????????email?varchar(20)?not?null?default?'',?
  30. ??????????????PRIMARY?KEY?(order_id));?
  31. ??????????????/;?
  32. ?
  33. my?$sth?=?$dbh->prepare($table);?
  34. ???$sth->execute();?
  35. ?
  36. my?$data?=?qq/?
  37. ?????????????INSERT?INTO?test?VALUES?
  38. ?????????????(null,'henry','henry@abc.com'),?
  39. ?????????????(null,'tom','tom@abc.com'),?
  40. ?????????????(null,'teddy','teddy@abc.com');?
  41. ?????????????/;?
  42. ?
  43. ????$sth?=?$dbh->do($data);?
  44. ????my?$query?=?qq/SELECT?*?FROM?test/;?
  45. ?
  46. ???$sth?=?$dbh->prepare($query);?
  47. ???$sth->execute();?
  48. ?
  49. while?(my?@array?=?$sth->fetchrow_array())?{?
  50. ???????print?join?"t",@array,"n";?
  51. }?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读