LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-09-2011, 04:36 PM   #1
totaluser
LQ Newbie
 
Registered: Nov 2011
Posts: 28

Rep: Reputation: Disabled
Perl connect to SQLite database - strange behaviour


Hello everybody,
I created a small SQLite database that I want to use in my Perl script

Code:
#!/usr/bin/perl -w

 use strict;
 use warnings;
 use DBI;
 use DBD::SQLite;
 use POSIX 'strftime';

 my $now = strftime "%d%m%Y_%H%M%S", localtime;

 my $db_handle=DBI->connect("dbi:SQLite:dbname=myDB.db") == 0  or die "$DBI::errstr\n";


 $db_handle->do( "DROP TABLE routers" ) or die "$DBI::errstr\n";
 $db_handle->do( "DROP TABLE modules" ) or die "$DBI::errstr\n";

 $db_handle->do( "CREATE TABLE routers(NAME VARCHAR(20) PRIMARY KEY, IOS VARCHAR(200), RAM INTEGER, FLASH INTEGER, WLC VARCHAR(20), AIM VARCHAR(20))" ) or die "$DBI::errstr\n";
 $db_handle->do( "CREATE TABLE modules(NAME VARCHAR(200), DESCR VARCHAR(200), PID VARCHAR(200), VID VARCHAR(3), SN VARCHAR(11) PRIMARY KEY, ROUTER VARCHAR(20), FOREIGN KEY(ROUTER) REFERENCES routers(NAME))" ) or die "$DBI::errstr\n";



 system("sqlite3", "myDB.db", ".import A512_routers.table routers") == 0 or die "Import of routers table failed";
 system("sqlite3", "myDB.db", ".import A512_modules.table modules") == 0 or die "Import of modules table failed";

 $db_handle->do( "CREATE TABLE routers_$now AS SELECT * FROM routers" ) or die "$DBI::errstr\n";
 $db_handle->do( "CREATE TABLE modules_$now AS SELECT * FROM modules" ) or die "$DBI::errstr\n";
 $db_handle->disconnect;
if I run it like this, the script fails at this line
Code:
 my $db_handle=DBI->connect("dbi:SQLite:dbname=myDB.db") == 0  or die "$DBI::errstr\n";
telling me
Use of uninitialized value in concatenation (.) or string at ./test.pl line 12.

line 12 is the line I am showing you

on the other hand if I remove the die part so it looks like this
Code:
my $db_handle=DBI->connect("dbi:SQLite:dbname=myDB.db");
the program runs fine and the data is indeed imported and new tables are created


I am interested in your opinions why this is happening and what is causing it

thank you very much for your replies in advance
 
Old 11-09-2011, 04:52 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Try removing the quotes around "$DBI::errstr\n";

PHP Code:
my $db_handle=DBI->connect("dbi:SQLite:dbname=myDB.db"
    or die 
$DBI::errstr "\n"
 
Old 11-09-2011, 05:04 PM   #3
totaluser
LQ Newbie
 
Registered: Nov 2011
Posts: 28

Original Poster
Rep: Reputation: Disabled
after removing quotes like you suggested it still displays the same message as shown in 1st post

if I modify it to
Code:
my $db_handle=DBI->connect("dbi:SQLite:dbname=myDB.db") or die $DBI::errstr ;
then the message changes to
Use of uninitialized value in die at ./test.pl line 12.
Died at ./test.pl line 12.

Last edited by totaluser; 11-09-2011 at 05:17 PM. Reason: //inconsistent script name
 
Old 11-09-2011, 05:08 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Uh ? Maybe this would work ?
(I noticed the script name, bad karma...)

PHP Code:
my $db_handle=DBI->connect("dbi:SQLite:dbname=myDB.db") or die DBI->errstr 
 
Old 11-09-2011, 05:16 PM   #5
totaluser
LQ Newbie
 
Registered: Nov 2011
Posts: 28

Original Poster
Rep: Reputation: Disabled
sorry for inconsistent script name

even after removing that $ sign and . "\n" it still is the same
Use of uninitialized value in die at ./test.pl line 12.
Died at ./test.pl line 12.


and with . "\n"
Use of uninitialized value in concatenation (.) or string at ./test.pl line 12.
 
Old 11-09-2011, 05:29 PM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Are you sure it works with "dbi:SQLite:dbname=myDB.db" ? (I notice the ':', dunno if it shouldn't be a ';' instead)

PHP Code:
my $db_handle=DBI->connect("dbi:SQLite;dbname=myDB.db") or die DBI->errstr
[edit]
No, it should be "dbi:SQLite:dbname=myDB.db" :/
http://search.cpan.org/~msergeant/DB.../DBD/SQLite.pm

Maybe complete the extra arguments for the connect method

PHP Code:
my $db_handle=DBI->connect("dbi:SQLite:dbname=myDB.db""""") or die DBI->errstr
[edit]
After a few test, this method call works:
PHP Code:
my $db_handle=DBI->connect("dbi:SQLite:dbname=myDB.db"
    or die 
DBI->errstr "\n"
However I can reproduce your error with:
PHP Code:
my $db_handle=DBI->connect("dbi:SQLite:dbname=myDB.db") == 
    
or die DBI->errstr "\n"

Last edited by Cedrik; 11-09-2011 at 06:15 PM.
 
1 members found this post helpful.
Old 11-10-2011, 12:59 AM   #7
totaluser
LQ Newbie
 
Registered: Nov 2011
Posts: 28

Original Poster
Rep: Reputation: Disabled
Thank you very for the time you spent solving my problem

now it works fine on my testing server (Perl 5.8.8, Perld-DBI 1.607, Perl-DBD-SQLite 1.14) and hopefully it will work on a production server as well once appropriate modules are installed.



Thanks again
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
qdevelop can't connect database and needs sqlite support. mei0fei Linux - Software 1 08-26-2011 07:59 PM
[SOLVED] perl connect to sqlite ted_chou12 Linux - Software 1 04-22-2011 04:25 AM
LXer: Sqlite-Commander - A ncurses based tool to display the records and tables of a sqlite database LXer Syndicated Linux News 0 01-02-2011 08:11 AM
Sqlite Database image malformed ravindranvasudevan Programming 6 08-10-2009 03:37 AM
SQLite database backup ddpicard Linux - Software 2 11-01-2004 06:54 PM


All times are GMT -5. The time now is 01:37 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration