Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Can SQL be used without a database?

by jonadab (Parson)
on Dec 30, 2003 at 18:44 UTC ( [id://317738]=note: print w/replies, xml ) Need Help??


in reply to Can SQL be used without a database?

If what you want is a simple SLQ database engine that doesn't require you to install a bunch of stuff or administrate a full RDBMS server system, you may be interested in SQLite, which just stores your database in a single flat file. I've never used it, but it may be easier than installing and configuring a more full-featured RDBMS.


$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

Replies are listed 'Best First'.
2Re: Can SQL be used without a database?
by jeffa (Bishop) on Dec 30, 2003 at 19:55 UTC

    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)
    

        ...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://317738]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-25 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found