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

Re: DBI::Shell sqlite dbh->func(

by NetWallah (Abbot)
on Oct 13, 2013 at 02:12 UTC ( #1058030=note: print w/ replies, xml ) Need Help??


in reply to DBI::Shell sqlite dbh->func(

It looks like the third parameter to func() should be a subref, not the string 'my_perl_func'.

I've been looking for a reason/excuse to try this, so, please post progress on this project.

             When in doubt, mumble; when in trouble, delegate; when in charge, ponder. -- James H. Boren


Comment on Re: DBI::Shell sqlite dbh->func(
Re^2: DBI::Shell sqlite dbh->func(
by rodinski (Initiate) on Oct 13, 2013 at 23:04 UTC
    This code below seems to work for me.

    If I try it without the single quotes within the func declaration, I get a strict subs error

    Now how to get DBI::Shell to interact with the user so they have access to this function?

    #!/usr/bin/perl -w # create a SQLite test database use strict; use DBI; my $db = shift || 'no_name.splite'; my $dbh = DBI->connect("dbi:SQLite:$db", "","", {RaiseError => 1, sqlite_see_if_its_a_number => 1, PrintError=>0}) or die "can't connect\n"; $dbh->func('sqrt_s_s', -1, 'sqrt_sum_of_sqrs', 'create_function'); #ne +ed length function sub sqrt_sum_of_sqrs { my $sum_sqrs = 0.0; map {$sum_sqrs += $_**2} @_; return sqrt($sum_sqrs); } my $sth; my @rowa = $dbh->selectrow_array("select 3 as A,4 as B, sqrt_s_s(3,4) +as C"); print join ("\t",@rowa) ;

      If I try it without the single quotes within the func declaration, I get a strict subs error

      Something like this?

      #!/bin/env perl # create a SQLite test database use strict; use warnings; use DBI; my $db = shift @ARGV || 'no_name.splite'; my $dbh = DBI->connect( "dbi:SQLite:$db", "", "", { RaiseError => 1, sqlite_see_if_its_a_number => 1, PrintError => 0 } ); $dbh->func('sqrt_s_s', -1, \&sqrt_sum_of_sqrs, 'create_function'); # n +eed length function my @rowa = $dbh->selectrow_array( "select 3 as A, 4 as B, sqrt_s_s( 3, + 4 ) as C" ); print join( "\t", @rowa ), "\n"; sub sqrt_sum_of_sqrs { my $sum_sqrs = 0.0; map { $sum_sqrs += $_ ** 2 } @_; return sqrt( $sum_sqrs ); }

      Update: So now you need to research three things.

      1. How would you prompt a user for input?
      2. How to get the user input?
      3. How do you pass the user input into your function?

        My past research peaked my interest in the command line tool dbish. So my answers are as follows.

        >Update: So now you need to research three things.

        >How would you prompt a user for input?

        via dbish (or DBI::Shell interface)

        >How to get the user input?

        via dbish (or DBI::Shell interface)

        >How do you pass the user input into your function?

        I'm thinking that there should already be code that has performed the ->func method.

        I'm hoping to be able to type into the dbish interface something like this:

        select 3 as oposite, 4 as adjacent, sqrt(3,4) as hyp

        and get

        3 4 5

        as a result.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-12-29 02:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls