-
Notifications
You must be signed in to change notification settings - Fork 561
/
docs.html
1166 lines (981 loc) · 44.5 KB
/
docs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>pyodbc</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<div id="titlebox">
pyodbc - A Python DB API module for ODBC
</div>
<div id="nav">
<a href="index.html">Home</a>
: <a href="docs.html">Documentation</a>
: <a href="http://sourceforge.net/project/showfiles.php?group_id=162557">Download</a>
: <a href="http://sourceforge.net/news/?group_id=162557">News</a>
: <a href="http://github.com/mkleehammer/pyodbc">Source Code (github)</a>
: <a href="http://sourceforge.net/tracker/?group_id=162557&atid=824254">Bug Tracker</a>
: <a href="http://sourceforge.net/projects/pyodbc">SourceForge</a>
: <a href="http://www.python.org/peps/pep-0249.html">Python DB API</a>
: <a href="license.html">MIT License</a>
</div>
<div id="contents">
<ul id="toc">
<li><a href="#examples">Quick Examples</a></li>
<li><a href="#pyodbc">Module Interface</a></li>
<li><a href="#connection">Connection Objects</a></li>
<li><a href="#cursor">Cursor Objects</a></li>
<li><a href="#row">Row Objects</a></li>
<li><a href="#datatypes">Data Types</a></li>
<li><a href="#datatypes">Errors</a></li>
<li><a href="#catalog">Catalog Functions</a></li>
</ul>
<p>This module implements the <a href="http://www.python.org/peps/pep-0249.html">Python Database API Specification</a>,
so you should first be familiar with it.</p>
<h1 id="examples">Quick Examples</h1>
<p>Make a direct connection to a database and create a cursor:</p>
<pre>
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=testdb;UID=me;PWD=pass')
cursor = cnxn.cursor()</pre>
<p>Select some values and print them:</p>
<pre>
cursor.execute("select user_id, user_name from users")
for row in cursor:
print row.user_id, row.user_name</pre>
<p>Select the values, but use a more compact form. The execute function returns the cursor
object when a SELECT is executed, so execute can be moved into the for loop:</p>
<pre>
for row in cursor.execute("select user_id, user_name from users"):
print row.user_id, row.user_name</pre>
<p>Select a calculated value, giving it a name:</p>
<pre>
cursor.execute("select count(*) as user_count from users")
row = cursor.fetchone()
print '%d users' % row.user_count</pre>
<p>Supply parameters:</p>
<pre>
cursor.execute("select count(*) as user_count from users where age > ?", 21)
row = cursor.fetchone()
print '%d users' % row.user_count</pre>
<p>Delete some records and retrieve the count:</p>
<pre>
count = cursor.execute("delete from users where age < ?", 18)
print "deleted %s users" % count</pre>
<h1 id="pyodbc">Module Interface</h1>
<h2 id="connect">connect(connectionstring, autocommit=False)</h2>
<dl>
<dt>connectionstring</dt>
<dd>The ODBC connection string.</dd>
<dt>autocommit</td>
<dd>A Boolean that determines if the connection should be in autocommit mode or manual-commit
mode.</dd>
</dl>
<p>Returns a new <a href="#connection">Connection</a> object.</p>
<p>The connection string is passed unmodified to <a
href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcsqldriverconnect.asp">SQLDriverConnect</a>.
Connection strings can have driver specific components and you should refer to the
SQLDriverConnect or other ODBC documentation for details, but below are two common examples.</p>
<p>To connect using a DSN (a data source specified in the Data Access control panel applet),
use a string similar to the following.</p>
<pre>
cnxn = pyodbc.connect("DSN=<i>dsnname</i>")
cnxn = pyodbc.connect("DSN=<i>dsnname</i>;PWD=<i>password</i>")
cnxn = pyodbc.connect("DSN=<i>dsnname</i>;UID=<i>user</i>;PWD=<i>password</i>")</pre>
<p>To connect to SQL Server directly (without a DSN), you must specify the server and database
to connect to using SQL Server-specific keywords. Note that the braces are required around the
driver name.</p>
<pre>
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=<i>server</i>;DATABASE=<i>database</i>;UID=<i>user</i>;PWD=<i>password</i>)</pre>
<h2>Module Description Variables</h2>
<dl>
<dt>version</dt>
<dd>The pyodbc version as a string in the format <i>major</i>.<i>minor</i>.<i>revision</i>.</dd>
<dt>apilevel</dt>
<dd>The string constant '2.0' indicating this module supports DB API level 2.0.</dd>
<dt>lowercase</dt>
<dd>A Boolean that controls whether column names in result rows are lowercased. This can be
changed any time and affects queries executed after the change. The default is False. This
can be useful when database columns have inconsistent capitalization.</dd>
<dt>pooling</dt>
<dd>A Boolean indicating whether connection pooling is enabled. This is a global (HENV)
setting, so it can only be modified before the first connection is made. The default is
True, which enables ODBC connection pooling.</dd>
<dt>threadsafety</dt>
<dd>The integer 1, indicating that threads may share the module but not connections. Note
that connections and cursors may be used by different threads, just not at the same time.</dd>
<dt>qmark</dt>
<dd>The string constant "qmark" to indicate parameters are identified using question marks.</dd>
</dl>
<h2>DB API Type Functions</h2>
<p>The DB API defines a set of functions that convert from well-known types to data types
required by the database module. If your code does not need to be portable between database
modules (if you will only use pyodbc), you do not need to use these.</p>
<dl>
<dt>Date(year,month,day), DateFromTicks(ticks)</dt>
<dd>Both of these return
a <a href="http://docs.python.org/lib/datetime-date.html">datetime.date</a> instance.</dd>
<dt>Time(hour,minute,second), TimeFromTicks(ticks)</dt>
<dd>Both of these return
a <a href="http://docs.python.org/lib/datetime-time.html">datetime.time</a> instance.</dd>
<dt>Timestamp(year,month,day,hour,minute,second), TimestampFromTicks(ticks)</dt>
<dd>Both of these return
a <a href="http://docs.python.org/lib/datetime-datetime.html">datetime.datetime</a>
instance.</dd>
<dt>DATETIME</dt>
<dd>Set to the <a href="http://docs.python.org/lib/datetime-datetime.html">datetime.datetime</a> type. This is not
entirely accurate since dates and times actually use two different classes, <code>datetime.date</code> and
<code>datetime.time</code>, but there is no way to specify this.</dd>
<dt>STRING</dt>
<dd>Set to the <code>string</code> type.</dd>
<dt>NUMBER</dt>
<dd>Set to the <code>float</code> type. This is not entirely accurate since the module uses different types of numbers
for different ODBC data types. Instead of using this, simply pass int, float, double, or decimal objects.</dd>
<dt>ROWID</dt>
<dd>Set to the <code>int</code> type.</dd>
<dt>Binary(string)</dt>
<dd>Returns a <code>buffer</code> instance.</dd>
<dt>BINARY</dt>
<dd>Set to the <code>buffer</code> type.</dd>
</dl>
<h2>Module Constants</h2>
<p>The following ODBC constants are defined. They only used with ODBC specific functions such
as Cursor.tables.</p>
<ul>
<li>SQL_ACCESSIBLE_PROCEDURES</li>
<li>SQL_ACCESSIBLE_TABLES</li>
<li>SQL_ACTIVE_ENVIRONMENTS</li>
<li>SQL_AGGREGATE_FUNCTIONS</li>
<li>SQL_ALTER_DOMAIN</li>
<li>SQL_ALTER_TABLE</li>
<li>SQL_ASYNC_MODE</li>
<li>SQL_BATCH_ROW_COUNT</li>
<li>SQL_BATCH_SUPPORT</li>
<li>SQL_BIGINT</li>
<li>SQL_BINARY</li>
<li>SQL_BIT</li>
<li>SQL_BOOKMARK_PERSISTENCE</li>
<li>SQL_CATALOG_LOCATION</li>
<li>SQL_CATALOG_NAME</li>
<li>SQL_CATALOG_NAME_SEPARATOR</li>
<li>SQL_CATALOG_TERM</li>
<li>SQL_CATALOG_USAGE</li>
<li>SQL_CHAR</li>
<li>SQL_COLLATION_SEQ</li>
<li>SQL_COLUMN_ALIAS</li>
<li>SQL_CONCAT_NULL_BEHAVIOR</li>
<li>SQL_CONVERT_FUNCTIONS</li>
<li>SQL_CONVERT_VARCHAR</li>
<li>SQL_CORRELATION_NAME</li>
<li>SQL_CREATE_ASSERTION</li>
<li>SQL_CREATE_CHARACTER_SET</li>
<li>SQL_CREATE_COLLATION</li>
<li>SQL_CREATE_DOMAIN</li>
<li>SQL_CREATE_SCHEMA</li>
<li>SQL_CREATE_TABLE</li>
<li>SQL_CREATE_TRANSLATION</li>
<li>SQL_CREATE_VIEW</li>
<li>SQL_CURSOR_COMMIT_BEHAVIOR</li>
<li>SQL_CURSOR_ROLLBACK_BEHAVIOR</li>
<li>SQL_DATABASE_NAME</li>
<li>SQL_DATA_SOURCE_NAME</li>
<li>SQL_DATA_SOURCE_READ_ONLY</li>
<li>SQL_DATETIME_LITERALS</li>
<li>SQL_DBMS_NAME</li>
<li>SQL_DBMS_VER</li>
<li>SQL_DDL_INDEX</li>
<li>SQL_DECIMAL</li>
<li>SQL_DEFAULT_TXN_ISOLATION</li>
<li>SQL_DESCRIBE_PARAMETER</li>
<li>SQL_DM_VER</li>
<li>SQL_DOUBLE</li>
<li>SQL_DRIVER_NAME</li>
<li>SQL_DRIVER_ODBC_VER</li>
<li>SQL_DRIVER_VER</li>
<li>SQL_DROP_ASSERTION</li>
<li>SQL_DROP_CHARACTER_SET</li>
<li>SQL_DROP_COLLATION</li>
<li>SQL_DROP_DOMAIN</li>
<li>SQL_DROP_SCHEMA</li>
<li>SQL_DROP_TABLE</li>
<li>SQL_DROP_TRANSLATION</li>
<li>SQL_DROP_VIEW</li>
<li>SQL_DYNAMIC_CURSOR_ATTRIBUTES1</li>
<li>SQL_DYNAMIC_CURSOR_ATTRIBUTES2</li>
<li>SQL_EXPRESSIONS_IN_ORDERBY</li>
<li>SQL_FILE_USAGE</li>
<li>SQL_FLOAT</li>
<li>SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES1</li>
<li>SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES2</li>
<li>SQL_GETDATA_EXTENSIONS</li>
<li>SQL_GROUP_BY</li>
<li>SQL_GUID</li>
<li>SQL_IDENTIFIER_CASE</li>
<li>SQL_IDENTIFIER_QUOTE_CHAR</li>
<li>SQL_INDEX_KEYWORDS</li>
<li>SQL_INFO_SCHEMA_VIEWS</li>
<li>SQL_INSERT_STATEMENT</li>
<li>SQL_INTEGER</li>
<li>SQL_INTEGRITY</li>
<li>SQL_INTERVAL_DAY</li>
<li>SQL_INTERVAL_DAY_TO_HOUR</li>
<li>SQL_INTERVAL_DAY_TO_MINUTE</li>
<li>SQL_INTERVAL_DAY_TO_SECOND</li>
<li>SQL_INTERVAL_HOUR</li>
<li>SQL_INTERVAL_HOUR_TO_MINUTE</li>
<li>SQL_INTERVAL_HOUR_TO_SECOND</li>
<li>SQL_INTERVAL_MINUTE</li>
<li>SQL_INTERVAL_MINUTE_TO_SECOND</li>
<li>SQL_INTERVAL_MONTH</li>
<li>SQL_INTERVAL_SECOND</li>
<li>SQL_INTERVAL_YEAR</li>
<li>SQL_INTERVAL_YEAR_TO_MONTH</li>
<li>SQL_KEYSET_CURSOR_ATTRIBUTES1</li>
<li>SQL_KEYSET_CURSOR_ATTRIBUTES2</li>
<li>SQL_KEYWORDS</li>
<li>SQL_LIKE_ESCAPE_CLAUSE</li>
<li>SQL_LONGVARBINARY</li>
<li>SQL_LONGVARCHAR</li>
<li>SQL_MAX_ASYNC_CONCURRENT_STATEMENTS</li>
<li>SQL_MAX_BINARY_LITERAL_LEN</li>
<li>SQL_MAX_CATALOG_NAME_LEN</li>
<li>SQL_MAX_CHAR_LITERAL_LEN</li>
<li>SQL_MAX_COLUMNS_IN_GROUP_BY</li>
<li>SQL_MAX_COLUMNS_IN_INDEX</li>
<li>SQL_MAX_COLUMNS_IN_ORDER_BY</li>
<li>SQL_MAX_COLUMNS_IN_SELECT</li>
<li>SQL_MAX_COLUMNS_IN_TABLE</li>
<li>SQL_MAX_COLUMN_NAME_LEN</li>
<li>SQL_MAX_CONCURRENT_ACTIVITIES</li>
<li>SQL_MAX_CURSOR_NAME_LEN</li>
<li>SQL_MAX_DRIVER_CONNECTIONS</li>
<li>SQL_MAX_IDENTIFIER_LEN</li>
<li>SQL_MAX_INDEX_SIZE</li>
<li>SQL_MAX_PROCEDURE_NAME_LEN</li>
<li>SQL_MAX_ROW_SIZE</li>
<li>SQL_MAX_ROW_SIZE_INCLUDES_LONG</li>
<li>SQL_MAX_SCHEMA_NAME_LEN</li>
<li>SQL_MAX_STATEMENT_LEN</li>
<li>SQL_MAX_TABLES_IN_SELECT</li>
<li>SQL_MAX_TABLE_NAME_LEN</li>
<li>SQL_MAX_USER_NAME_LEN</li>
<li>SQL_MULTIPLE_ACTIVE_TXN</li>
<li>SQL_MULT_RESULT_SETS</li>
<li>SQL_NEED_LONG_DATA_LEN</li>
<li>SQL_NON_NULLABLE_COLUMNS</li>
<li>SQL_NO_NULLS</li>
<li>SQL_NULLABLE</li>
<li>SQL_NULLABLE_UNKNOWN</li>
<li>SQL_NULL_COLLATION</li>
<li>SQL_NUMERIC</li>
<li>SQL_NUMERIC_FUNCTIONS</li>
<li>SQL_ODBC_INTERFACE_CONFORMANCE</li>
<li>SQL_ODBC_VER</li>
<li>SQL_OJ_CAPABILITIES</li>
<li>SQL_ORDER_BY_COLUMNS_IN_SELECT</li>
<li>SQL_PARAM_ARRAY_ROW_COUNTS</li>
<li>SQL_PARAM_ARRAY_SELECTS</li>
<li>SQL_PC_NOT_PSEUDO</li>
<li>SQL_PC_PSEUDO</li>
<li>SQL_PC_UNKNOWN</li>
<li>SQL_PROCEDURES</li>
<li>SQL_PROCEDURE_TERM</li>
<li>SQL_QUOTED_IDENTIFIER_CASE</li>
<li>SQL_REAL</li>
<li>SQL_ROW_UPDATES</li>
<li>SQL_SCHEMA_TERM</li>
<li>SQL_SCHEMA_USAGE</li>
<li>SQL_SCOPE_CURROW</li>
<li>SQL_SCOPE_SESSION</li>
<li>SQL_SCOPE_TRANSACTION</li>
<li>SQL_SCROLL_OPTIONS</li>
<li>SQL_SEARCH_PATTERN_ESCAPE</li>
<li>SQL_SERVER_NAME</li>
<li>SQL_SMALLINT</li>
<li>SQL_SPECIAL_CHARACTERS</li>
<li>SQL_SQL92_DATETIME_FUNCTIONS</li>
<li>SQL_SQL92_FOREIGN_KEY_DELETE_RULE</li>
<li>SQL_SQL92_FOREIGN_KEY_UPDATE_RULE</li>
<li>SQL_SQL92_GRANT</li>
<li>SQL_SQL92_NUMERIC_VALUE_FUNCTIONS</li>
<li>SQL_SQL92_PREDICATES</li>
<li>SQL_SQL92_RELATIONAL_JOIN_OPERATORS</li>
<li>SQL_SQL92_REVOKE</li>
<li>SQL_SQL92_ROW_VALUE_CONSTRUCTOR</li>
<li>SQL_SQL92_STRING_FUNCTIONS</li>
<li>SQL_SQL92_VALUE_EXPRESSIONS</li>
<li>SQL_SQL_CONFORMANCE</li>
<li>SQL_STANDARD_CLI_CONFORMANCE</li>
<li>SQL_STATIC_CURSOR_ATTRIBUTES1</li>
<li>SQL_STATIC_CURSOR_ATTRIBUTES2</li>
<li>SQL_STRING_FUNCTIONS</li>
<li>SQL_SUBQUERIES</li>
<li>SQL_SYSTEM_FUNCTIONS</li>
<li>SQL_TABLE_TERM</li>
<li>SQL_TIMEDATE_ADD_INTERVALS</li>
<li>SQL_TIMEDATE_DIFF_INTERVALS</li>
<li>SQL_TIMEDATE_FUNCTIONS</li>
<li>SQL_TINYINT</li>
<li>SQL_TXN_CAPABLE</li>
<li>SQL_TXN_ISOLATION_OPTION</li>
<li>SQL_TYPE_DATE</li>
<li>SQL_TYPE_TIME</li>
<li>SQL_TYPE_TIMESTAMP</li>
<li>SQL_UNION</li>
<li>SQL_UNKNOWN_TYPE</li>
<li>SQL_USER_NAME</li>
<li>SQL_VARBINARY</li>
<li>SQL_VARCHAR</li>
<li>SQL_WCHAR</li>
<li>SQL_WLONGVARCHAR</li>
<li>SQL_WVARCHAR</li>
<li>SQL_XOPEN_CLI_YEAR</li>
</ul>
<h1 id="connection">Connection Objects</h1>
<h2 id="connection_autocommit">autocommit</h2>
<p>False if the connection is in manual-commit mode (the default), which is the mode described
by the DB API. True if the connection is in auto-commit mode. This can be set using the
autocommit keyword in the connection function or can be changed by setting this attribute.</p>
<h2 id="connection_searchesc">searchesc</h2>
<p>The search pattern escape character used to escape '%' and '_' in search patterns, as returned by
SQLGetInfo(SQL_SEARCH_PATTERN_ESCAPE). The value is driver specific.</p>
<h2>execute(sql, [params])</h2>
<p>This is a new method (not in the DB API) that creates a new Cursor object and returns
Cursor.execute(...). See <a href="#cursor_execute">Cursor.execute</a> for a description of the
parameters.</p>
<pre>
for results in cnxn.execute("select user_id from tmp"):
print results.user_id</pre>
<p>Since a new Cursor is created by each call, do not use when executing multiple statements in
a row.</p>
<h2>close()</h2>
<p>Close the connection now (rather than whenever __del__ is called). The connection will be
unusable from this point forward; a ProgrammingError exception will be raised if any operation
is attempted with the connection. The same applies to all cursor objects trying to use the
connection. Note that closing a connection without committing the changes first will cause an
implicit rollback to be performed.</p>
<h2>commit()</h2>
<p>Commit any pending transaction to the database.</p>
<p>Note that Connections do not (yet) support autocommit; <code>Connection.commit()</code> must
be called or changes will be rolled back when the connection is closed.</p>
<h2>rollback()</h2>
<p>Causes the the database to roll back to the start of any pending transaction.</p>
<h2>cursor()</h2>
<p>Return a new <a href="#cursor">Cursor</a> object using the connection.</p>
<h2 id="connection_getinfo">getinfo(infotype)</h2>
<p>Calls SQLGetInfo, passing <code>infotype</code> and returns the result as a Boolean, string,
integer, or long value. The return type is determined by <code>infotype</code>.</p>
<p>The infotype value should be one of the following constants, defined in the pyodbc module.
The table below shows the data type returned. See
<a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcsqlgetinfo.asp?frame=true&hidetoc=true">
SQLGetInfo</a> for the meaning of each constant.</p>
<table class="general">
<thead>
<tr><td>Constant</td><td>Return Type</td></tr>
</thead>
<tbody>
<tr><td>SQL_ACCESSIBLE_PROCEDURES</td><td>True or False</td></tr>
<tr class="treven"><td>SQL_ACCESSIBLE_TABLES</td><td>True or False</td></tr>
<tr><td>SQL_ACTIVE_ENVIRONMENTS</td><td>int</td></tr>
<tr class="treven"><td>SQL_AGGREGATE_FUNCTIONS</td><td>int or long</td></tr>
<tr><td>SQL_ALTER_DOMAIN</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_ALTER_TABLE</td><td>int or long</td></tr>
<tr><td>SQL_ASYNC_MODE</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_BATCH_ROW_COUNT</td><td>int or long</td></tr>
<tr><td>SQL_BATCH_SUPPORT</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_BOOKMARK_PERSISTENCE</td><td>int or long</td></tr>
<tr><td>SQL_CATALOG_LOCATION</td><td>int</td></tr>
<tr class="treven"><td>SQL_CATALOG_NAME</td><td>True or False</td></tr>
<tr><td>SQL_CATALOG_NAME_SEPARATOR</td><td>string</td></tr>
<tr class="treven"><td>SQL_CATALOG_TERM</td><td>string</td></tr>
<tr><td>SQL_CATALOG_USAGE</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_COLLATION_SEQ</td><td>string</td></tr>
<tr><td>SQL_COLUMN_ALIAS</td><td>True or False</td></tr>
<tr class="treven"><td>SQL_CONCAT_NULL_BEHAVIOR</td><td>int</td></tr>
<tr><td>SQL_CONVERT_FUNCTIONS</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_CONVERT_VARCHAR</td><td>int or long</td></tr>
<tr><td>SQL_CORRELATION_NAME</td><td>int</td></tr>
<tr class="treven"><td>SQL_CREATE_ASSERTION</td><td>int or long</td></tr>
<tr><td>SQL_CREATE_CHARACTER_SET</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_CREATE_COLLATION</td><td>int or long</td></tr>
<tr><td>SQL_CREATE_DOMAIN</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_CREATE_SCHEMA</td><td>int or long</td></tr>
<tr><td>SQL_CREATE_TABLE</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_CREATE_TRANSLATION</td><td>int or long</td></tr>
<tr><td>SQL_CREATE_VIEW</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_CURSOR_COMMIT_BEHAVIOR</td><td>int</td></tr>
<tr><td>SQL_CURSOR_ROLLBACK_BEHAVIOR</td><td>int</td></tr>
<tr class="treven"><td>SQL_DATABASE_NAME</td><td>string</td></tr>
<tr><td>SQL_DATA_SOURCE_NAME</td><td>string</td></tr>
<tr class="treven"><td>SQL_DATA_SOURCE_READ_ONLY</td><td>True or False</td></tr>
<tr><td>SQL_DATETIME_LITERALS</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_DBMS_NAME</td><td>string</td></tr>
<tr><td>SQL_DBMS_VER</td><td>string</td></tr>
<tr class="treven"><td>SQL_DDL_INDEX</td><td>int or long</td></tr>
<tr><td>SQL_DEFAULT_TXN_ISOLATION</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_DESCRIBE_PARAMETER</td><td>True or False</td></tr>
<tr><td>SQL_DM_VER</td><td>string</td></tr>
<tr class="treven"><td>SQL_DRIVER_HDESC</td><td>int or long</td></tr>
<tr><td>SQL_DRIVER_HENV</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_DRIVER_HLIB</td><td>int or long</td></tr>
<tr><td>SQL_DRIVER_HSTMT</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_DRIVER_NAME</td><td>string</td></tr>
<tr><td>SQL_DRIVER_ODBC_VER</td><td>string</td></tr>
<tr class="treven"><td>SQL_DRIVER_VER</td><td>string</td></tr>
<tr><td>SQL_DROP_ASSERTION</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_DROP_CHARACTER_SET</td><td>int or long</td></tr>
<tr><td>SQL_DROP_COLLATION</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_DROP_DOMAIN</td><td>int or long</td></tr>
<tr><td>SQL_DROP_SCHEMA</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_DROP_TABLE</td><td>int or long</td></tr>
<tr><td>SQL_DROP_TRANSLATION</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_DROP_VIEW</td><td>int or long</td></tr>
<tr><td>SQL_DYNAMIC_CURSOR_ATTRIBUTES1</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_DYNAMIC_CURSOR_ATTRIBUTES2</td><td>int or long</td></tr>
<tr><td>SQL_EXPRESSIONS_IN_ORDERBY</td><td>True or False</td></tr>
<tr class="treven"><td>SQL_FILE_USAGE</td><td>int</td></tr>
<tr><td>SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES1</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES2</td><td>int or long</td></tr>
<tr><td>SQL_GETDATA_EXTENSIONS</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_GROUP_BY</td><td>int</td></tr>
<tr><td>SQL_IDENTIFIER_CASE</td><td>int</td></tr>
<tr class="treven"><td>SQL_IDENTIFIER_QUOTE_CHAR</td><td>string</td></tr>
<tr><td>SQL_INDEX_KEYWORDS</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_INFO_SCHEMA_VIEWS</td><td>int or long</td></tr>
<tr><td>SQL_INSERT_STATEMENT</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_INTEGRITY</td><td>True or False</td></tr>
<tr><td>SQL_KEYSET_CURSOR_ATTRIBUTES1</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_KEYSET_CURSOR_ATTRIBUTES2</td><td>int or long</td></tr>
<tr><td>SQL_KEYWORDS</td><td>string</td></tr>
<tr class="treven"><td>SQL_LIKE_ESCAPE_CLAUSE</td><td>True or False</td></tr>
<tr><td>SQL_MAX_ASYNC_CONCURRENT_STATEMENTS</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_MAX_BINARY_LITERAL_LEN</td><td>int or long</td></tr>
<tr><td>SQL_MAX_CATALOG_NAME_LEN</td><td>int</td></tr>
<tr class="treven"><td>SQL_MAX_CHAR_LITERAL_LEN</td><td>int or long</td></tr>
<tr><td>SQL_MAX_COLUMNS_IN_GROUP_BY</td><td>int</td></tr>
<tr class="treven"><td>SQL_MAX_COLUMNS_IN_INDEX</td><td>int</td></tr>
<tr><td>SQL_MAX_COLUMNS_IN_ORDER_BY</td><td>int</td></tr>
<tr class="treven"><td>SQL_MAX_COLUMNS_IN_SELECT</td><td>int</td></tr>
<tr><td>SQL_MAX_COLUMNS_IN_TABLE</td><td>int</td></tr>
<tr class="treven"><td>SQL_MAX_COLUMN_NAME_LEN</td><td>int</td></tr>
<tr><td>SQL_MAX_CONCURRENT_ACTIVITIES</td><td>int</td></tr>
<tr class="treven"><td>SQL_MAX_CURSOR_NAME_LEN</td><td>int</td></tr>
<tr><td>SQL_MAX_DRIVER_CONNECTIONS</td><td>int</td></tr>
<tr class="treven"><td>SQL_MAX_IDENTIFIER_LEN</td><td>int</td></tr>
<tr><td>SQL_MAX_INDEX_SIZE</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_MAX_PROCEDURE_NAME_LEN</td><td>int</td></tr>
<tr><td>SQL_MAX_ROW_SIZE</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_MAX_ROW_SIZE_INCLUDES_LONG</td><td>True or False</td></tr>
<tr><td>SQL_MAX_SCHEMA_NAME_LEN</td><td>int</td></tr>
<tr class="treven"><td>SQL_MAX_STATEMENT_LEN</td><td>int or long</td></tr>
<tr><td>SQL_MAX_TABLES_IN_SELECT</td><td>int</td></tr>
<tr class="treven"><td>SQL_MAX_TABLE_NAME_LEN</td><td>int</td></tr>
<tr><td>SQL_MAX_USER_NAME_LEN</td><td>int</td></tr>
<tr class="treven"><td>SQL_MULTIPLE_ACTIVE_TXN</td><td>True or False</td></tr>
<tr><td>SQL_MULT_RESULT_SETS</td><td>True or False</td></tr>
<tr class="treven"><td>SQL_NEED_LONG_DATA_LEN</td><td>True or False</td></tr>
<tr><td>SQL_NON_NULLABLE_COLUMNS</td><td>int</td></tr>
<tr class="treven"><td>SQL_NULL_COLLATION</td><td>int</td></tr>
<tr><td>SQL_NUMERIC_FUNCTIONS</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_ODBC_INTERFACE_CONFORMANCE</td><td>int or long</td></tr>
<tr><td>SQL_ODBC_VER</td><td>string</td></tr>
<tr class="treven"><td>SQL_OJ_CAPABILITIES</td><td>int or long</td></tr>
<tr><td>SQL_ORDER_BY_COLUMNS_IN_SELECT</td><td>True or False</td></tr>
<tr class="treven"><td>SQL_PARAM_ARRAY_ROW_COUNTS</td><td>int or long</td></tr>
<tr><td>SQL_PARAM_ARRAY_SELECTS</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_PROCEDURES</td><td>True or False</td></tr>
<tr><td>SQL_PROCEDURE_TERM</td><td>string</td></tr>
<tr class="treven"><td>SQL_QUOTED_IDENTIFIER_CASE</td><td>int</td></tr>
<tr><td>SQL_ROW_UPDATES</td><td>True or False</td></tr>
<tr class="treven"><td>SQL_SCHEMA_TERM</td><td>string</td></tr>
<tr><td>SQL_SCHEMA_USAGE</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_SCROLL_OPTIONS</td><td>int or long</td></tr>
<tr><td>SQL_SEARCH_PATTERN_ESCAPE</td><td>string</td></tr>
<tr class="treven"><td>SQL_SERVER_NAME</td><td>string</td></tr>
<tr><td>SQL_SPECIAL_CHARACTERS</td><td>string</td></tr>
<tr class="treven"><td>SQL_SQL92_DATETIME_FUNCTIONS</td><td>int or long</td></tr>
<tr><td>SQL_SQL92_FOREIGN_KEY_DELETE_RULE</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_SQL92_FOREIGN_KEY_UPDATE_RULE</td><td>int or long</td></tr>
<tr><td>SQL_SQL92_GRANT</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_SQL92_NUMERIC_VALUE_FUNCTIONS</td><td>int or long</td></tr>
<tr><td>SQL_SQL92_PREDICATES</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_SQL92_RELATIONAL_JOIN_OPERATORS</td><td>int or long</td></tr>
<tr><td>SQL_SQL92_REVOKE</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_SQL92_ROW_VALUE_CONSTRUCTOR</td><td>int or long</td></tr>
<tr><td>SQL_SQL92_STRING_FUNCTIONS</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_SQL92_VALUE_EXPRESSIONS</td><td>int or long</td></tr>
<tr><td>SQL_SQL_CONFORMANCE</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_STANDARD_CLI_CONFORMANCE</td><td>int or long</td></tr>
<tr><td>SQL_STATIC_CURSOR_ATTRIBUTES1</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_STATIC_CURSOR_ATTRIBUTES2</td><td>int or long</td></tr>
<tr><td>SQL_STRING_FUNCTIONS</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_SUBQUERIES</td><td>int or long</td></tr>
<tr><td>SQL_SYSTEM_FUNCTIONS</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_TABLE_TERM</td><td>string</td></tr>
<tr><td>SQL_TIMEDATE_ADD_INTERVALS</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_TIMEDATE_DIFF_INTERVALS</td><td>int or long</td></tr>
<tr><td>SQL_TIMEDATE_FUNCTIONS</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_TXN_CAPABLE</td><td>int</td></tr>
<tr><td>SQL_TXN_ISOLATION_OPTION</td><td>int or long</td></tr>
<tr class="treven"><td>SQL_UNION</td><td>int or long</td></tr>
<tr><td>SQL_USER_NAME</td><td>string</td></tr>
<tr class="treven"><td>SQL_XOPEN_CLI_YEAR</td><td>string</td></tr>
</tbody>
</table>
<h1 id="cursor">Cursor Objects</h1>
<p>These objects represent a database cursor, which is used to manage the context of a fetch operation. Cursors
created from the same connection are not isolated, i.e., any changes done to the database by a cursor are immediately
visible by the other cursors.</p>
<h2>description</h2>
<p>This read-only attribute is a sequence of 7-item sequences. Each of these sequences contains information describing
one result column: (name, type_code, display_size, internal_size, precision, scale, null_ok). pyodbc only provides
values for name, type_code, internal_size, and null_ok. The other values are set to None.
This attribute will be None for operations that do not return rows or if the cursor has not had an operation invoked
via the executeXXX() method yet.
The type_code member is the class type used to create the Python objects when reading rows. For example, a varchar
column's type will be <i>str</i>. The complete list of types supported is listed in the <a href="#datatypes">Data
Types</a> section.</p>
<h2>rowcount</h2>
<p>This is always -1.</p>
<h2>callproc(procname[,parameters])</h2>
<p>This is not yet supported.</p>
<h2>close()</h2>
<p>Close the cursor now (rather than whenever __del__ is called). The cursor will be unusable from this point forward;
a ProgrammingError exception will be raised if any operation is attempted with the cursor.</p>
<h2 id="cursor_execute">execute(sql [,parameters])</h2>
<p>Prepare and execute SQL. Parameters may be passed as a sequence, as specified by the DB API, or as individual
parameters.</p>
<pre>
# standard
cursor.execute("select a from tbl where b=? and c=?", (x, y))
# pyodbc extension
cursor.execute("select a from tbl where b=? and c=?", x, y)</pre>
<p>The DB API specification does not specify the return value of this method. Cursors in pyodbc return different
things based on the SQL statement executed. Select statements return the Cursor object itself to allow more compact
code such as putting the execute method into for loops or appending fetchone or fetchall:</p>
<pre>
for row in cursor.execute("select album_id, photo_id from photos"):
print row.album_id, row.photo_id
row = cursor.execute("select count(*) from tmp").fetchone()
rows = cursor.execute("select * from tmp").fetchall()</pre>
<p>Update and delete statements return the number of rows affected:</p>
<pre>
count = cursor.execute("update photos set processed=1 where user_id=1")
count = cursor.execute("delete from photos where user_id=1")</pre>
<p>All other statements return <code>None</code>.</p>
<h2>executemany(sql, seq_of_parameters)</h2>
<p>Prepare a database operation (query or command) and then execute it against all parameter sequences or mappings
found in the sequence seq_of_parameters. This method returns <code>None</code>.</p>
<h2>fetchone()</h2>
<p>Fetch the next row of a query result set, returning a single <a href="#row">Row</a>, or <code>None</code> when no more
data is available.</p>
<p>A ProgrammingError exception is raised if the previous call to executeXXX() did not produce any result set or no
call was issued yet.</p>
<pre>
cursor.execute("select user_name from photos where user_id=?", userid)
row = cursor.fetchone()
if row:
print row.user_name</pre>
<h2>nextset, setinputsizes, setoutputsize</h2>
<p>These are optional in the API and are not supported.</p>
<h2>fetchmany([size=cursor.arraysize])</h2>
<p>Fetch the next set of rows of a query result, returning a list of <a href="#row">Rows</a>. An empty list is returned
when no more rows are available.</p>
<p>The number of rows to fetch per call is specified by the parameter. If it is not given, the cursor's arraysize,
which defaults to 1, determines the number of rows to be fetched. If this is not possible due to the specified number
of rows not being available, fewer rows may be returned.</p>
<p>A ProgrammingError exception is raised if the previous call to executeXXX() did not produce any result set or no
call was issued yet.</p>
<h2>fetchall()</h2>
<p>Fetch all remaining rows of a query result, returning them as a list of <a href="#row">Rows</a>. Since this reads
all rows into memory, it should not be used if there are a lot of rows. Consider iterating over the rows instead.</p>
<p>A ProgrammingError exception is raised if the previous call to executeXXX() did not produce any result set or no
call was issued yet.</p>
<pre>
cursor.execute("select photo_id from photos where user_id=1")
rows = cursor.fetchall()
for row in rows:
print row.user_name</pre>
<h2>__iter__, next</h2>
<p>These methods allow a cursor to be used in a <code>for</code> loop, returning a single <a href="#row">Row</a> for
each iteration. This allows all rows to be visited easily.</p>
<pre>
cursor.execute("select photo_id from photos where user_id=1")
for row in cursor:
print row.photo_id</pre>
<h2 id="cursor_tables">tables(table=None, catalog=None, schema=None, tableType=None)</h2>
<p>Executes SQLTables and creates a results set of tables defined in the data source. Returns the Cursor.</p>
<p>The table, catalog, and schema interpret the '_' and '%' characters as wildcards. The escape character is driver
specific, so use <a href="#connection_searchesc">Connection.searchescape</a>.</p>
<p>Each row has the following columns. See the SQLTables documentation for more information.</p>
<ol start="0">
<li>table_cat: The catalog name.</li>
<li>table_schem: The schema name.</li>
<li>table_name: The table name.</li>
<li>table_type: One of 'TABLE', 'VIEW', SYSTEM TABLE', 'GLOBAL TEMPORARY'
'LOCAL TEMPORARY', 'ALIAS', 'SYNONYM', or a data source-specific type name.</li>
</ol>
<pre>
for row in cursor.tables():
print row.table_name</pre>
<h2 id="cursor_columns">columns(table=None, catalog=None, schema=None, column=None)</h2>
<p>Creates a results set of column names in specified tables by executing the ODBC SQLColumns function.
Each row fetched has the following columns:</p>
<ol start="0">
<li>table_cat</li>
<li>table_schem</li>
<li>table_name</li>
<li>column_name</li>
<li>data_type</li>
<li>type_name</li>
<li>column_size</li>
<li>buffer_length</li>
<li>decimal_digits</li>
<li>num_prec_radix</li>
<li>nullable</li>
<li>remarks</li>
<li>column_def</li>
<li>sql_data_type</li>
<li>sql_datetime_sub</li>
<li>char_octet_length</li>
<li>ordinal_position</li>
<li>is_nullable: One of SQL_NULLABLE, SQL_NO_NULLS, SQL_NULLS_UNKNOWN.</li>
</ol>
<h2 id="cursor_statistics">statistics(table, catalog=None, schema=None, unique=False, quick=True)</h2>
Creates a results set of statistics about a single table and the indexes
associated with the table by executing SQLStatistics.
<dl>
<dt>unique</dt>
<dd>If <code>True</code>, only unique indexes are retured. Otherwise all
indexes are returned.</dd>
<dt>quick</dt>
<dd>If <code>True</code>, CARDINALITY and PAGES are returned only if they are
readily available from the server</dd>
</dl>
<p>Each row fetched has the following columns:</p>
<ol start="0">
<li>table_cat</li>
<li>table_schem</li>
<li>table_name</li>
<li>non_unique</li>
<li>index_qualifier</li>
<li>index_name</li>
<li>type</li>
<li>ordinal_position</li>
<li>column_name</li>
<li>asc_or_desc</li>
<li>cardinality</li>
<li>pages</li>
<li>filter_condition</li>
</ol>
<h2 id="cursor_rowid">rowIdColumns(table, catalog=None, schema=None, nullable=True)</h2>
<p>Executes SQLSpecialColumns with SQL_BEST_ROWID which creates a result set of columns that uniquely identify a row.
Returns the Cursor object. Each row fetched has the following columns.</p>
<ol start="0">
<li>scope: One of SQL_SCOPE_CURROW, SQL_SCOPE_TRANSACTION, or SQL_SCOPE_SESSION</li>
<li>column_name</li>
<li>data_type: The ODBC SQL data type constant (e.g. SQL_CHAR)</li>
<li>type_name</li>
<li>column_size</li>
<li>buffer_length</li>
<li>decimal_digits</li>
<li>pseudo_column: One of SQL_PC_UNKNOWN, SQL_PC_NOT_PSEUDO, SQL_PC_PSEUDO</li>
</ol>
<h2 id="cursor_rowver">rowVerColumns(table, catalog=None, schema=None, nullable=True)</h2>
<p>Executes SQLSpecialColumns with SQL_ROWVER which creates a result set of
columns that are automatically updated when any value in the row is updated.
Returns the Cursor object. Each row fetched has the following columns.</p>
<ol start="0">
<li>scope: One of SQL_SCOPE_CURROW, SQL_SCOPE_TRANSACTION, or SQL_SCOPE_SESSION</li>
<li>column_name</li>
<li>data_type: The ODBC SQL data type constant (e.g. SQL_CHAR)</li>
<li>type_name</li>
<li>column_size</li>
<li>buffer_length</li>
<li>decimal_digits</li>
<li>pseudo_column: One of SQL_PC_UNKNOWN, SQL_PC_NOT_PSEUDO, SQL_PC_PSEUDO</li>
</ol>
<h2 id="cursor_primarykeys">primaryKeys(table, catalog=None, schema=None)</h2>
<p>Creates a results set of column names that make up the primary key for a
table by executing the SQLPrimaryKeys function. Each row fetched has the
following columns:</p>
<ol start="0">
<li>table_cat</li>
<li>table_schem</li>
<li>table_name</li>
<li>column_name</li>
<li>key_seq</li>
<li>pk_name</li>
</ol>
<h2 id="cursor_foreignkeys">foreignKeys(table=None, catalog=None, schema=None,
foreignTable=None, foreignCatalog=None, foreignSchema=None)</h2>
<p>Executes the SQLForeignKeys function and creates a results set of column
names that are foreign keys in the specified table (columns in the specified
table that refer to primary keys in other tables) or foreign keys in other
tables that refer to the primary key in the specified table. Each row fetched
has the following columns:</p>
<ol start="0">
<li>pktable_cat</li>
<li>pktable_schem</li>
<li>pktable_name</li>
<li>pkcolumn_name</li>
<li>fktable_cat</li>
<li>fktable_schem</li>
<li>fktable_name</li>
<li>fkcolumn_name</li>
<li>key_seq</li>
<li>update_rule</li>
<li>delete_rule</li>
<li>fk_name</li>
<li>pk_name</li>
<li>deferrability</li>
</ol>
<h2 id="cursor_procedures">procedures(procedure=None, catalog=None, schema=None)</h2>
<p>Executes SQLProcedures and creates a result set of information about the
procedures in the data source. Each row fetched has the following columns:</p>
<ol start="0">
<li>procedure_cat</li>
<li>procedure_schem</li>
<li>procedure_name</li>
<li>num_input_params</li>
<li>num_output_params</li>
<li>num_result_sets</li>
<li>remarks</li>
<li>procedure_type</li>
</ol>
<h2 id="cursor_gettypeinfo">getTypeInfo(sqlType=None)</h2>
<p>Executes SQLGetTypeInfo a creates a result set with information about the
specified data type or all data types supported by the ODBC driver if not
specified. Each row fetched has the following columns:</p>
<ol start="0">
<li>type_name</li>
<li>data_type</li>
<li>column_size</li>
<li>literal_prefix</li>
<li>literal_suffix</li>
<li>create_params</li>
<li>nullable</li>
<li>case_sensitive</li>
<li>searchable</li>
<li>unsigned_attribute</li>
<li>fixed_prec_scale</li>
<li>auto_unique_value</li>
<li>local_type_name</li>
<li>minimum_scale</li>
<li>maximum_scale</li>
<li>sql_data_type</li>
<li>sql_datetime_sub</li>
<li>num_prec_radix</li>
<li>interval_precision</li>
</ol>
<h1 id="row">Row Objects</h1>
<h2>cursor_description</h2>
<p>The column metadata from Cursor.description is also accessible from Row objects as
the <code>cursor_description</code> attribute. This is convenient when Rows are used as ad-hoc
data structures and are passed to other functions that need the metadata; now the Cursor does
not need to be passed with them.</p>
<pre>
row = cursor.execute("select name, account_id from persons").fetchone()
column_names = [ t[0] for t in row.cursor_description ]
</pre>
<h2>Accessing Values</h2>
<p>The DB API specifies that results must be tuple-like, so columns are normally accessed by indexing into the
sequence (e.g. <code>row[0]</code>) and pyodbc supports this. However, columns can also be accessed by name:</p>
<pre>
cursor.execute("select <b>album_id</b>, <b>photo_id</b> from photos where user_id=1")
row = cursor.fetchone()
print <b>row.album_id</b>, <b>row.photo_id</b>
print row[0], row[1] # same as above, but less readable</pre>
<p>This makes the code easier to maintain when modifying SQL, more readable, and allows rows to be used where a
custom class might otherwise be used. All rows from a single <code>execute</code> share the same dictionary of
column names, so using Row objects to hold a large result set may also use less memory than creating a object for
each row.</p>
<p>The SQL "as" keyword allows the name of a column in the result set to be specified. This is useful if a column
name has a spaces or if there is no name:</p>
<pre>
cursor.execute("select count(*) <b>as photo_count</b> from photos where user_id=1")
row = cursor.fetchone()
print <b>row.photo_count</b></pre>
<h2>Rows Are Mutable</h2>
<p>Though SQL is very powerful, values sometimes need to be modified before they can be used.
Rows allow their values to be replaced, which makes Rows even more convenient ad-hoc dat
structures.</p>
<pre>
# Replace a datetime in each row with one that has a time zone.
rows = cursor.fetchall()
for row in rows:
row.start_date = row.start_date.astimezone(tz)</pre>
<p>Note that only existing columns can be replaced; new columns cannot be added to rows. If
you want to add a value that doesn't exist in the database, add a NULL to the select statement
as a placeholder:</p>
<pre>
row = cursor.execute("select name, NULL as account_id from persons").fetchone()
row.account_id = 1
</pre>
<h1 id="datatypes">Data Types</h1>
<p>The following table shows the ODBC data types supported and the Python type used to represent values.
<code>None</code> is always used for <code>NULL</code> values.</p>
<table border="0" cellspacing="4">
<thead>
<tr>
<td>ODBC</td><td>Python</td></tr>
</thead>
<tbody>
<tr>
<td>char, varchar, longvarchar, GUID</td>
<td>string</td>