sqlite3-ruby 1.2.3-x86-mingw32 → 1.2.5-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog.cvs +88 -0
- data/History.txt +68 -0
- data/LICENSE +27 -0
- data/Manifest.txt +41 -0
- data/{README.rdoc → README.txt} +6 -1
- data/Rakefile +5 -0
- data/ext/sqlite3_api/extconf.rb +3 -9
- data/ext/sqlite3_api/sqlite3_api.i +4 -0
- data/ext/sqlite3_api/sqlite3_api_wrap.c +4977 -3053
- data/{doc/faq → faq}/faq.rb +0 -0
- data/{doc/faq → faq}/faq.yml +0 -0
- data/lib/1.8/sqlite3_api.so +0 -0
- data/lib/1.9/sqlite3_api.so +0 -0
- data/lib/sqlite3/database.rb +13 -7
- data/lib/sqlite3/driver/dl/api.rb +1 -1
- data/lib/sqlite3/driver/native/driver.rb +9 -1
- data/lib/sqlite3/resultset.rb +5 -1
- data/lib/sqlite3/statement.rb +3 -2
- data/lib/sqlite3/version.rb +4 -2
- data/setup.rb +1333 -0
- data/tasks/benchmark.rake +9 -0
- data/tasks/faq.rake +9 -0
- data/tasks/gem.rake +32 -0
- data/tasks/native.rake +35 -0
- data/tasks/vendor_sqlite3.rake +104 -0
- data/test/{mocks.rb → helper.rb} +23 -1
- data/test/{tc_database.rb → test_database.rb} +25 -6
- data/test/{tc_errors.rb → test_errors.rb} +1 -5
- data/test/test_integration.rb +542 -0
- data/test/test_integration_open_close.rb +30 -0
- data/test/test_integration_pending.rb +111 -0
- data/test/test_integration_resultset.rb +159 -0
- data/test/test_integration_statement.rb +195 -0
- metadata +90 -43
- data/doc/faq/faq.html +0 -408
- data/ext/sqlite3_api/MANIFEST +0 -4
- data/ext/sqlite3_api/Makefile +0 -142
- data/ext/sqlite3_api/mkmf.log +0 -66
- data/ext/sqlite3_api/sqlite3_api.so +0 -0
- data/ext/sqlite3_api/sqlite3_api_wrap.o +0 -0
- data/ext/sqlite3_api/win32/build.bat +0 -7
- data/test/tc_integration.rb +0 -1044
- data/test/tests.rb +0 -6
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sqlite3-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.5
|
5
5
|
platform: x86-mingw32
|
6
6
|
authors:
|
7
7
|
- Jamis Buck
|
@@ -9,42 +9,72 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-07-25 00:00:00 -03:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mocha
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake-compiler
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.5.0
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: hoe
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.3.2
|
44
|
+
version:
|
45
|
+
description: |-
|
46
|
+
This module allows Ruby programs to interface with the SQLite3
|
47
|
+
database engine (http://www.sqlite.org). You must have the
|
48
|
+
SQLite engine installed in order to build this module.
|
49
|
+
|
50
|
+
Note that this module is NOT compatible with SQLite 2.x.
|
51
|
+
email:
|
52
|
+
- jamis@37signals.com
|
18
53
|
executables: []
|
19
54
|
|
20
|
-
extensions:
|
21
|
-
|
55
|
+
extensions: []
|
56
|
+
|
22
57
|
extra_rdoc_files:
|
23
|
-
-
|
58
|
+
- History.txt
|
59
|
+
- Manifest.txt
|
60
|
+
- README.txt
|
24
61
|
files:
|
25
|
-
-
|
26
|
-
-
|
27
|
-
-
|
28
|
-
-
|
29
|
-
-
|
62
|
+
- ChangeLog.cvs
|
63
|
+
- History.txt
|
64
|
+
- LICENSE
|
65
|
+
- Manifest.txt
|
66
|
+
- README.txt
|
67
|
+
- Rakefile
|
30
68
|
- ext/sqlite3_api/extconf.rb
|
31
|
-
- ext/sqlite3_api/Makefile
|
32
|
-
- ext/sqlite3_api/MANIFEST
|
33
|
-
- ext/sqlite3_api/mkmf.log
|
34
69
|
- ext/sqlite3_api/sqlite3_api.i
|
35
|
-
- ext/sqlite3_api/sqlite3_api.so
|
36
70
|
- ext/sqlite3_api/sqlite3_api_wrap.c
|
37
|
-
-
|
38
|
-
-
|
39
|
-
-
|
40
|
-
- lib/sqlite3
|
71
|
+
- faq/faq.rb
|
72
|
+
- faq/faq.yml
|
73
|
+
- lib/sqlite3.rb
|
41
74
|
- lib/sqlite3/constants.rb
|
42
75
|
- lib/sqlite3/database.rb
|
43
|
-
- lib/sqlite3/driver
|
44
|
-
- lib/sqlite3/driver/dl
|
45
76
|
- lib/sqlite3/driver/dl/api.rb
|
46
77
|
- lib/sqlite3/driver/dl/driver.rb
|
47
|
-
- lib/sqlite3/driver/native
|
48
78
|
- lib/sqlite3/driver/native/driver.rb
|
49
79
|
- lib/sqlite3/errors.rb
|
50
80
|
- lib/sqlite3/pragmas.rb
|
@@ -53,31 +83,42 @@ files:
|
|
53
83
|
- lib/sqlite3/translator.rb
|
54
84
|
- lib/sqlite3/value.rb
|
55
85
|
- lib/sqlite3/version.rb
|
56
|
-
-
|
86
|
+
- setup.rb
|
87
|
+
- tasks/benchmark.rake
|
88
|
+
- tasks/faq.rake
|
89
|
+
- tasks/gem.rake
|
90
|
+
- tasks/native.rake
|
91
|
+
- tasks/vendor_sqlite3.rake
|
57
92
|
- test/bm.rb
|
58
|
-
- test/driver
|
59
|
-
- test/driver/dl
|
60
93
|
- test/driver/dl/tc_driver.rb
|
61
|
-
- test/
|
94
|
+
- test/helper.rb
|
62
95
|
- test/native-vs-dl.rb
|
63
|
-
- test/
|
64
|
-
- test/
|
65
|
-
- test/
|
66
|
-
- test/
|
67
|
-
-
|
96
|
+
- test/test_database.rb
|
97
|
+
- test/test_errors.rb
|
98
|
+
- test/test_integration.rb
|
99
|
+
- test/test_integration_open_close.rb
|
100
|
+
- test/test_integration_pending.rb
|
101
|
+
- test/test_integration_resultset.rb
|
102
|
+
- test/test_integration_statement.rb
|
103
|
+
- lib/1.8/sqlite3_api.so
|
104
|
+
- lib/1.9/sqlite3_api.so
|
68
105
|
has_rdoc: true
|
69
|
-
homepage: http://
|
106
|
+
homepage: http://sqlite3-ruby.rubyforge.org
|
107
|
+
licenses: []
|
108
|
+
|
70
109
|
post_install_message:
|
71
110
|
rdoc_options:
|
72
111
|
- --main
|
73
|
-
- README.
|
112
|
+
- README.txt
|
113
|
+
- --main=README.txt
|
74
114
|
require_paths:
|
75
115
|
- lib
|
116
|
+
- ext
|
76
117
|
required_ruby_version: !ruby/object:Gem::Requirement
|
77
118
|
requirements:
|
78
|
-
- - "
|
119
|
+
- - ">"
|
79
120
|
- !ruby/object:Gem::Version
|
80
|
-
version: 1.8.
|
121
|
+
version: 1.8.5
|
81
122
|
version:
|
82
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
124
|
requirements:
|
@@ -87,10 +128,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
128
|
version:
|
88
129
|
requirements: []
|
89
130
|
|
90
|
-
rubyforge_project:
|
91
|
-
rubygems_version: 1.
|
131
|
+
rubyforge_project: sqlite-ruby
|
132
|
+
rubygems_version: 1.3.4
|
92
133
|
signing_key:
|
93
|
-
specification_version:
|
94
|
-
summary:
|
134
|
+
specification_version: 3
|
135
|
+
summary: This module allows Ruby programs to interface with the SQLite3 database engine (http://www.sqlite.org)
|
95
136
|
test_files:
|
96
|
-
- test/
|
137
|
+
- test/test_database.rb
|
138
|
+
- test/test_errors.rb
|
139
|
+
- test/test_integration.rb
|
140
|
+
- test/test_integration_open_close.rb
|
141
|
+
- test/test_integration_pending.rb
|
142
|
+
- test/test_integration_resultset.rb
|
143
|
+
- test/test_integration_statement.rb
|
data/doc/faq/faq.html
DELETED
@@ -1,408 +0,0 @@
|
|
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…</a></li>
|
66
|
-
<li><a href='#9144080'>I’d like to use a block to iterate through the rows…</a></li>
|
67
|
-
<li><a href='#9144010'>I need to get the column names as well as the rows…</a></li>
|
68
|
-
<li><a href='#9143940'>I just want the first row of the result set…</a></li>
|
69
|
-
<li><a href='#9143870'>I just want the first value of the first row of the result set…</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’d like the rows to be indexible by column name.</a></li>
|
76
|
-
<li><a href='#9143470'>I’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…</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’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’d like to use a block to iterate through the rows…</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…</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’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…</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…</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 “name”.</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 ‘hello’ and the second with ‘world’. 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" => "bob",
|
227
|
-
"value" => "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" => "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" => "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 “fields” that returns an array of the column names. The row will also have a property “types” 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’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’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 “results_as_hash” 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 “fields” property is unavailable on the row, although the “types” 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’s <a href="http://rubyforge.org/projects/arrayfields">ArrayFields</a> module. Just require “arrayfields”, 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’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 “type translation” 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=>:two, "three"=>"four", 5=>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’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 = <<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—you’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’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’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>
|