Leser: 21
1 2 3
use DBI; my $dbh = DBI->connect( 'DBI:SQLite:/pfad/zu/db', $user, $passwort ) or die $DBI::errstr;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#!/usr/bin/env perl # Core Modules use strict; use warnings; use utf8; use open ':encoding(UTF-8)'; use open ':std'; use DBI; my $dsn = "dbi:SQLite:dbname=person.db"; my $user = ""; my $pass = ""; my $options = { RaiseError => 1, AutoCommit => 1, sqlite_unicode => 1, }; my $dbh = DBI->connect($dsn, $user, $pass, $options); $dbh->do(q{ CREATE TABLE person ( id INTEGER PRIMARY KEY AUTOINCREMENT, first_name VARCHAR(255), last_name VARCHAR(255) ) }); my $stmt = $dbh->prepare(q{INSERT INTO person (first_name, last_name) VALUES (?, ?)}); for my $data ( ['foo','bar'], ['baz','maz'], ['möp','höp'] ) { $stmt->execute(@{$data}[0,1]); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#!/usr/bin/env perl # Core Modules use strict; use warnings; use utf8; use open ':encoding(UTF-8)'; use open ':std'; use DBI; my $dsn = 'dbi:SQLite:dbname=person.db'; my $user = ''; my $pass = ''; my $options = { RaiseError => 1, AutoCommit => 1, sqlite_unicode => 1, }; my $dbh = DBI->connect($dsn, $user, $pass, $options); my $sth = $dbh->prepare(q{SELECT id,first_name,last_name FROM person}); $sth->execute; while ( my $data = $sth->fetchrow_arrayref ) { printf "ID: %s\n", $data->[0]; printf "First Name: %s\n", $data->[1]; printf "Last Name: %s\n", $data->[2]; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
sidburn@sid:~/perl/sqlite$ ll
total 8
-rwxr--r-- 1 sidburn sidburn 719 2009-12-29 10:41 create_db
-rwxr--r-- 1 sidburn sidburn 624 2009-12-29 10:42 read_db
sidburn@sid:~/perl/sqlite$ ./create_db
sidburn@sid:~/perl/sqlite$ ll
total 12
-rwxr--r-- 1 sidburn sidburn 719 2009-12-29 10:41 create_db
-rw-r--r-- 1 sidburn sidburn 3072 2009-12-29 10:44 person.db
-rwxr--r-- 1 sidburn sidburn 624 2009-12-29 10:42 read_db
sidburn@sid:~/perl/sqlite$ file person.db
person.db: SQLite 3.x database
sidburn@sid:~/perl/sqlite$ ./read_db
ID: 1
First Name: foo
Last Name: bar
ID: 2
First Name: baz
Last Name: maz
ID: 3
First Name: möp
Last Name: höp
2009-12-29T09:57:01 Great__perlVielen dank das hilft mir schonmal sehr.
hast du eine Ahnung wie ich es machen muss wenn die DB auf einem externem Server ist? Muss ich mich zuerst dort einloggen und dann die SQLite3 befehle absetzen?