sqlite3-ruby 1.2.3-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sqlite3-ruby might be problematic. Click here for more details.

@@ -0,0 +1,51 @@
1
+ = SQLite3/Ruby Interface
2
+
3
+ This module allows Ruby programs to interface with the SQLite3
4
+ database engine (http://www.sqlite.org). You must have the
5
+ SQLite engine installed in order to build this module.
6
+
7
+ Note that this module is NOT compatible with SQLite 2.x.
8
+
9
+
10
+ == Compilation and Installation
11
+
12
+ Simply do the following, after installing SQLite3:
13
+
14
+ ruby setup.rb config
15
+ ruby setup.rb setup
16
+ ruby setup.rb install
17
+
18
+ Alternatively, you can download and install the RubyGem package for
19
+ SQLite3/Ruby (you must have RubyGems and SQLite3 installed, first):
20
+
21
+ gem install sqlite3-ruby
22
+
23
+ If you have sqlite3 installed in a non-standard location, you can specify the location of the include and lib files by doing:
24
+
25
+ gem install sqlite3-ruby -- --with-sqlite3-include=/opt/local/include \
26
+ --with-sqlite3-lib=/opt/local/lib
27
+
28
+ Also, the gem ships with the C source-code pre-built, so (as of version 1.1.1)
29
+ you no longer need to have SWIG installed. However, if you have SWIG installed
30
+ and you want to generate the C file yourself, you can specify the
31
+ <code>--with-swig</code> option.
32
+
33
+ == Usage
34
+
35
+ For help figuring out the SQLite3/Ruby interface, check out the
36
+ FAQ[http://sqlite-ruby.rubyforge.org/sqlite3/faq.html]. It includes examples of
37
+ usage. If you have any questions that you feel should be address in the
38
+ FAQ, please send them to jamis@37signals.com
39
+
40
+ == Source Code
41
+
42
+ The source repository is accessible via git:
43
+
44
+ git clone git://github.com/jamis/sqlite3-ruby.git
45
+
46
+ == Contact Information
47
+
48
+ The project page is http://rubyforge.org/projects/sqlite-ruby. There, you can
49
+ find links to mailing lists and forums that you can use to discuss this
50
+ library. Additionally, there are trackers for submitting bugs and feature
51
+ requests. Feel free to use them!
@@ -0,0 +1,408 @@
1
+ <html>
2
+ <head>
3
+ <title>SQLite3/Ruby FAQ</title>
4
+ <style type="text/css">
5
+ a, a:visited, a:active {
6
+ color: #00F;
7
+ text-decoration: none;
8
+ }
9
+
10
+ a:hover {
11
+ text-decoration: underline;
12
+ }
13
+
14
+ .faq-list {
15
+ color: #000;
16
+ font-family: vera-sans, verdana, arial, sans-serif;
17
+ }
18
+
19
+ .faq-title {
20
+ background: #007;
21
+ color: #FFF;
22
+ font-family: vera-sans, verdana, arial, sans-serif;
23
+ padding-left: 1em;
24
+ padding-top: 0.5em;
25
+ padding-bottom: 0.5em;
26
+ font-weight: bold;
27
+ font-size: large;
28
+ border: 1px solid #000;
29
+ }
30
+
31
+ .faq-answer {
32
+ margin-left: 1em;
33
+ color: #000;
34
+ font-family: vera-sans, verdana, arial, sans-serif;
35
+ }
36
+
37
+ .faq-answer pre {
38
+ margin-left: 1em;
39
+ color: #000;
40
+ background: #FFE;
41
+ font-size: normal;
42
+ border: 1px dotted #CCC;
43
+ padding: 1em;
44
+ }
45
+
46
+ h1 {
47
+ background: #005;
48
+ color: #FFF;
49
+ font-family: vera-sans, verdana, arial, sans-serif;
50
+ padding-left: 1em;
51
+ padding-top: 1em;
52
+ padding-bottom: 1em;
53
+ font-weight: bold;
54
+ font-size: x-large;
55
+ border: 1px solid #00F;
56
+ }
57
+ </style>
58
+ </head>
59
+ <body>
60
+ <h1>SQLite/Ruby FAQ</h1>
61
+ <div class="faq-list">
62
+ <ul>
63
+ <li>How do I do a database query?
64
+ <ul>
65
+ <li><a href='#9144150'>I just want an array of the rows&#8230;</a></li>
66
+ <li><a href='#9144080'>I&#8217;d like to use a block to iterate through the rows&#8230;</a></li>
67
+ <li><a href='#9144010'>I need to get the column names as well as the rows&#8230;</a></li>
68
+ <li><a href='#9143940'>I just want the first row of the result set&#8230;</a></li>
69
+ <li><a href='#9143870'>I just want the first value of the first row of the result set&#8230;</a></li>
70
+ </ul>
71
+ </li>
72
+ <li><a href='#9143750'>How do I prepare a statement for repeated execution?</a></li>
73
+ <li><a href='#9143680'>How do I use placeholders in an <span class="caps">SQL</span> statement?</a></li>
74
+ <li><a href='#9143610'>How do I discover metadata about a query?</a></li>
75
+ <li><a href='#9143540'>I&#8217;d like the rows to be indexible by column name.</a></li>
76
+ <li><a href='#9143470'>I&#8217;d like the values from a query to be the correct types, instead of String.</a></li>
77
+ <li><a href='#9143400'>How do insert binary data into the database?</a></li>
78
+ <li><a href='#9143330'>How do I do a <span class="caps">DDL</span> (insert, update, delete) statement?</a></li>
79
+ <li><a href='#9143260'>How do I execute multiple statements in a single string?</a></li>
80
+ <li><a href='#9143190'>How do I begin/end a transaction?</a></li>
81
+ </ul>
82
+ </div>
83
+ <a name='9144150'></a>
84
+ <div class='faq-title'>How do I do a database query? I just want an array of the rows&#8230;</div>
85
+ <div class='faq-answer'><p>Use the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> method. If you don&#8217;t give it a block, it will return an array of all the rows:</p>
86
+
87
+
88
+ <pre>
89
+ require 'sqlite3'
90
+
91
+ db = SQLite3::<a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database</a>.new( "test.db" )
92
+ rows = db.execute( "select * from test" )
93
+ </pre></div>
94
+ <a name='9144080'></a>
95
+ <div class='faq-title'>How do I do a database query? I&#8217;d like to use a block to iterate through the rows&#8230;</div>
96
+ <div class='faq-answer'><p>Use the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> method. If you give it a block, each row of the result will be yielded to the block:</p>
97
+
98
+
99
+ <pre>
100
+ require 'sqlite3'
101
+
102
+ db = SQLite3::<a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database</a>.new( "test.db" )
103
+ db.execute( "select * from test" ) do |row|
104
+ ...
105
+ end
106
+ </pre></div>
107
+ <a name='9144010'></a>
108
+ <div class='faq-title'>How do I do a database query? I need to get the column names as well as the rows&#8230;</div>
109
+ <div class='faq-answer'><p>Use the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute2</a> method. This works just like <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a>; if you don&#8217;t give it a block, it returns an array of rows; otherwise, it will yield each row to the block. <em>However</em>, the first row returned is always an array of the column names from the query:</p>
110
+
111
+
112
+ <pre>
113
+ require 'sqlite3'
114
+
115
+ db = SQLite3::<a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database</a>.new( "test.db" )
116
+ columns, *rows = db.execute2( "select * from test" )
117
+
118
+ # or use a block:
119
+
120
+ columns = nil
121
+ db.execute2( "select * from test" ) do |row|
122
+ if columns.nil?
123
+ columns = row
124
+ else
125
+ # process row
126
+ end
127
+ end
128
+ </pre></div>
129
+ <a name='9143940'></a>
130
+ <div class='faq-title'>How do I do a database query? I just want the first row of the result set&#8230;</div>
131
+ <div class='faq-answer'><p>Easy. Just call <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#get_first_row</a>:</p>
132
+
133
+
134
+ <pre>
135
+ row = db.get_first_row( "select * from table" )
136
+ </pre>
137
+
138
+ <p>This also supports bind variables, just like <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> and friends.</p></div>
139
+ <a name='9143870'></a>
140
+ <div class='faq-title'>How do I do a database query? I just want the first value of the first row of the result set&#8230;</div>
141
+ <div class='faq-answer'><p>Also easy. Just call <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#get_first_value</a>:</p>
142
+
143
+
144
+ <pre>
145
+ count = db.get_first_value( "select count(*) from table" )
146
+ </pre>
147
+
148
+ <p>This also supports bind variables, just like <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> and friends.</p></div>
149
+ <a name='9143750'></a>
150
+ <div class='faq-title'>How do I prepare a statement for repeated execution?</div>
151
+ <div class='faq-answer'><p>If the same statement is going to be executed repeatedly, you can speed things up a bit by <em>preparing</em> the statement. You do this via the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#prepare</a> method. It returns a <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement</a> object, and you can then invoke #execute on that to get the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/ResultSet.html'>ResultSet</a>:</p>
152
+
153
+
154
+ <pre>
155
+ stmt = db.prepare( "select * from person" )
156
+
157
+ 1000.times do
158
+ stmt.execute do |result|
159
+ ...
160
+ end
161
+ end
162
+
163
+ stmt.close
164
+
165
+ # or, use a block
166
+
167
+ db.prepare( "select * from person" ) do |stmt|
168
+ 1000.times do
169
+ stmt.execute do |result|
170
+ ...
171
+ end
172
+ end
173
+ end
174
+ </pre>
175
+
176
+ <p>This is made more useful by the ability to bind variables to placeholders via the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#bind_param</a> and <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#bind_params</a> methods. (See the next <span class="caps">FAQ</span> for details.)</p></div>
177
+ <a name='9143680'></a>
178
+ <div class='faq-title'>How do I use placeholders in an <span class="caps">SQL</span> statement?</div>
179
+ <div class='faq-answer'><p>Placeholders in an <span class="caps">SQL</span> statement take any of the following formats:</p>
180
+
181
+
182
+ <ul>
183
+ <li><code>?</code></li>
184
+ <li><code>?_nnn_</code></li>
185
+ <li><code>:_word_</code></li>
186
+ </ul>
187
+
188
+
189
+ <p>Where <em>n_ is an integer, and _word</em> is an alpha-numeric identifier (or number). When the placeholder is associated with a number, that number identifies the index of the bind variable to replace it with. When it is an identifier, it identifies the name of the correponding bind variable. (In the instance of the first format-<del>a single question mark</del>-the placeholder is assigned a number one greater than the last index used, or 1 if it is the first.)</p>
190
+
191
+
192
+ <p>For example, here is a query using these placeholder formats:</p>
193
+
194
+
195
+ <pre>
196
+ select *
197
+ from table
198
+ where ( c = ?2 or c = ? )
199
+ and d = :name
200
+ and e = :1
201
+ </pre>
202
+
203
+ <p>This defines 5 different placeholders: 1, 2, 3, and &#8220;name&#8221;.</p>
204
+
205
+
206
+ <p>You replace these placeholders by <em>binding</em> them to values. This can be accomplished in a variety of ways.</p>
207
+
208
+
209
+ <p>The <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a>, and <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute2</a> methods all accept additional arguments following the <span class="caps">SQL</span> statement. These arguments are assumed to be bind parameters, and they are bound (positionally) to their corresponding placeholders:</p>
210
+
211
+
212
+ <pre>
213
+ db.execute( "select * from table where a = ? and b = ?",
214
+ "hello",
215
+ "world" )
216
+ </pre>
217
+
218
+ <p>The above would replace the first question mark with &#8216;hello&#8217; and the second with &#8216;world&#8217;. If the placeholders have an explicit index given, they will be replaced with the bind parameter at that index (1-based).</p>
219
+
220
+
221
+ <p>If a Hash is given as a bind parameter, then its key/value pairs are bound to the placeholders. This is how you bind by name:</p>
222
+
223
+
224
+ <pre>
225
+ db.execute( "select * from table where a = :name and b = :value",
226
+ "name" =&gt; "bob",
227
+ "value" =&gt; "priceless" )
228
+ </pre>
229
+
230
+ <p>You can also bind explicitly using the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement</a> object itself. Just pass additional parameters to the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#execute</a> statement:</p>
231
+
232
+
233
+ <pre>
234
+ db.prepare( "select * from table where a = :name and b = ?" ) do |stmt|
235
+ stmt.execute "value", "name" =&gt; "bob"
236
+ end
237
+ </pre>
238
+
239
+ <p>Or do a <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#prepare</a> to get the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement</a>, and then use either <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#bind_param</a> or <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#bind_params</a>:</p>
240
+
241
+
242
+ <pre>
243
+ stmt = db.prepare( "select * from table where a = :name and b = ?" )
244
+
245
+ stmt.bind_param( "name", "bob" )
246
+ stmt.bind_param( 1, "value" )
247
+
248
+ # or
249
+
250
+ stmt.bind_params( "value", "name" =&gt; "bob" )
251
+ </pre></div>
252
+ <a name='9143610'></a>
253
+ <div class='faq-title'>How do I discover metadata about a query?</div>
254
+ <div class='faq-answer'><p>If you ever want to know the names or types of the columns in a result set, you can do it in several ways.</p>
255
+
256
+
257
+ <p>The first way is to ask the row object itself. Each row will have a property &#8220;fields&#8221; that returns an array of the column names. The row will also have a property &#8220;types&#8221; that returns an array of the column types:</p>
258
+
259
+
260
+ <pre>
261
+ rows = db.execute( "select * from table" )
262
+ p rows[0].fields
263
+ p rows[0].types
264
+ </pre>
265
+
266
+ <p>Obviously, this approach requires you to execute a statement that actually returns data. If you don&#8217;t know if the statement will return any rows, but you still need the metadata, you can use <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#query</a> and ask the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/ResultSet.html'>ResultSet</a> object itself:</p>
267
+
268
+
269
+ <pre>
270
+ db.query( "select * from table" ) do |result|
271
+ p result.columns
272
+ p result.types
273
+ ...
274
+ end
275
+ </pre>
276
+
277
+ <p>Lastly, you can use <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#prepare</a> and ask the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement</a> object what the metadata are:</p>
278
+
279
+
280
+ <pre>
281
+ stmt = db.prepare( "select * from table" )
282
+ p stmt.columns
283
+ p stmt.types
284
+ </pre></div>
285
+ <a name='9143540'></a>
286
+ <div class='faq-title'>I&#8217;d like the rows to be indexible by column name.</div>
287
+ <div class='faq-answer'><p>By default, each row from a query is returned as an Array of values. This means that you can only obtain values by their index. Sometimes, however, you would like to obtain values by their column name.</p>
288
+
289
+
290
+ <p>The first way to do this is to set the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database</a> property &#8220;results_as_hash&#8221; to true. If you do this, then all rows will be returned as Hash objects, with the column names as the keys. (In this case, the &#8220;fields&#8221; property is unavailable on the row, although the &#8220;types&#8221; property remains.)</p>
291
+
292
+
293
+ <pre>
294
+ db.results_as_hash = true
295
+ db.execute( "select * from table" ) do |row|
296
+ p row['column1']
297
+ p row['column2']
298
+ end
299
+ </pre>
300
+
301
+ <p>The other way is to use Ara Howard&#8217;s <a href="http://rubyforge.org/projects/arrayfields">ArrayFields</a> module. Just require &#8220;arrayfields&#8221;, and all of your rows will be indexable by column name, even though they are still arrays!</p>
302
+
303
+
304
+ <pre>
305
+ require 'arrayfields'
306
+
307
+ ...
308
+ db.execute( "select * from table" ) do |row|
309
+ p row[0] == row['column1']
310
+ p row[1] == row['column2']
311
+ end
312
+ </pre></div>
313
+ <a name='9143470'></a>
314
+ <div class='faq-title'>I&#8217;d like the values from a query to be the correct types, instead of String.</div>
315
+ <div class='faq-answer'><p>You can turn on &#8220;type translation&#8221; by setting <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#type_translation</a> to true:</p>
316
+
317
+
318
+ <pre>
319
+ db.type_translation = true
320
+ db.execute( "select * from table" ) do |row|
321
+ p row
322
+ end
323
+ </pre>
324
+
325
+ <p>By doing this, each return value for each row will be translated to its correct type, based on its declared column type.</p>
326
+
327
+
328
+ <p>You can even declare your own translation routines, if (for example) you are using an <span class="caps">SQL</span> type that is not handled by default:</p>
329
+
330
+
331
+ <pre>
332
+ # assume "objects" table has the following schema:
333
+ # create table objects (
334
+ # name varchar2(20),
335
+ # thing object
336
+ # )
337
+
338
+ db.type_translation = true
339
+ db.translator.add_translator( "object" ) do |type, value|
340
+ db.decode( value )
341
+ end
342
+
343
+ h = { :one=&gt;:two, "three"=&gt;"four", 5=&gt;6 }
344
+ dump = db.encode( h )
345
+
346
+ db.execute( "insert into objects values ( ?, ? )", "bob", dump )
347
+
348
+ obj = db.get_first_value( "select thing from objects where name='bob'" )
349
+ p obj == h
350
+ </pre></div>
351
+ <a name='9143400'></a>
352
+ <div class='faq-title'>How do insert binary data into the database?</div>
353
+ <div class='faq-answer'><p>Use blobs. Blobs are new features of SQLite3. You have to use bind variables to make it work:</p>
354
+
355
+
356
+ <pre>
357
+ db.execute( "insert into foo ( ?, ? )",
358
+ SQLite3::Blob.new( "\0\1\2\3\4\5" ),
359
+ SQLite3::Blob.new( "a\0b\0c\0d ) )
360
+ </pre>
361
+
362
+ <p>The blob values must be indicated explicitly by binding each parameter to a value of type SQLite3::Blob.</p></div>
363
+ <a name='9143330'></a>
364
+ <div class='faq-title'>How do I do a <span class="caps">DDL</span> (insert, update, delete) statement?</div>
365
+ <div class='faq-answer'><p>You can actually do inserts, updates, and deletes in exactly the same way as selects, but in general the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> method will be most convenient:</p>
366
+
367
+
368
+ <pre>
369
+ db.execute( "insert into table values ( ?, ? )", *bind_vars )
370
+ </pre></div>
371
+ <a name='9143260'></a>
372
+ <div class='faq-title'>How do I execute multiple statements in a single string?</div>
373
+ <div class='faq-answer'><p>The standard query methods (<a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a>, <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute2</a>, <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#query</a>, and <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#execute</a>) will only execute the first statement in the string that is given to them. Thus, if you have a string with multiple <span class="caps">SQL</span> statements, each separated by a string, you can&#8217;t use those methods to execute them all at once.</p>
374
+
375
+
376
+ <p>Instead, use <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute_batch</a>:</p>
377
+
378
+
379
+ <pre>
380
+ sql = &lt;&lt;SQL
381
+ create table the_table (
382
+ a varchar2(30),
383
+ b varchar2(30)
384
+ );
385
+
386
+ insert into the_table values ( 'one', 'two' );
387
+ insert into the_table values ( 'three', 'four' );
388
+ insert into the_table values ( 'five', 'six' );
389
+ SQL
390
+
391
+ db.execute_batch( sql )
392
+ </pre>
393
+
394
+ <p>Unlike the other query methods, <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute_batch</a> accepts no block. It will also only ever return <ins>nil</ins>. Thus, it is really only suitable for batch processing of <span class="caps">DDL</span> statements.</p></div>
395
+ <a name='9143190'></a>
396
+ <div class='faq-title'>How do I begin/end a transaction?</div>
397
+ <div class='faq-answer'><p>Use <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#transaction</a> to start a transaction. If you give it a block, the block will be automatically committed at the end of the block, unless an exception was raised, in which case the transaction will be rolled back. (Never explicitly call <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#commit</a> or <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#rollback</a> inside of a transaction block&#8212;you&#8217;ll get errors when the block terminates!)</p>
398
+
399
+
400
+ <pre> database.transaction do |db| db.execute( "insert into table values ( 'a', 'b', 'c' )" ) ... end </pre>
401
+
402
+ <p>Alternatively, if you don&#8217;t give a block to <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#transaction</a>, the transaction remains open until you explicitly call <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#commit</a> or <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#rollback</a>.</p>
403
+
404
+
405
+ <pre> db.transaction db.execute( "insert into table values ( 'a', 'b', 'c' )" ) db.commit </pre>
406
+
407
+ <p>Note that SQLite does not allow nested transactions, so you&#8217;ll get errors if you try to open a new transaction while one is already active. Use <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#transaction_active</a>? to determine whether a transaction is active or not.</p></div>
408
+ </body></html>