sqlite-ruby 2.1.0-mswin32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,453 @@
1
+ ---
2
+ - "How do I do a database query?":
3
+ - "I just want an array of the rows...": >-
4
+
5
+ Use the Database#execute method. If you don't give it a block, it will
6
+ return an array of all the rows:
7
+
8
+
9
+ <pre>
10
+ require 'sqlite'
11
+
12
+ db = SQLite::Database.new( "test.db" )
13
+ rows = db.execute( "select * from test" )
14
+ </pre>
15
+
16
+ - "I'd like to use a block to iterate through the rows...": >-
17
+
18
+ Use the Database#execute method. If you give it a block, each row of the
19
+ result will be yielded to the block:
20
+
21
+
22
+ <pre>
23
+ require 'sqlite'
24
+
25
+ db = SQLite::Database.new( "test.db" )
26
+ db.execute( "select * from test" ) do |row|
27
+ ...
28
+ end
29
+ </pre>
30
+
31
+ - "I need to get the column names as well as the rows...": >-
32
+
33
+ Use the Database#execute2 method. This works just like Database#execute;
34
+ if you don't give it a block, it returns an array of rows; otherwise, it
35
+ will yield each row to the block. _However_, the first row returned is
36
+ always an array of the column names from the query:
37
+
38
+
39
+ <pre>
40
+ require 'sqlite'
41
+
42
+ db = SQLite::Database.new( "test.db" )
43
+ columns, *rows = db.execute( "select * from test" )
44
+
45
+ # or use a block:
46
+
47
+ columns = nil
48
+ db.execute( "select * from test" ) do |row|
49
+ if columns.nil?
50
+ columns = row
51
+ else
52
+ # process row
53
+ end
54
+ end
55
+ </pre>
56
+
57
+ - "I need the result set object itself...": >-
58
+
59
+ Sometimes you don't want all the rows at once, and yet you'd like to be
60
+ able to iterate through the results. For instance, you may want to pass
61
+ the results to some other function (or series of functions) and have them
62
+ pull rows from the results on demand. This is more effecient for very
63
+ large queries.
64
+
65
+
66
+ To do this, use Database#query. If called without a block, it returns the
67
+ ResultSet instance. If called _with_ a block, it yields the ResultSet to
68
+ the block (and then closes the set when the block terminates).
69
+
70
+
71
+ You can do both internal and external iteration with the result set this
72
+ way:
73
+
74
+
75
+ <pre>
76
+ require 'sqlite'
77
+
78
+ db = SQLite::Database.new( "test.db" )
79
+ result = db.query( "select * from test" )
80
+
81
+ if do_external_iteration
82
+
83
+ while ( row = result.next )
84
+ # process row
85
+ end
86
+
87
+ elsif do_internal_iteration
88
+
89
+ result.each do |row|
90
+ # process row
91
+ end
92
+
93
+ elsif get_result_metadata
94
+
95
+ column_names = result.columns
96
+ column_types = result.types
97
+
98
+ end
99
+
100
+ result.close
101
+ </pre>
102
+
103
+ _Note_: if you are using Database#query to execute statements with no
104
+ result sets (ie, inserts, deletes, udpates, etc.), you _must_ call
105
+ @result.next@ to actually perform the operation. Otherwise, the
106
+ operation will never be executed:
107
+
108
+
109
+ <pre>
110
+ db.query( "insert into table values ( 'a', 'b' )" ) do |result|
111
+ result.next
112
+ end
113
+ </pre>
114
+
115
+
116
+ In general, Database#query is not a very good choice for such
117
+ operations...
118
+
119
+
120
+ - "I just want the first row of the result set...": >-
121
+
122
+ Easy. Just call Database#get_first_row:
123
+
124
+
125
+ <pre>
126
+ row = db.get_first_row( "select * from table" )
127
+ </pre>
128
+
129
+
130
+ This also supports bind variables, just like Database#execute
131
+ and friends.
132
+
133
+ - "I just want the first value of the first row of the result set...": >-
134
+
135
+ Also easy. Just call Database#get_first_value:
136
+
137
+
138
+ <pre>
139
+ count = db.get_first_value( "select count(*) from table" )
140
+ </pre>
141
+
142
+
143
+ This also supports bind variables, just like Database#execute
144
+ and friends.
145
+
146
+ - "How do I prepare a statement for repeated execution?": >-
147
+ If the same statement is going to be executed repeatedly, you can speed
148
+ things up a bit by _preparing_ the statement. You do this via the
149
+ Database#prepare method. It returns a Statement object, and you can
150
+ then invoke #execute on that to get the ResultSet:
151
+
152
+
153
+ <pre>
154
+ stmt = db.prepare( "select * from person" )
155
+
156
+ 1000.times do
157
+ stmt.execute do |result|
158
+ ...
159
+ end
160
+ end
161
+ </pre>
162
+
163
+
164
+ This is made more useful by the ability to bind variables to placeholders
165
+ via the Statement#bind_param and Statement#bind_params methods. (See the
166
+ next FAQ for details.)
167
+
168
+ - "How do I use placeholders in an SQL statement?": >-
169
+ Placeholders in an SQL statement take any of the following formats:
170
+
171
+
172
+ * @?@
173
+
174
+ * @?_n_@
175
+
176
+ * @:_word_@
177
+
178
+ * @:_word_:@
179
+
180
+
181
+ Where _n_ is an integer, and _word_ is an alpha-numeric identifier (or
182
+ number). When the placeholder is associated with a number, that number
183
+ identifies the index of the bind variable to replace it with. When it
184
+ is an identifier, it identifies the name of the correponding bind
185
+ variable. (In the instance of the first format--a single question
186
+ mark--the placeholder is assigned a number one greater than the last
187
+ index used, or 1 if it is the first.)
188
+
189
+
190
+ For example, here is a query using these placeholder formats:
191
+
192
+
193
+ <pre>
194
+ select *
195
+ from table
196
+ where ( c = ?2 or c = ? )
197
+ and d = :name
198
+ and e = :spouse:
199
+ and f = :1
200
+ </pre>
201
+
202
+
203
+ This defines 5 different placeholders: 1, 2, 3, "name", and "spouse".
204
+
205
+
206
+ You replace these placeholders by _binding_ them to values. This can be
207
+ accomplished in a variety of ways.
208
+
209
+
210
+ The Database#execute, Database#execute2, and Database#query methods all
211
+ accept additional arguments following the SQL statement. These arguments
212
+ are assumed to be bind parameters, and they are bound (positionally) to
213
+ their corresponding placeholders:
214
+
215
+
216
+ <pre>
217
+ db.execute( "select * from table where a = ? and b = ?",
218
+ "hello",
219
+ "world" )
220
+ </pre>
221
+
222
+
223
+ The above would replace the first question mark with 'hello' and the
224
+ second with 'world'. If the placeholders have an explicit index given, they
225
+ will be replaced with the bind parameter at that index (1-based).
226
+
227
+
228
+ If a Hash is given as a bind parameter, then its key/value pairs are bound
229
+ to the placeholders. This is how you bind by name:
230
+
231
+
232
+ <pre>
233
+ db.execute( "select * from table where a = :name and b = :value",
234
+ "name" => "bob",
235
+ "value" => "priceless" )
236
+ </pre>
237
+
238
+
239
+ You can also bind explicitly using the Statement object itself. Just do a
240
+ Database#prepare to get the Statement, and then use either Statement#bind_param
241
+ or Statement#bind_params:
242
+
243
+
244
+ <pre>
245
+ stmt = db.prepare( "select * from table where a = :name and b = ?" )
246
+
247
+ stmt.bind_param( "name", "bob" )
248
+ stmt.bind_param( 1, "value" )
249
+
250
+ # or
251
+
252
+ stmt.bind_params( "value", "name" => "bob" )
253
+ </pre>
254
+
255
+ - "How do I discover metadata about a query?": >-
256
+
257
+ If you ever want to know the names or types of the columns in a result
258
+ set, you can do it in several ways.
259
+
260
+
261
+ The first way is to ask the row object itself. Each row will have a
262
+ property "fields" that returns an array of the column names. The row
263
+ will also have a property "types" that returns an array of the column
264
+ types:
265
+
266
+
267
+ <pre>
268
+ rows = db.execute( "select * from table" )
269
+ p rows[0].fields
270
+ p rows[0].types
271
+ </pre>
272
+
273
+
274
+ Obviously, this approach requires you to execute a statement that actually
275
+ returns data. If you don't know if the statement will return any rows, but
276
+ you still need the metadata, you can use Database#query and ask the ResultSet
277
+ object itself:
278
+
279
+
280
+ <pre>
281
+ db.query( "select * from table" ) do |result|
282
+ p result.columns
283
+ p result.types
284
+ ...
285
+ end
286
+ </pre>
287
+
288
+
289
+ Lastly, you can use Database#prepare and ask the Statement object what
290
+ the metadata are:
291
+
292
+
293
+ <pre>
294
+ stmt = db.prepare( "select * from table" )
295
+ p stmt.columns
296
+ p stmt.types
297
+ </pre>
298
+
299
+ - "I'd like the rows to be indexible by column name.": >-
300
+ By default, each row from a query is returned as an Array of values. This
301
+ means that you can only obtain values by their index. Sometimes, however,
302
+ you would like to obtain values by their column name.
303
+
304
+
305
+ The first way to do this is to set the Database property "results_as_hash"
306
+ to true. If you do this, then all rows will be returned as Hash objects,
307
+ with the column names as the keys. (In this case, the "fields" property
308
+ is unavailable on the row, although the "types" property remains.)
309
+
310
+
311
+ <pre>
312
+ db.results_as_hash = true
313
+ db.execute( "select * from table" ) do |row|
314
+ p row['column1']
315
+ p row['column2']
316
+ end
317
+ </pre>
318
+
319
+
320
+ The other way is to use Ara Howard's "ArrayFields":http://rubyforge.org/projects/arrayfields
321
+ module. Just require "arrayfields", and all of your rows will be indexable by column name,
322
+ even though they are still arrays!
323
+
324
+
325
+ <pre>
326
+ require 'arrayfields'
327
+
328
+ ...
329
+ db.execute( "select * from table" ) do |row|
330
+ p row[0] == row['column1']
331
+ p row[1] == row['column2']
332
+ end
333
+ </pre>
334
+
335
+ - "I'd like the values from a query to be the correct types, instead of String.": >-
336
+ You can turn on "type translation" by setting Database#type_translation to true:
337
+
338
+
339
+ <pre>
340
+ db.type_translation = true
341
+ db.execute( "select * from table" ) do |row|
342
+ p row
343
+ end
344
+ </pre>
345
+
346
+
347
+ By doing this, each return value for each row will be translated to its
348
+ correct type, based on its declared column type.
349
+
350
+
351
+ You can even declare your own translation routines, if (for example) you are
352
+ using an SQL type that is not handled by default:
353
+
354
+
355
+ <pre>
356
+ # assume "objects" table has the following schema:
357
+ # create table objects (
358
+ # name varchar2(20),
359
+ # thing object
360
+ # )
361
+
362
+ db.type_translation = true
363
+ db.translator.add_translator( "object" ) do |type, value|
364
+ db.decode( value )
365
+ end
366
+
367
+ h = { :one=>:two, "three"=>"four", 5=>6 }
368
+ dump = db.encode( h )
369
+
370
+ db.execute( "insert into objects values ( ?, ? )", "bob", dump )
371
+
372
+ obj = db.get_first_value( "select thing from objects where name='bob'" )
373
+ p obj == h
374
+ </pre>
375
+
376
+ - "How do I do a DDL (insert, update, delete) statement?": >-
377
+ You can actually do inserts, updates, and deletes in exactly the same way
378
+ as selects, but in general the Database#execute method will be most
379
+ convenient:
380
+
381
+
382
+ <pre>
383
+ db.execute( "insert into table values ( ?, ? )", *bind_vars )
384
+ </pre>
385
+
386
+ - "How do I execute multiple statements in a single string?": >-
387
+ The standard query methods (Database#execute, Database#execute2,
388
+ Database#query, and Statement#execute) will only execute the first
389
+ statement in the string that is given to them. Thus, if you have a
390
+ string with multiple SQL statements, each separated by a string,
391
+ you can't use those methods to execute them all at once.
392
+
393
+
394
+ Instead, use Database#execute_batch:
395
+
396
+
397
+ <pre>
398
+ sql = <<SQL
399
+ create table the_table (
400
+ a varchar2(30),
401
+ b varchar2(30)
402
+ );
403
+
404
+ insert into the_table values ( 'one', 'two' );
405
+ insert into the_table values ( 'three', 'four' );
406
+ insert into the_table values ( 'five', 'six' );
407
+ SQL
408
+
409
+ db.execute_batch( sql )
410
+ </pre>
411
+
412
+
413
+ Unlike the other query methods, Database#execute_batch accepts no
414
+ block. It will also only ever return +nil+. Thus, it is really only
415
+ suitable for batch processing of DDL statements.
416
+
417
+ - "How do I begin/end a transaction?":
418
+ Use Database#transaction to start a transaction. If you give it a block,
419
+ the block will be automatically committed at the end of the block,
420
+ unless an exception was raised, in which case the transaction will be
421
+ rolled back. (Never explicitly call Database#commit or Database#rollback
422
+ inside of a transaction block--you'll get errors when the block
423
+ terminates!)
424
+
425
+
426
+ <pre>
427
+ database.transaction do |db|
428
+ db.execute( "insert into table values ( 'a', 'b', 'c' )" )
429
+ ...
430
+ end
431
+ </pre>
432
+
433
+
434
+ Alternatively, if you don't give a block to Database#transaction, the
435
+ transaction remains open until you explicitly call Database#commit or
436
+ Database#rollback.
437
+
438
+
439
+ <pre>
440
+ db.transaction
441
+ db.execute( "insert into table values ( 'a', 'b', 'c' )" )
442
+ db.commit
443
+ </pre>
444
+
445
+
446
+ Note that SQLite does not allow nested transactions, so you'll get errors
447
+ if you try to open a new transaction while one is already active. Use
448
+ Database#transaction_active? to determine whether a transaction is
449
+ active or not.
450
+
451
+ #- "How do I discover metadata about a table/index?":
452
+ #
453
+ #- "How do I do tweak database settings?":