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

DRIVER PRIVATE METHODS

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

NOTES

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

PERFORMANCE

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

BUGS

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

AUTHOR

226       Matt Sergeant, matt@sergeant.org
227
228       Perl extension functions contributed by Francis J. Lacoste <fla‐
229       coste@logreport.org> and Wolfgang Sourdeau <wolfgang@logreport.org>
230

SEE ALSO

232       DBI.
233
234
235
236perl v5.8.8                       2004-09-10                   DBD::SQLite2(3)
Impressum