insert into two table and check table perl DBI

From: Hendra Kusnandar (hendrakusnandar_at_yahoo.com)
Date: 03/27/04

  • Next message: Abhishek Jain: "Will DBI work with SQL 2000"
    Date: Sat, 27 Mar 2004 00:57:31 -0800 (PST)
    To: dbi-users@perl.org
    
    

    #!/usr/bin/perl
    use DBI;
    sub insert_user {
     my ($nim, $nama, $password) = @_;
     my ($insert_sql,$sth);
     $database = "practice_perl";
     $dbuser = "hendra";
     $dbpasswd = "hendra";
     $dbhost = "localhost";
     $dsn = "DBI:mysql:database=$database;host=$dbhost";
     $dbh = DBI->connect($dsn,$dbuser,$dbpasswd) || die $DBI::errstr;
     $insert_sql = qq {insert into user (nik,name,password) values ('$nik', '$name', '$password')};

     $sth = $dbh->prepare($insert_sql) || die "prepare: $insert_sql: $DBI::errstr";
     $sth->execute || die "Unable to execute query: $dbh->errstr\n";
     $sth->finish;
     $dbh->disconnect;
    }#end insert_user
     
    sub insert_log {
     my ($input) = @_;
     my ($insert_sql_log);
     $database = "practice_perl";
     $dbuser = "hendra";
     $dbpasswd = "hendra";
     $dbhost = "localhost";
     $dsn = "DBI:mysql:database=$database;host=$dbhost";
     $dbh = DBI->connect($dsn,$dbuser,$dbpasswd) || die $DBI::errstr;
     $insert_sql_log = "insert into log values ('',".$dbh->quote($input).")";
     $dbh->do($insert_sql_log) || die ($dbh->errstr);
     return $dbh->{'mysql_insertid'};
     $dbh->disconnect;
    }#end insert_log
     
    print "INPUT : ";
    while ($input = <>) {
      #format insert : nik,name,password
      if($input =~/^(\d{8}),(.*),(\w{6})$/i){
         insert_user($1,$2,$3);
      }else{
        insert_log($input);
        print "Wrong Format";
      }#end if
      print "\nINPUT : ";
    }#end while
     
    hai everyone, i have write scripts perl on the top and if i input data 12345678,Hendra Kusnandar,123456 data will insert
    into table user, and if i input wrong format data will insert into log.
    this scripts is work but if i insert again 12345678,Hendra Kusnandar,123456 scripts will error and the message are : DBD::mysql::st execute failed: Duplicate entry '12345678' for key 1 at ./test.pl line 20, <> line 1.
    Unable to execute query: DBI::db=HASH(0x81e1ed0)->errstr
    i know field nik in table user on mysql is primary key that i can't insert same data...how to correct this scripts if i insert the same data and will show the message on shell "cannot insert, same data".
    second i want if data insert into table user also data can insert into table log

    I have tried every way possible I can think of and I cannot get it to work
    at all. Any help with this would be greatly appreciated.
     
    regards,

    hendra kusnandar
     

     

    ---------------------------------
    Do you Yahoo!?
    Yahoo! Finance Tax Center - File online. File on time.


  • Next message: Abhishek Jain: "Will DBI work with SQL 2000"

    Relevant Pages

    • How to preserve current selection after appl.events carry out an copy-pastespecial action ?
      ... I need to keep format and conditions in a sheet ... Private Sub Worksheet_Deactivate ... ' Call FormatTemp Sub to ensure right format in sheet ... Dim RngSel As Range ...
      (microsoft.public.excel.programming)
    • How to preserve current selection after appl.events carry out an copy-pastespecial action ?
      ... I need to keep format and conditions in a sheet ... Private Sub Worksheet_Deactivate ... ' Call FormatTemp Sub to ensure right format in sheet ... Dim RngSel As Range ...
      (microsoft.public.excel.misc)
    • Quick Date entry revisited
      ... change the Compiler constand #Const US_STYLE ... Private Sub Worksheet_SelectionChange ... 'value has been changed to text format ... 'This is the European and US date entry / format version ...
      (microsoft.public.excel.programming)
    • some problems in date entry in a web bsed statement
      ... not in excel spreadsheet format.But there is provision to save the ... the original statement have many problems ... 2.if the date is>12 it is general format. ... Public Sub expt4() ...
      (microsoft.public.excel.misc)
    • Re: Formated Text?
      ... A picture box is similar. ... Dim MyRect As RECT ... Public Sub Move(ByVal Left As Long, ByVal Top As Long, _ ... Public Sub DrawString(ByVal hdc As Long, Text As String, Format As CONST_DTFLAGS) ...
      (microsoft.public.vb.general.discussion)