Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

2Re: Can SQL be used without a database?

by jeffa (Bishop)
on Dec 30, 2003 at 19:55 UTC ( #317761=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://317761]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2022-08-13 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?