Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Query Language with Flat Files

by Marshall (Canon)
on Sep 18, 2009 at 19:21 UTC ( [id://796187]=note: print w/replies, xml ) Need Help??


in reply to Query Language with Flat Files

I suggested earlier, DBD::CSV. This is a "full DB" that uses a CSV (Comma Separated Value) file. The advantage of this is that since it uses a "flat" CSV data file, you can easily 1)create the initial DB, 2) watch what happens as you run SQL statements. This module is used like you'd use a massive commercial DB...you connect to the DB, then run SQL on it. It handles multiple processes and deals with all this lock stuff.

You have a simple data structure. Learn how to use basic SQL with something easy. The code (connect to DB, SQL stuff) will all be same on a bigger DB. The difference will be that on a higher performance DB, you can't just "cat" the DB file to see what is going on.

You have a "wimp" DB. I mean 15K records with 5 fields that can be represented as a "flat" file, and that is only updated 3 times per minute, is "nothing". It will be fast enough. Get the code working then think about something more sophisticated.

Update:
Here is some very simple code to get you started:
the DBI::CSV didn't used to work on Windows, but it does now (Perl 5.10).
You will need to read a bit about SQL, but that knowledge will be transportable to other databases. Note that field names come from the file, like "username". This is exactly like an Excel spreadsheet can dump out.

#!/usr/bin/perl -w use strict; use DBI; #need to have DBI::CSV installed for this script my $db = DBI->connect("DBI:CSV:f_dir=./DEMO.db") or die "Cannot connect: $DBI::errstr"; my $get_rows = $db->prepare("SELECT * from DEMO.db"); $get_rows->execute() || die "can't fetch all rows"; while (my ($name,$username, $time, $n1, $n2) = $get_rows->fetchrow_array) { print "$time $name $username $n1 $n2\n"; } my $get_names = $db->prepare("SELECT username from DEMO.db"); $get_names->execute() || die "can't fetch all usernames"; while (my $name = $get_names->fetchrow_array) { print "$name\n"; } __END__ DATA IN THE FILE: DEMO.db: ------------------------------(this line not in this file) name,username,time,num1,num2 "John Smith",jhon99,"10:30:01",345,765 "John Smith",jhon98,"10:30:01",345,765 "Bob Smith",bsm01,"11:30:01",002,246 ABOVE CODE PRINTS: ------------ 10:30:01 John Smith jhon99 345 765 10:30:01 John Smith jhon98 345 765 11:30:01 Bob Smith bsm01 002 246 jhon99 jhon98 bsm01

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 09:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found