This should do what you are looking to do.

#!/usr/bin/perl use strict; use warnings; use DBI; sub dbConnect{ my ($database, $username, $password, $hostname) = @_; my $db = DBI->connect("DBI:SQLite:$database:$hostname", $username, $password) || die "Cannot connect to host database.".$DBI::errstr; return $db; } sub dbDisconnect{ my ($db) = @_; $db->disconnect; } sub dbINSERT{ my $table = shift; my $set = shift; my @values = @_; my($database) = "myDB"; my($username) = "username"; my($password) = "password"; my($hostname) = "myHost.com"; my $db = dbConnect($database, $username, $password, $hostname); my $vals = join',', map { $db->quote($_) } @values; my $query = qq|INSERT into $table ($set) VALUES ($vals)|; print "$query\n"; $query = $db->prepare($query); #LINE 31 $query->execute; #LINE 40 $query->finish; dbDisconnect($db); } sub dbUPDATE{ # Do updates here } sub dbDELETE{ # Do deletes here } dbINSERT('ORGANISM', 'ORG_NAME, GI, NC', "11111111", "some bacteria", +"NC_000000");
Though, I would move the db connection out of the insert/delete/update subs and just do something like this
#!/usr/bin/perl use strict; use warnings; use DBI; sub dbConnect{ my ($database, $username, $password, $hostname) = @_; my $db = DBI->connect("DBI:SQLite:$database:$hostname", $username, $password) || die "Cannot connect to host database.".$DBI::errstr; return $db; } sub dbINSERT{ my $db = shift; my $table = shift; my $set = shift; my @values = @_; my $vals = join',', map { $db->quote($_) } @values; my $query = qq|INSERT into $table ($set) VALUES ($vals)|; print "$query\n"; $query = $db->prepare($query); #LINE 31 return ( $query->execute ) : 0 : 1; } sub dbUPDATE{ # Do updates here } sub dbDELETE{ # Do deletes here } my $db = dbConnect("myDB", "username", "password", "myHost.com"); my $status = ( dbINSERT($db, 'ORGANISM', 'ORG_NAME, GI, NC', "11111111 +", "some bacteria", "NC_000000") == 0 ) ? 0 : 1; END { $db->disconnect }
Its untested, but should work fine for you.

Update added return code from insert and removed disconnect sub. After re-reading your original post - you may wish to re-add the disconnect sub ( and remove the END block ) as this looks to be part of a larger program

Ted
--
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
  --Ralph Waldo Emerson

In reply to Re: Query Formatting by tcf03
in thread Query Formatting by ShayShay

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.