dbi

Perl: Using DBI placeholders for ORDER BY clause

Can I use placeholders in a prepared statement for the order by clause of my query? I'm guessing not, as I tried it, and it didn't seem to work, but it didn't throw any errors either, which seemed strange. Is there a better way to do this, other than just generating an SQL string with the validated inputs? ...

What is an easy way to read a sql server table into a hash in Perl?

I'd like a simple perl script / sub to read a sql server table into a hash, does anyone have a good script or snippet (with 'use's) that can do this given a connection string and table name? Here's a template: sub sqltable { my ($connStr,$table) = @_; my ($user, $password, $host) = ($connectstr =~ m|^(.*)/(.*)@(.*)$|); ...

How can I fetch the last row I inserted using DBI?

How can I fetch the last row that was inserted using DBI (DBD::mysql)? Code sample: my $sth = $dbh->prepare('INSERT INTO a ( x, y, z ) VALUES ( ?, ?, ? )'); $sth->execute( $x, $y, $z ); How can I get access to the data that was inserted by the above prepare statement? I need to get the primary ID (AUTOINCREMENT) value. UPDATE: From...

Does SELECT DISTINCT work with Perl's DBD::CSV?

Hello, I found a SELECT-example on the web. When I try it in my script I get this error-message: Specifying DISTINCT when using aggregate functions isn't reasonable - ignored. at /usr/lib/perl5/site_perl/5.10.0/SQL/Parser.pm line 496. #!/usr/bin/perl use warnings; use strict; use DBI; my $dbh = DBI->connect( "DBI:CSV:", undef, undef,...

How do I tell DBD::mysql where mysql.sock is?

Using DBD::mysql with DBI, I am getting the following error when attempting to connect to the database. DBI connect('database=mydb:host=localhost','someuser',...) failed: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) at ./myscript.pl line 97 Yes MySQL is up and running. The problem is that mysql.sock is ...

What is the difference between DBI and DBD?

Can someone please shed some light on what exactly is DBI and DBD? When should either one be used and the benefits of using one over the other. ...

Do SQL::Statement's REGEX and TRIM work with DBD::CSV?

Hello! The functions "REGEX()" and "TRIM()" in this script don't work as I would expect. The REGEX-function returns always true and the TRIM-function returns the "trim_char", not the trimmed string. (When I write the TRIM-function with FROM instead the "," I get an error message.) #!/usr/bin/perl use warnings; use strict; use 5.010; us...

How can I test if the DBI driver state is within a transaction?

I've got a couple of methods that should be executed only in the case my DBI driver class is currently into a transaction to ensure data integrity. I'm looking to write something like this: sub m{ my ($self , $dbh ) = @_ ; unless( $dbh->isInTransaction()){ die "Use this only within a transaction\n" ; } etc ... } From the ...

How can I export an Oracle table to tab separated values?

I need to export a table in the database to a tab separated values file. I am using DBI on Perl and SQL*Plus. Does it support (DBI or SQL*Plus) exporting and importing to or from TSV files? I can write a code to do my need, But I would like to use a ready made solution if it is available. ...

How do I find a literal % with the LIKE-operator with DBD::CSV?

Hello! How do I find a literal % with the LIKE-operator? #!/usr/bin/perl use warnings; use strict; use DBI; my $table = 'formula'; my $dbh = DBI->connect ( "DBI:CSV:", undef, undef, { RaiseError => 1 } ); my $AoA = [ [ qw( id formula ) ], [ 1, 'a + b' ], [ 2, 'c - d' ], [ 3, 'e * f' ], [ 4, 'g / h...

MySQL comments causing messed up bind parameter count with Perl DBI

I've got the following Perl code which makes a DBI call: my $artsql = q{ *** SNIP A BUNCH OF SQL *** where a.article_id != ? and at.type_name != 'List Element' -- don't get list children and aw.flowstate = 'Published' and a.visible_as_article = 1 } ...

Why does my usage of Perl's DBI->connect fail silently?

Why does this code fail silently? How do I get it to show me exactly what the SQL error is? $dbh=DBI->connect($db_name,$db_user,$db_pass); I modified the code to look like this: $dbh=DBI->connect($db_name,$db_user,$db_pass) or die("could not connect to db: $db_name"); Which instead of allowing me to use $dbh unassigned, it will...

Get column heading names from MySQL result in Ruby

I'm using the Ruby mysql module. I want to print out the results of a query and include the column names. I'm having trouble finding the method to give me an array of these names. I have the values as shown below. result = my.query("select * from foo") result.each do |row| puts row.join(',') end Thanks for the help! ...

How can I insert array data into MySQL in Perl?

I parse a text file with the script below. How to insert the array data to MySQL table? I already learned Perl MySQL DBI connect method. And I can connect to local MySQL DB successfully. I can create the table with MySQL command line. #!C:\Perl\bin\perl.exe use strict; use warnings; while ( <DATA> ) { my @rocks = split(/\s+/, ...

Why doesn't my Perl DBI query return any results in a CGI script?

I'm using DBI for the first time (and not long started Perl [2 weeks]) and I can't seem to get any results from the database. Here's what I have: if( defined( $get{findAllPages} ) && defined( $post{ki} ) ){ my ($database, $hostname, $port, $password, $user ); $database = "#########"; $hostname = "localhost"; $password = "#...

Select query question

Hello! I have a table in a mysql-database with the fields "name", "title", "cd_id", "tracks" The entries look like this: Schubert | Symphonie Nr 7 | 27 | 2 Brahms | Symphonie Nr 1 | 11 | 4 Brahms | Symphonie Nr 2 | 27 | 4 Shostakovich | Jazz Suite Nr 1 | 19 | 3 To get the tracks per cd (cd_id) I have written this script: #!/usr/bin/...

Is there an equivalent of PHP's mysql_real_escape_string() for Perl's DBI?

Could some tell me if there is a function which works the same as PHP's mysql_real_escape_string() for Perl from the DBI module? ...

Why can't I insert HTML into my database?

Hi, I have errors when inserting into my database. I have tried everything to sus this. Someone mentioned usind DBI's trace() to track what is going wrong but i cannot make heads or tails of what is going on. Could someone please take a look and see what they think might be the problem. D BI 1.607-ithread default trace level set to 0x0...

Is MySQL more resistant to SQL injection attack than PostgreSQL (under Perl/DBI) ?

I am reviewing a Linux based perl web application that contains a login handler with the ubiquitous my $sth = $DB->prepare("SELECT password from passwords where userid='$userid'") or die; $sth->execute or die; ... where $userid is initialized from (unsafe, unfiltered) web user input. It is well known that the DBI documentation recomme...

How can I pass MySQL functions as bind parameters in prepared statement?

I'm trying to do this: $sth = $dbi->prepare('INSERT INTO table VALUES (?, ?, ?)'); $sth->execute( $var1, $var2 || 'NOW()', $var3 ); without any luck. Any ideas? ...