I don't know why I can't insert for the type field.

Maybe if you print $sql before executing it, you see what's wrong. I did:

INSERT INTO sales VALUES(10112, 6768, cash, 1020, 780, 1)
"cash" isn't quoted, so the SQL engine thinks it's a column or variable name. The solution to your problem is to either use quotes in the format string (bad), or to use placeholders (good). Placeholders were designed for this problem. Placeholders work much like sprintf, so understanding how they work shouldn't be a problem for you. Still, it's a good idea to read in DBI's documentation what they have to say about it.

In this example I will use DBIx::Simple, because typing (??) is much easier than typing (?, ?, ?, ?, ?, ?) and I am lazy.

#!/usr/bin/perl -w use strict; use DBIx::Simple; my $db = DBIx::Simple->new('dbi:SQLite:mydb.txt', '', '', { RaiseError + => 1 }); eval { $db->query( 'CREATE TABLE sales (customer, vendor, type, product, app, res +ult)' ); }; while (<DATA>) { $db->query('INSERT INTO sales VALUES (??)', split); } __DATA__ ...

RaiseError lets the program die on errors. This saves a lot of "or die" typing. The eval is there to allow the CREATE TABLE statement to fail. (It'll fail if the table already exists.)

hth

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }


In reply to Re: Can't insert into for one column by Juerd
in thread Can't insert into for one column by Anonymous Monk

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.