Perl to SQLite bridge is not working, database connect fails ....

Discussion in 'Perl Misc' started by vishwanathsen03@gmail.com, Apr 8, 2014.

  1. Guest

    Hi,

    I have installed the perl modules -
    DBI
    DBD::SQLite

    Now I am running a simple perl script that calls sqlite database.

    #!/usr/bin/perl

    use DBI;
    use strict;

    my $driver = "SQLite";
    my $database = "ex2.db";
    my $dsn = "DBI:$driver:dbname=$database";
    my $userid = "";
    my $password = "";
    my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
    or die $DBI::errstr;

    print "Opened database successfully\n";


    Perl script fails on the line that does connect (DBI->connect) with the message -

    DBI connect('dbname=ex2.db','',...) failed: database disk image is malformed at perl_sqllite_example1.pl line 11

    SQLite and database seems to be fine, I could open/load the database using sqlite3 and I could do insert and select operations.
    I could write a C program and I could use the C program to do an insert operation successfully in the database.

    So it is the perl to sqlite bridge that has issues....

    Kindly suggest.....


    Regards,
    Vishwanath
    , Apr 8, 2014
    #1
    1. Advertising

  2. John Bokma Guest

    writes:

    > Hi,
    >
    > I have installed the perl modules -
    > DBI
    > DBD::SQLite
    >
    > Now I am running a simple perl script that calls sqlite database.
    >
    > #!/usr/bin/perl
    >
    > use DBI;
    > use strict;
    >
    > my $driver = "SQLite";
    > my $database = "ex2.db";
    > my $dsn = "DBI:$driver:dbname=$database";
    > my $userid = "";
    > my $password = "";
    > my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
    > or die $DBI::errstr;
    >
    > print "Opened database successfully\n";
    >
    >
    > Perl script fails on the line that does connect (DBI->connect) with the message -
    >
    > DBI connect('dbname=ex2.db','',...) failed: database disk image is malformed at perl_sqllite_example1.pl line 11
    >
    > SQLite and database seems to be fine, I could open/load the database using sqlite3 and I could do insert and select operations.
    > I could write a C program and I could use the C program to do an insert operation successfully in the database.
    >
    > So it is the perl to sqlite bridge that has issues....


    Which version did you use to create the database file (it sounds like
    version 2)? You might have to pass legacy_file_format, see documentation
    of DBD::SQLite.

    --
    John Bokma j3b

    Blog: http://johnbokma.com/ Perl Consultancy: http://castleamber.com/
    Perl for books: http://johnbokma.com/perl/help-in-exchange-for-books.html
    John Bokma, Apr 8, 2014
    #2
    1. Advertising

  3. Re: Perl to SQLite bridge is not working, database connect fails....

    # take a working sample from one of my scripts and adapt it ...


    use strict;
    use warnings;
    use DBI qw:)sql_types); # για blobs


    my $dbh = DBI->connect('dbi:SQLite:dbname=C:/Program
    Files/WebApp/sites/database/sms.db', undef, undef, {AutoCommit=>1,
    RaiseError=>1, sqlite_unicode=>1}) or die "$DBI::errstr\n";;
    $dbh->do('PRAGMA foreign_keys = OFF');
    $dbh->do('PRAGMA cache_size = 1000');
    $dbh->do('PRAGMA synchronous = OFF');
    $dbh->do('PRAGMA sqlite_allow_multiple_statements = OFF')
    $dbh->{'sqlite_see_if_its_a_number'} = 1;
    $dbh->{'sqlite_use_immediate_transaction'} = 0;


    my $sth = $dbh->prepare('SELECT provider_name, account_username FROM
    Account WHERE webapp_username == ?1 ORDER BY
    provider_name,account_username') or die "$DBI::errstr\n";
    $sth->bind_param(1, 'user1', SQL_VARCHAR);

    $sth->execute() or die "$DBI::errstr\n";
    my ($provider, $username);

    $sth->bind_columns(\$provider,\$username);

    while ( $sth->fetchrow_array )
    {
    print "$provider,$username\n";
    }


    $dbh->disconnect;
    George Mpouras, Apr 8, 2014
    #3
  4. John Bokma Guest

    George Mpouras <> writes:

    > $dbh->do('PRAGMA foreign_keys = OFF');
    > $dbh->do('PRAGMA cache_size = 1000');
    > $dbh->do('PRAGMA synchronous = OFF');
    > $dbh->do('PRAGMA sqlite_allow_multiple_statements = OFF')


    Which I probably would write as:

    $dbh->do( "PRAGMA $_->[0] = $_->[1]" ) for (
    [ foreign_keys => 'OFF' ],
    [ cache_size => 1000 ],
    [ synchonous => 'OFF' ],
    [ sqlite_allow_multiple_statements => 'OFF' ]
    );

    But I am afraid your working example is not going to work as well, at
    least if I am correct that it's an SQLite version issue.

    --
    John Bokma j3b

    Blog: http://johnbokma.com/ Perl Consultancy: http://castleamber.com/
    Perl for books: http://johnbokma.com/perl/help-in-exchange-for-books.html
    John Bokma, Apr 9, 2014
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carl Youngblood
    Replies:
    1
    Views:
    210
    Carl Youngblood
    Apr 9, 2005
  2. Evan Weaver

    Perl bridge module for RubyInline?

    Evan Weaver, Feb 26, 2007, in forum: Ruby
    Replies:
    3
    Views:
    107
    Daniel Berger
    Feb 27, 2007
  3. Replies:
    4
    Views:
    334
  4. Replies:
    4
    Views:
    690
    Abhinav
    Mar 3, 2004
  5. Replies:
    2
    Views:
    268
Loading...

Share This Page