in reply to Searching a text file

I'd approach this with DBI and one of the CSV drivers like DBD::CSV. That will save you a lot of code, and leave most of your search in the hands of DBI.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Searching a text file
by monoxide (Beadle) on Nov 14, 2004 at 03:03 UTC
    Thanks Zaxo, but how would i write an SQL query that would let me search for a string in any field in the table? also i am not such a big fan of DBD::CSV.

      Why would you *want* to search for a string in any field? You last field looks like a boolean (on off) your 4th field looks boolean too (male female). If you are searching for emails you would not really want to look there....

      Presumably you have a data entry form. This can also be a search form, just add a button that says search. When a user enters data in one or more fields and clicks search all you do is make a query like:

      $sth = $dbh->prepare('SELECT foo,bar,baz FROM widget WHERE foo=? AND b +ar=? AND baz=?'); $sth->execute($foo,$bar,$baz);

      where you dynamically construct the WHERE clause depending on how many fields the user enters data into. To search *all* the fields for the one string the syntax would simply be OR, not AND and include all the fields you want to search.....

      $sth = $dbh->prepare('SELECT foo,bar,baz FROM widget WHERE foo=? OR ba +r=? OR baz=?'); $sth->execute($str,$str,$str);

      DBD::SQLite might suit you better.

      cheers

      tachyon

      If you want to search for a string in any field, just concatenate all your fields into one field and write an SQL which does a "LIKE" search (with appropriate "%" before and after the search term) on that single field.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law