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.sqlite.org/.
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       For real work please use the updated DBD::SQLite driver with the up-to-
22       date sqlite3 backend.  SQLite2 supports the following features:
23
24       Implements a large subset of SQL92
25           See http://www.sqlite.org/lang.html for details.
26
27       A complete DB in a single disk file
28           Everything for your database is stored in a single disk file,
29           making it easier to move things around than with DBD::CSV.
30
31       Atomic commit and rollback
32           Yes, DBD::SQLite2 is small and light, but it supports full
33           transactions
34
35       Extensible
36           User-defined aggregate or regular functions can be registered with
37           the SQL parser.
38
39       There's lots more to it, so please refer to the docs on the SQLite web
40       page, listed above, for SQL details. Also refer to DBI for details on
41       how to use DBI itself.
42

CONFORMANCE WITH DBI SPECIFICATION

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

DRIVER PRIVATE ATTRIBUTES

51   Database Handle Attributes
52       sqlite_version
53           Returns the version of the SQLite library which DBD::SQLite2 is
54           using, i.e, "2.8.15".
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
69           may be considered a bug.
70

DRIVER PRIVATE METHODS

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

NOTES

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

PERFORMANCE

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

BUGS

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

AUTHOR

223       Matt Sergeant, matt@sergeant.org
224
225       Perl extension functions contributed by Francis J. Lacoste
226       <flacoste@logreport.org> and Wolfgang Sourdeau
227       <wolfgang@logreport.org>.  Maintenance help by Reini Urban
228       <rurban@cpan.org>
229

LICENSE

231       This module is available under the same licences as perl, the Artistic
232       license and the GPL.
233

SEE ALSO

235       DBD::SQLite, DBI.
236
237
238
239perl v5.38.0                      2023-07-20                   DBD::SQLite2(3)
Impressum