comparison docs/source/capabilities.rst @ 294:dd7b3e4b58dd

new py docs
author catherine@dellzilla
date Tue, 24 Mar 2009 14:40:53 -0400
parents abb4c6524113
children 3efffbf7481f
comparison
equal deleted inserted replaced
293:569cba4da162 294:dd7b3e4b58dd
330 Python 330 Python
331 ====== 331 ======
332 332
333 The `py` command allows the user to execute Python commands, either one-at-a-time (with 333 The `py` command allows the user to execute Python commands, either one-at-a-time (with
334 `py {command}`) or in an interactive environment (beginning with a bare `py` statement, 334 `py {command}`) or in an interactive environment (beginning with a bare `py` statement,
335 and continuing until `end py` is entered). 335 and continuing until Ctrl-D, `quit()`, or `exit()` is entered).
336 336
337 A history of result sets from each query is exposed to the python session as the list `r`; 337 A history of result sets from each query is exposed to the python session as the list `r`;
338 the most recent result set is `r[-1]`. Bind variables are exposed as the dictionary `binds`. 338 the most recent result set is `r[-1]`. Each row can be references as a tuple, or as an
339 object with an attribute for each column.
340
341 Bind variables are exposed as the dictionary `binds`. Each row from each result set has
342 a .bind() method that fills a bind varible for each column with that row's value.
343
344 Resultsets in `r` are read-only, but `binds` can be written as well as read, and will
345 be working bind variables in the SQL environment.
346
347 SQL and sqlpython commands can be issued from the Python environment with `sql("{your SQL}")`.
348
339 All variables are retained each time the python environment is entered (whether interactively, 349 All variables are retained each time the python environment is entered (whether interactively,
340 or with one-line `py` statements). 350 or with one-line `py` statements).
341
342 Resultsets in `r` are read-only, but `binds` can be written as well as read, and will
343 be working bind variables in the SQL environment.
344
345 :: 351 ::
346 352
347 0:testschema@eqtest> select title, author from play; 353 0:testschema@orcl> select title, author from play;
348 354
349 TITLE AUTHOR 355 TITLE AUTHOR
350 --------------- ----------- 356 --------------- -----------
351 Timon of Athens Shakespeare 357 Timon of Athens Shakespeare
352 Twelfth Night Shakespeare 358 Twelfth Night Shakespeare
353 The Tempest Shakespeare 359 The Tempest Shakespeare
354 Agamemnon Aeschylus 360 Agamemnon Aeschylus
355 361
356 4 rows selected. 362 4 rows selected.
357 363
358 0:testschema@eqtest> py import urllib 364 0:testschema@orcl> py import urllib
359 0:testschema@eqtest> py current_season = urllib.urlopen('http://cincyshakes.com/').read() 365 0:testschema@orcl> py current_season = urllib.urlopen('http://cincyshakes.com/').read()
360 0:testschema@eqtest> py 366 0:testschema@orcl> py
361 Now accepting python commands; end with `end py` 367 Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
368 [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
369 Type "help", "copyright", "credits" or "license" for more information.
370 (mysqlpy)
371
372 py <command>: Executes a Python command.
373 py: Enters interactive Python mode; end with `Ctrl-D`, `quit()`, or 'exit`.
374 Past SELECT results are exposed as list `r`;
375 most recent resultset is `r[-1]`.
376 SQL bind, substitution variables are exposed as `binds`, `substs`.
377 SQL and sqlpython commands can be issued with sql("your non-python command here").
378
362 >>> r[-1] 379 >>> r[-1]
363 [('Timon of Athens', 'Shakespeare'), ('Twelfth Night', 'Shakespeare'), ('The Tempest', 'Shakespeare'), ('Agamemnon', 'Aeschylus')] 380 [('Timon of Athens', 'Shakespeare'), ('Twelfth Night', 'Shakespeare'), ('The Tempest', 'Shakespeare'), ('Agamemnon', 'Aeschylus')]
381 >>> r[-1][0][0]
382 'Timon of Athens'
364 >>> for row in r[-1]: 383 >>> for row in r[-1]:
365 ... print '%s by %s' % (row.title, row.author) 384 ... print "%s, by %s" % (row.title, row.author)
366 Timon of Athens by Shakespeare 385 ...
367 Twelfth Night by Shakespeare 386 Timon of Athens, by Shakespeare
368 The Tempest by Shakespeare 387 Twelfth Night, by Shakespeare
369 Agamemnon by Aeschylus 388 The Tempest, by Shakespeare
389 Agamemnon, by Aeschylus
370 >>> [row.title for row in r[-1] if row.title in current_season] 390 >>> [row.title for row in r[-1] if row.title in current_season]
371 ['Timon of Athens', 'Twelfth Night'] 391 ['Timon of Athens', 'Twelfth Night']
372 >>> binds['nowplaying'] = [row.title for row in r[-1] if row.title in current_season][0] 392 >>> binds['author'] = 'Shakespeare'
373 >>> end py 393 >>> query = "SELECT title FROM play WHERE author = :author"
374 0:testschema@eqtest> print 394 >>> sql(query)
375 :nowplaying = Timon of Athens 395
376 0:testschema@eqtest> select title, author from play where title = :nowplaying; 396 TITLE
397 ---------------
398 Timon of Athens
399 Twelfth Night
400 The Tempest
401
402 3 rows selected.
403
404 >>> r[-1]
405 [('Timon of Athens',), ('Twelfth Night',), ('The Tempest',)]
406 >>> r[-1][0]
407 ('Timon of Athens',)
408 >>> r[-1][0].bind()
409 >>> binds['title']
410 'Timon of Athens'
411 >>> quit()
412 0:testschema@orcl> select title, author from play where title = :title;
377 413
378 TITLE AUTHOR 414 TITLE AUTHOR
379 --------------- ----------- 415 --------------- -----------
380 Timon of Athens Shakespeare 416 Timon of Athens Shakespeare
381 417
382 1 row selected. 418 1 row selected.
383 419
384 Parameters 420 Parameters
385 ========== 421 ==========
386 422
387 Several parameters control the behavior of sqlpython itself. 423 Several parameters control the behavior of sqlpython itself.
388 424
393 commit_on_exit Automatically commits work at end of session True 429 commit_on_exit Automatically commits work at end of session True
394 continuation_prompt Prompt for second line and onward of long statement > 430 continuation_prompt Prompt for second line and onward of long statement >
395 default_file_name The file opened by `edit`, if not specified afiedt.buf 431 default_file_name The file opened by `edit`, if not specified afiedt.buf
396 echo Echo command entered before executing False 432 echo Echo command entered before executing False
397 editor Text editor invoked by `edit`. varies 433 editor Text editor invoked by `edit`. varies
398 heading Print column names True 434 heading Print column names along with results True
399 maxfetch Maximum number of rows to return from any query 1000 435 maxfetch Maximum number of rows to return from any query 1000
400 maxtselctrows Maximum # of rows from a tselect or \\n query 10 436 maxtselctrows Maximum # of rows from a tselect or \\n query 10
401 prompt Probably unwise to change user@instance> 437 prompt Probably unwise to change user@instance>
402 scan Interpret & as indicating substitution variables True 438 scan Interpret & as indicating substitution variables True
403 serveroutput Print DBMS_OUTPUT.PUT_LINE results True 439 serveroutput Print DBMS_OUTPUT.PUT_LINE results True