in reply to Can't insert into for one column

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' }

Replies are listed 'Best First'.
Re^2: Can't insert into for one column
by Anonymous Monk on Jun 15, 2004 at 19:11 UTC
    Thank you everyone who responded :-)

    I haven't done much with DBI - just reading through some examples in O'Reilly books, the 'Perl Cookbook' and 'Computer Science and Perl Programming' and the MySql book by Dubois. I do remember seeing the quote function in some of the examples but it did not click that that was causing the problem. I read a question on clpm that the poster wanted some advice and I thought a database might provide a better solution to his problem.

    http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=10crcltibum94e +6%40corp.supernews.com
    Chris