use DBI;
use Data::Dumper;
my $dbh = DBI->connect(
'dbi:SQLite:dbname=dbfile','','',
# { RaiseError => 1},
);
eval {
$dbh->do('drop table foo');
$dbh->do('create table foo(id int unsigned, name char(64))');
};
my $sth = $dbh->prepare('insert into foo values (?,?)');
$sth->execute(@$_) for [1,'moe'],[2,'curly'],[3,'larry'];
print Dumper $dbh->selectall_arrayref('select * from foo');
However, you may notice from the code that i am supplying the ID. I do not think that SQLite
offers auto-incremented ID's for you.
Alternatively, if you already have your data stored in another format, check out
SQL Fairy before you roll your own.
UPDATE:
That's right ... thanks for pointing out my error (once again!) merlyn. :)
Here is are updated lines for the snippet above:
$dbh->do('create table foo(id integer primary key, name char(64))');
my $sth = $dbh->prepare('insert into foo(name) values (?)');
$sth->execute($_) for qw(moe curry lary);
Much better. ;)
| [reply] [d/l] [select] |
If you are into small scale,rapid application development then SQLite is definately the thing for you.
So what all do you need to get started with an SQLite driven application?
1. Install the DBI (1.39 was the latest last i checked)
2. Install DBD::SQLite (0.29)
3. Install the dbish (11.93)
As jeffa has shown in his examples above ,you can use a script to create a table .
The other way to do this is too run /opt/perl5/bin/dbish
This gives you a shell interface to your database.
Make a file to hold your records e.g touch /record.db
After running the dbish choose the option to connect to the sqlite database (option 3).
Then provide the dsn e.g. dbi:SQLite:/record.db
Once you are connected to the db you can run your create,insert,delete,update commands.
However,if you want to shift records from a flat file into a database it is advisable to write a script .
An example of this is below:
Flat file example
10.50.0.0 255.255.0.0
164.130.4.0 255.255.252.0
164.130.8.0 255.255.248.0
164.130.16.0 255.255.240.0
164.130.32.0 255.255.248.0
164.130.40.0 255.255.252.0
And the script to read it and insert records into a table
use DBI;
my $dir='/ipmap';
my $id=0;
opendir(DIR,$dir);
my @files = grep { $_ ne "." and $_ ne ".." } readdir DIR;
closedir(DIR);
my %attrib = (
PrintError => 0,
RaiseError => 1
);
my $dbh = DBI->connect('dbi:SQLite:ipadd.db',"","",\%attrib);
my $query = $dbh->prepare("insert into siteip (tla,ip,range) values (?
+,?,?)");
print "@files\n";
foreach my $file (@files)
{
chomp($file);
my @ins='';
my($tla,undef)=split(/\./,$file);
print "Processing for $tla\n";
open(FI,"$dir\/$file")|| die $!;
while(<FI>)
{
$id=+1;
next if m/^\s*$/ or m/^\s*#/;
my $line=$_;
@ins = split(/\s+/,$line);
# print "Inserting $tla $ins[0] $ins[1]";
$query->execute("$tla","$ins[0]","$ins[1]");
}
}
$dbh->disconnect;
Hope this helps in your first attemp at using SLite.
cheers
chimni
| [reply] [d/l] [select] |
$dbh = DBI->connect('dbi:AnyData(RaiseError=>1):');
$dbh->func( 'rdbtable', 'ARRAY', @rdb, 'ad_import');
$dbh->func( 'asitable', 'ARRAY', @asi, 'ad_import');
$dbh->func( 'bomtable', 'ARRAY', @bom, 'ad_import');
$dbh->func( 'mpttable', 'ARRAY', @mpt, 'ad_import');
$rdbtable_sth = $dbh->prepare( "SELECT SIGNAL_NAME,LOCATION,X_COORDINA
+TE,Y_COORDINATE,Z_COORDINATE,COMPONENT_SIDE,ETCH_SIDE,GROUND,GROUND_L
+OCATION FROM rdbtable" );
$asitable_sth = $dbh->prepare( "SELECT SIGNAL_NAME,LOCATION,REFERENC
+E_DESIGNATOR,PIN_NUMBER,DEVICE_NAME,PACKAGE_TYPE,CS_PART_NUMBER FROM
+asitable WHERE LOCATION = ?" );
$rdbtable_sth->execute;
while (($location,$rdb_signal_name) = $rdbtable_sth->fetchrow_array) {
$asitable_sth->execute($location);
$row = $asitable_sth->fetchrow_arrayref;
$asi_signal_name = $row ? $row->[0] : '';
print "$rdb_signal_name : $asi_signal_name\n";
}
Here are the 2 tables:
rdbtable:
SIGNAL_NAME,LOCATION,X_COORDINATE,Y_COORDINATE,Z_COORDINATE,COMPONENT_SIDE,ETCH_SIDE,GROUND,GROUND_LOCATION
A_AD0,BP1.19E25,-895.0390,3277.3700,0.0000,0,1,GND,BP1.19245
A_AD0,F6_54.W4VIA,1825.0000,3975.0000,0.0000,0,1,GND,F6_54.W7VIA
A_AD1,F6_54.Y3VIA,1775.0000,3925.0000,0.0000,0,1,GND,F6_54.AB4VIA
A_AD1,BP1.18A3,-1210.0000,3513.5910,0.0000,0,1,,
A_AD2,BP1.19C25,-1052.5200,3277.3700,0.0000,0,1,GND,BP1.19254
A_AD2,F6_54.W3VIA,1825.0000,3925.0000,0.0000,0,1,GND,F6_54.AB4VIA
asitable:
SIGNAL_NAME,LOCATION,REFERENCE_DESIGNATOR,PIN_NUMBER,DEVICENAME,PACKAGE_TYPE,CS_PART_NUMBER
A_AD0,F6_54.W4VIA,F6_54,W4,XC2V4000,BGA957XBTF-XC2V4000_9B,059-000-408
A_AD1,F6_54.Y3VIA,F6_54,Y3,XC2V4000,BGA957XBTF-XC2V4000_9B,059-000-408
A_AD2,F6_54.W3VIA,F6_54,W3,XC2V4000,BGA957XBTF-XC2V4000_9B,059-000-408
A_AD3,F6_54.AA5VIA,F6_54,AA5,XC2V4000,BGA957XBTF-XC2V4000_9B,059-000-408
A_AD4,F6_54.Y5VIA,F6_54,Y5,XC2V4000,BGA957XBTF-XC2V4000_9B,059-000-408
A_AD5,F6_54.Y6VIA,F6_54,Y6,XC2V4000,BGA957XBTF-XC2V4000_9B,059-000-408
Any suggestions for fixing what I have or better ways to handle this situation would be greatly appreciated.
Thanks again,
Jim
| [reply] [d/l] |
$test[0][0] = 'No';
$test[0][1] = 'Letter';
$test[0][2] = 'Roman';
$test[1][0] = 1;
$test[1][1] = 'a';
$test[1][2] = 'i';
$test[2][0] = 2;
$test[2][1] = 'b';
$test[2][2] = 'ii';
$test[3][0] = 3;
$test[3][1] = 'c';
$test[3][2] = 'iii';
$test[4][0] = 4;
$test[4][1] = 'd';
$test[4][2] = 'iv';
$test[5][0] = 5;
$test[5][1] = 'e';
$test[5][2] = 'v';
$test[6][0] = 6;
$test[6][1] = 'f';
$test[6][2] = 'vi';
I don't currently see what the difference is from the following (which works):
@test = [
['No','Letter','Roman'],
[1,'a','i'],
[2,'b','ii'],
[3,'c','iii'],
[4,'d','iv'],
[5,'e','v'],
[6,'f','vi']
];
| [reply] [d/l] [select] |