Skip to content

Commit e99b11b

Browse files
committed
Update README for multiple result set errors.
1 parent 76d13e4 commit e99b11b

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

README.md

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ files are. For example, if you unzipped the connector to c:\mysql-connector-c-6.
6666
the gem like this:
6767

6868
gem install mysql2 -- --with-mysql-dir=c:\mysql-connector-c-6.1.1-win32
69-
69+
7070
Finally, you must copy libmysql.dll from the lib subdirectory of your MySQL or MySQL connector directory into
7171
your ruby\bin directory. In the above example, libmysql.dll would be located at
7272
c:\mysql-connector-c-6.1.1-win32\lib .
@@ -182,20 +182,45 @@ Mysql2::Client.new(
182182

183183
### Multiple result sets
184184

185-
You can also retrieve multiple result sets. For this to work you need to connect with
186-
flags `Mysql2::Client::MULTI_STATEMENTS`. Using multiple result sets is normally used
187-
when calling stored procedures that return more than one result set
185+
You can also retrieve multiple result sets. For this to work you need to
186+
connect with flags `Mysql2::Client::MULTI_STATEMENTS`. Multiple result sets can
187+
be used with stored procedures that return more than one result set, and for
188+
bundling several SQL statements into a single call to `client.query`.
188189

189190
``` ruby
190-
client = Mysql2::Client.new(:host => "localhost", :username => "root", :flags => Mysql2::Client::MULTI_STATEMENTS )
191-
result = client.query( 'CALL sp_customer_list( 25, 10 )')
191+
client = Mysql2::Client.new(:host => "localhost", :username => "root", :flags => Mysql2::Client::MULTI_STATEMENTS)
192+
result = client.query('CALL sp_customer_list( 25, 10 )')
192193
# result now contains the first result set
193-
while ( client.next_result)
194-
result = client.store_result
195-
# result now contains the next result set
194+
while client.next_result
195+
result = client.store_result
196+
# result now contains the next result set
197+
end
198+
```
199+
200+
Repeated calls to `client.next_result` will return true, false, or raise an
201+
exception if the respective query erred. When `client.next_result` returns true,
202+
call `client.store_result` to retieve a result object. Exceptions are not
203+
raised until `client.next_result` is called to find the status of the respective
204+
query. Subsequent queries are not executed if an earlier query raised an
205+
exception.
206+
207+
``` ruby
208+
result = client.query('SELECT 1; SELECT 2; SELECT A; SELECT 3')
209+
p result.first
210+
211+
while client.next_result
212+
result = client.store_result
213+
p result.first
196214
end
197215
```
198216

217+
Yields:
218+
```
219+
{"1"=>1}
220+
{"2"=>2}
221+
next_result: Unknown column 'A' in 'field list' (Mysql2::Error)
222+
```
223+
199224
See https://gist.github.com/1367987 for using MULTI_STATEMENTS with Active Record.
200225

201226
### Secure auth

0 commit comments

Comments
 (0)