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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The short answer is "yes you can use DBI with DBD::AnyData to treat your data as a database and run SQL queries on it".

The somewhat longer answer is that you'll want to convert your data from the hash to a text format such as CSV (Comma Separated Values) or XML or a Fixed-length record. AnyData can work with those and others. For the example below, I'll assume you will use CSV.

A second issue is the names of your fields. AnyData tries to stick to the rules of SQL which define fieldnames as starting with an alphabetic character and containing only alphanumerics and underscores. There are ways to circumvent that by quoting the field names, but that adds a level of complexity you don't really need. It would be much simpler to rename your fields to be valid SQL field names. I've done that in the example below (e.g. _SESSION_ID loses the underscore and image.x gets an underscore instead of a period).

With those caveats, here's a working example that takes a hash like you've shown, inserts it into a database and then queries that database.

#!/usr/bin/perl -w use strict; use DBI; my $dbh = DBI->connect('dbi:AnyData(RaiseError=1):'); $dbh->ad_catalog('Login','CSV','Login.csv'); my $data = {Password=>'raman',Session_ID=>'8ada23',Image_X=>55}; my @cols = qw(Password Session_ID Image_X); my @vals = @$data{@cols}; my $colStr = join ',', @cols; my $paramStr = join ',', ('?')x@cols; $dbh->do("DROP TABLE IF EXISTS Login"); $dbh->do("CREATE TABLE Login (Password TEXT,Session_ID TEXT, Image_X I +NT)"); $dbh->do("INSERT INTO Login ($colStr) VALUES ($paramStr)",{},@vals); my $sth=$dbh->prepare("SELECT Password FROM Login WHERE Image_X=?"); $sth->execute(55); $sth->dump_results; $sth->{Active}=0; # temporary workaround for DBD::File bug

There are a couple of perl "tricks" here - I use a hash slice to extract the values from your hash and I use join to create strings of placeholders. In actual practice you probably won't create, insert, and query all in one script (or at least not in the same part of the script) but I've combined them here so you can see the full process.

I hope this helps.


In reply to Re: dbi::anydata by jZed
in thread dbi::anydata by rjsaulakh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-16 05:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found