[prev in list] [next in list] [prev in thread] [next in thread] 

List:       msql
Subject:    [mSQL] mSQL + msql PERL interface
From:       jcoy () arkansas ! net (Coy, John)
Date:       1995-09-01 20:17:55
[Download RAW message or body]

Hi,

I'm new to mSQL and the PERL msql interface and I have
a quick (and perhaps silly) question.

What is the best way to quote my data once it is in an
array?  For example, if I have data in an array that I
obtained from a function (example @info = getpwent()),
how can I convert the elements of the array into quoted
strings quickly so I can use them in an INSERT command?
How can I do this even if the table I'm inserting them into
doesn't contain all char() types in all the columns?

Here is a sample script I'm trying to get working, but it
fails because the information in the getpwent() array is
not quoted. 

I hope I explained this well enough.  If not, could someone
kindly point me to an archive of scripts using the PERL
msql interface?

-- cut here --

#! /usr/bin/perl
#
use Msql;

$dbh = Msql->connect('', "misery") || die("Error: Unable to connect to DB\n");

if (! &table_exists("pwent")) {
    $query = <<EOF;
        create table pwent (
            uname       char(8),
            passwd      char(32),
            uid         int,
            gid         int,
            quota       int,
            comment     char(32),
            gcos        char(32),
            homedir     char(32),
            shell       char(32)
        )
EOF
    $sth = $dbh->query($query) || die("Error: Unable to create table");
}

while (@info = getpwent()) {
    #
    # here is where the problem lies.  How do I convert the array @info
    # into a string that can be used with $dbh->query()?
    #
    $data = join(",", @info);
    $query = "insert into pwent values ( $data )";
    $sth = $dbh->query($query) || die("Error: Unable to insert data\n");
}

sub table_exists {
    local($new) = @_;
    local($table);

    foreach $table ($dbh->ListTables()) {
        return 1 if ($table eq $new);
    }
    return 0;
}

--------------------------------------------------------------------------
To remove yourself from the Mini SQL mailing list send a message containing
"unsubscribe" to msql-list-request@bunyip.com.  Send a message containing
"info msql-list" to majordomo@bunyip.com for info on monthly archives of
the list. For more help, mail owner-msql-list@bunyip.com NOT the msql-list!

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic