Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Working with DBI and DBD::SQLite2

by JamesNC (Chaplain)
on Oct 30, 2004 at 15:01 UTC ( #404033=note: print w/replies, xml ) Need Help??

in reply to Working with DBI and DBD::SQLite2

1. $dbh is undefined, I think you need to either change $D to $dbh or vice versa.
2. $s is undefined, I think you mean $sth

Here is your stuff working and some modifications I made to show you some oddities ( that SQLite docs claim are not bugs but features...) about SQLite that I noticed right away. I don't like this rdbms because it really doesn't care what stuff you feed it. I defined an integer, but it doesn't complain when I send it something else. And you don't get an exception raised when you try to create a table with a bogus data type, which makes debuging difficult.

I would also note that attributes like NULL and NOT NULL which are required when creating tables in most all rdbms's is ignored by SQLite. Also, a DATE datatype which would benefit from right away is absent in SQLite ( you should convert all your dates to a linear value if you want to use this tool and compare inside of SQLite? )
I personally don't think I will ever use this tool. There are too many free fully featured rdbms's out there.
#!perl -w use strict; use DBI; my $drop_tables = 1; my $create_tables = 1; my $dbh=DBI->connect("dbi:SQLite2:dbname=Store.db","","", {RaiseError= +> 1}) or die "$!"; if( $drop_tables == 1 ){ #NO SUPPORT for IF EXISTS $dbh->do("DROP TABLE ISP"); $dbh->do("DROP TABLE UnityPrimary"); $dbh->do("DROP TABLE UnitySecondary"); } if( $create_tables == 1){ $dbh->do("CREATE TABLE ISP(Store INTEGER,Date CHAR[1] NOT NULL )") or +warn "$!"; #Define my own $dbh->do("CREATE TABLE UnityPrimary(Store INTEGER,Date CHAR[9] NOT NUL +L)"); $dbh->do("CREATE TABLE UnitySecondary(Store INTEGER ,Date CHAR[9] NOT +NULL )"); } #open(CSV,"reboots.csv")||die"Can't open: $!\n"; while(<DATA>){ chomp; my($store,$isp,$pri,$sec)=split /,/; my $sta=$dbh->prepare("INSERT INTO ISP(Store,Date) VALUES(?,?)"); my $stb=$dbh->prepare("INSERT INTO UnityPrimary(Store,Date) VALUES(? +,?)"); my $stc=$dbh->prepare("INSERT INTO UnitySecondary(Store,Date) VALUES +(?,?)"); $sta->execute( $store, $isp ); $stb->execute( $store, $pri ); $stc->execute( $store, $sec ); } #close CSV; my $sth=$dbh->prepare("SELECT * FROM UnityPrimary WHERE Store > 1"); $sth->execute; while(my@row=$sth->fetchrow_array){ print "@row\n"; } $dbh->disconnect; __DATA__ 1,18-Oct-04,29-Oct-04,29-Oct-04 notanInt,18-Oct-04,26-Oct-04,26-Oct-04 3,21-Oct-04,26-Oct-04,11-Aug-04 4,20-Oct-04,11-Jun-04,27-Oct-04 5,18-Oct-04,3-Sep-04,24-May-04 6,17-Oct-04,15-Oct-04,15-Oct-04
OUTPUT: notanInt 26-Oct-04 3 26-Oct-04 4 11-Jun-04 5 3-Sep-04 6 15-Oct-04

Replies are listed 'Best First'.
Re^2: Working with DBI and DBD::SQLite2
by TStanley (Canon) on Oct 30, 2004 at 16:10 UTC
    I cut and pasted from my test script. Looks like I forgot to rename a few variables. :)

    The only thing necessary for the triumph of evil is for good men to do nothing -- Edmund Burke

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://404033]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2018-03-18 02:37 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (228 votes). Check out past polls.