1DBD::SQLite2(3)       User Contributed Perl Documentation      DBD::SQLite2(3)
2
3
4

NAME

6       DBD::SQLite2 - Self Contained RDBMS in a DBI Driver (sqlite 2.x)
7

SYNOPSIS

9         use DBI;
10         my $dbh = DBI->connect("dbi:SQLite2:dbname=dbfile","","");
11

DESCRIPTION

13       SQLite is a public domain RDBMS database engine that you can find at
14       http://www.hwaci.com/sw/sqlite/.
15
16       Rather than ask you to install SQLite first, because SQLite is public
17       domain, DBD::SQLite2 includes the entire thing in the distribution. So
18       in order to get a fast transaction capable RDBMS working for your perl
19       project you simply have to install this module, and nothing else.
20
21       SQLite supports the following features:
22
23       Implements a large subset of SQL92
24           See http://www.hwaci.com/sw/sqlite/lang.html for details.
25
26       A complete DB in a single disk file
27           Everything for your database is stored in a single disk file,
28           making it easier to move things around than with DBD::CSV.
29
30       Atomic commit and rollback
31           Yes, DBD::SQLite2 is small and light, but it supports full
32           transactions!
33
34       Extensible
35           User-defined aggregate or regular functions can be registered with
36           the SQL parser.
37
38       There's lots more to it, so please refer to the docs on the SQLite web
39       page, listed above, for SQL details. Also refer to DBI for details on
40       how to use DBI itself.
41

CONFORMANCE WITH DBI SPECIFICATION

43       The API works like every DBI module does. Please see DBI for more
44       details about core features.
45
46       Currently many statement attributes are not implemented or are limited
47       by the typeless nature of the SQLite database.
48

DRIVER PRIVATE ATTRIBUTES

50   Database Handle Attributes
51       sqlite_version
52           Returns the version of the SQLite library which DBD::SQLite2 is
53           using, e.g., "2.8.0".
54
55       sqlite_encoding
56           Returns either "UTF-8" or "iso8859" to indicate how the SQLite
57           library was compiled.
58
59       sqlite_handle_binary_nulls
60           Set this attribute to 1 to transparently handle binary nulls in
61           quoted and returned data.
62
63           NOTE: This will cause all backslash characters ("\") to be doubled
64           up in all columns regardless of whether or not they contain binary
65           data or not. This may break your database if you use it from
66           another application. This does not use the built in
67           sqlite_encode_binary and sqlite_decode_binary functions, which may
68           be considered a bug.
69

DRIVER PRIVATE METHODS

71   $dbh->func('last_insert_rowid')
72       This method returns the last inserted rowid. If you specify an INTEGER
73       PRIMARY KEY as the first column in your table, that is the column that
74       is returned.  Otherwise, it is the hidden ROWID column. See the sqlite
75       docs for details.
76
77   $dbh->func( $name, $argc, $func_ref, "create_function" )
78       This method will register a new function which will be useable in SQL
79       query. The method's parameters are:
80
81       $name
82           The name of the function. This is the name of the function as it
83           will be used from SQL.
84
85       $argc
86           The number of arguments taken by the function. If this number is
87           -1, the function can take any number of arguments.
88
89       $func_ref
90           This should be a reference to the function's implementation.
91
92       For example, here is how to define a now() function which returns the
93       current number of seconds since the epoch:
94
95           $dbh->func( 'now', 0, sub { return time }, 'create_function' );
96
97       After this, it could be use from SQL as:
98
99           INSERT INTO mytable ( now() );
100
101   $dbh->func( $name, $argc, $pkg, 'create_aggregate' )
102       This method will register a new aggregate function which can then used
103       from SQL. The method's parameters are:
104
105       $name
106           The name of the aggregate function, this is the name under which
107           the function will be available from SQL.
108
109       $argc
110           This is an integer which tells the SQL parser how many arguments
111           the function takes. If that number is -1, the function can take any
112           number of arguments.
113
114       $pkg
115           This is the package which implements the aggregator interface.
116
117       The aggregator interface consists of defining three methods:
118
119       new()
120           This method will be called once to create an object which should be
121           used to aggregate the rows in a particular group. The step() and
122           finalize() methods will be called upon the reference return by the
123           method.
124
125       step(@_)
126           This method will be called once for each rows in the aggregate.
127
128       finalize()
129           This method will be called once all rows in the aggregate were
130           processed and it should return the aggregate function's result.
131           When there is no rows in the aggregate, finalize() will be called
132           right after new().
133
134       Here is a simple aggregate function which returns the variance (example
135       adapted from pysqlite):
136
137           package variance;
138
139           sub new { bless [], shift; }
140
141           sub step {
142               my ( $self, $value ) = @_;
143
144               push @$self, $value;
145           }
146
147           sub finalize {
148               my $self = $_[0];
149
150               my $n = @$self;
151
152               # Variance is NULL unless there is more than one row
153               return undef unless $n || $n == 1;
154
155               my $mu = 0;
156               foreach my $v ( @$self ) {
157                   $mu += $v;
158               }
159               $mu /= $n;
160
161               my $sigma = 0;
162               foreach my $v ( @$self ) {
163                   $sigma += ($x - $mu)**2;
164               }
165               $sigma = $sigma / ($n - 1);
166
167               return $sigma;
168           }
169
170           $dbh->func( "variance", 1, 'variance', "create_aggregate" );
171
172       The aggregate function can then be used as:
173
174           SELECT group_name, variance(score) FROM results
175           GROUP BY group_name;
176

NOTES

178       To access the database from the command line, try using dbish which
179       comes with the DBI module. Just type:
180
181         dbish dbi:SQLite:foo.db
182
183       On the command line to access the file foo.db.
184
185       Alternatively you can install SQLite from the link above without
186       conflicting with DBD::SQLite2 and use the supplied "sqlite" command
187       line tool.
188

PERFORMANCE

190       SQLite is fast, very fast. I recently processed my 72MB log file with
191       it, inserting the data (400,000+ rows) by using transactions and only
192       committing every 1000 rows (otherwise the insertion is quite slow), and
193       then performing queries on the data.
194
195       Queries like count(*) and avg(bytes) took fractions of a second to
196       return, but what surprised me most of all was:
197
198         SELECT url, count(*) as count FROM access_log
199           GROUP BY url
200           ORDER BY count desc
201           LIMIT 20
202
203       To discover the top 20 hit URLs on the site (http://axkit.org), and it
204       returned within 2 seconds. I'm seriously considering switching my log
205       analysis code to use this little speed demon!
206
207       Oh yeah, and that was with no indexes on the table, on a 400MHz PIII.
208
209       For best performance be sure to tune your hdparm settings if you are
210       using linux. Also you might want to set:
211
212         PRAGMA default_synchronous = OFF
213
214       Which will prevent sqlite from doing fsync's when writing (which slows
215       down non-transactional writes significantly) at the expense of some
216       peace of mind. Also try playing with the cache_size pragma.
217

BUGS

219       Likely to be many, please use http://rt.cpan.org/ for reporting bugs.
220

AUTHOR

222       Matt Sergeant, matt@sergeant.org
223
224       Perl extension functions contributed by Francis J. Lacoste
225       <flacoste@logreport.org> and Wolfgang Sourdeau <wolfgang@logreport.org>
226

SEE ALSO

228       DBI.
229
230
231
232perl v5.12.0                      2004-09-10                   DBD::SQLite2(3)
Impressum