Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

cgi can't write to sqlite database

by Anonymous Monk
on Jul 03, 2008 at 20:42 UTC ( #695467=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

O monks,

I have a little embedded database app that is purely for my own use, implemented as a cgi. I'm running apache on ubuntu, and the database is a sqlite file, accessed through the DBI package. I got the whole thing working great for read access. However, I can't seem to set it up so that apache can write to the sqlite file. Here's the initial version of my code that runs when I fill in a form and submit it:

#!/usr/bin/perl # requires DBD::SQLite use DBI; use strict; print html_header(); my $db = "/home/bcrowell/Documents/writing/selling_fiction/stories.sql +t"; my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",{ RaiseError => 1 +, AutoCommit => 1 }) or err("connecting"); print html_footer(); local $/; my $stuff = <>; my %inputs; foreach my $input(split(/&/,$stuff)) { $input =~ /(.*)=(.*)/; $inputs{$1} = $2; } # magazine, story, date my $magazine = $inputs{'magazine'}; # key my $story = $inputs{'story'}; my $date = $inputs{'date'}; print "$magazine, $story, $date\n"; my $insert_statement = <<INSERT; insert into submissions (magazine,story,date_submitted) values ($mag +azine,$story,$date) INSERT $dbh->do($insert_statement); $dbh->disconnect;

Here's the message that shows up in my apache error log:

DBD::SQLite::db do failed: unable to open database file(14) at dbdimp. +c line 403 at /usr/lib/cgi-bin/record_submission.cgi line 33, <> chun +k 1. DBD::SQLite::db do failed: unable to open database file(14) at dbdimp. +c line 403 at /usr/lib/cgi-bin/record_submission.cgi line 33, <> chun +k 1.

The line it's referring to is the $dbh->do(...).

I've tried setting the file to be globally writeable. I've tried moving the file out of my home directory and into the cgi-bin directory. Neither of those things seemed to help. I'm guessing that this is something related to security that apache is doing. This is just running on my desktop machine, however, and I'm behind my router's built-in firewall, so I'm not worried about the security of the script.

Any suggestions would be much appreciated!

Replies are listed 'Best First'.
Re: cgi can't write to sqlite database
by moritz (Cardinal) on Jul 03, 2008 at 21:06 UTC
    my $db = "/home/bcrowell/Documents/writing/selling_fiction/stories.sqlt";

    I doubt that Apache has access to all this directory, it would require read and executable bits (iirc) set on all these dirs.

    Try to place a copy in /tmp/ and make it globally writeable. If it still doesn't work, and you're not using selinux or some other security enhancements, it's not a permission problem.

      That was it -- thanks!!
      Thanks this solved my problem also. I too had the right permissions on the file and containing directory but not on all directories in the path.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://695467]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-05-19 16:49 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (72 votes). Check out past polls.