http://qs321.pair.com?node_id=317761


in reply to Re: Can SQL be used without a database?
in thread Can SQL be used without a database?

I can vouch for DBD::SQLite. It rocks. :) The first hurdle with SQLite is the fact that you have to have an egg before you can have chickens. What i mean is that you first have to write a script that creates (and then optionally populates) the database file. Something as trivial as:

use DBI; use Data::Dumper; my $dbh = DBI->connect( 'dbi:SQLite:dbname=dbfile','','', # { RaiseError => 1}, ); eval { $dbh->do('drop table foo'); $dbh->do('create table foo(id int unsigned, name char(64))'); }; my $sth = $dbh->prepare('insert into foo values (?,?)'); $sth->execute(@$_) for [1,'moe'],[2,'curly'],[3,'larry']; print Dumper $dbh->selectall_arrayref('select * from foo');
However, you may notice from the code that i am supplying the ID. I do not think that SQLite offers auto-incremented ID's for you.

Alternatively, if you already have your data stored in another format, check out SQL Fairy before you roll your own.

UPDATE:
That's right ... thanks for pointing out my error (once again!) merlyn. :)

Here is are updated lines for the snippet above:

$dbh->do('create table foo(id integer primary key, name char(64))'); my $sth = $dbh->prepare('insert into foo(name) values (?)'); $sth->execute($_) for qw(moe curry lary);
Much better. ;)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
•Re: 2Re: Can SQL be used without a database?
by merlyn (Sage) on Dec 30, 2003 at 20:03 UTC

      ...and using the DBI func method will pull out the value for you after an insert:

      $dbh->do( q{ CREATE TABLE foo ( id integer not null primary key, name varchar(20) ) }); $dbh->do( "INSERT INTO foo ( name ) VALUES ( 'bar' )" ); print "ID of record: ", $dbh->func( 'last_insert_rowid' ), "\n";

      Chris
      M-x auto-bs-mode