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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321
|
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.3.9: http://docutils.sourceforge.net/" />
<title>PyGreSQL Programming Information</title>
<meta content="The classic PyGreSQL interface (pg module)" name="description" />
<meta content="PyGreSQL, pg, PostGreSQL, Python" name="keywords" />
<link rel="stylesheet" href="docs.css" type="text/css" />
</head>
<body>
<div class="document" id="pygresql-programming-information">
<h1 class="title">PyGreSQL Programming Information</h1>
<h2 class="subtitle" id="the-classic-pygresql-interface-pg-module">The classic PyGreSQL interface (pg module)</h2>
<div class="contents topic" id="contents">
<p class="topic-title first"><a name="contents">Contents</a></p>
<ul class="auto-toc simple">
<li><a class="reference" href="#introduction" id="id5" name="id5">1 Introduction</a></li>
<li><a class="reference" href="#module-functions-and-constants" id="id6" name="id6">2 Module functions and constants</a><ul class="auto-toc">
<li><a class="reference" href="#connect-opens-a-pg-connection" id="id7" name="id7">2.1 connect - opens a pg connection</a></li>
<li><a class="reference" href="#get-defhost-set-defhost-default-server-host-dv" id="id8" name="id8">2.2 get_defhost, set_defhost - default server host [DV]</a></li>
<li><a class="reference" href="#get-defport-set-defport-default-server-port-dv" id="id9" name="id9">2.3 get_defport, set_defport - default server port [DV]</a></li>
<li><a class="reference" href="#get-defopt-set-defopt-default-connection-options-dv" id="id10" name="id10">2.4 get_defopt, set_defopt - default connection options [DV]</a></li>
<li><a class="reference" href="#get-deftty-set-deftty-default-debug-tty-dv" id="id11" name="id11">2.5 get_deftty, set_deftty - default debug tty [DV]</a></li>
<li><a class="reference" href="#get-defbase-set-defbase-default-database-name-dv" id="id12" name="id12">2.6 get_defbase, set_defbase - default database name [DV]</a></li>
<li><a class="reference" href="#escape-string-escape-a-string-for-use-within-sql" id="id13" name="id13">2.7 escape_string - escape a string for use within SQL</a></li>
<li><a class="reference" href="#escape-bytea-escape-binary-data-for-use-within-sql-as-type-bytea" id="id14" name="id14">2.8 escape_bytea - escape binary data for use within SQL as type <cite>bytea</cite></a></li>
<li><a class="reference" href="#unescape-bytea-unescape-bytea-data-that-has-been-retrieved-as-text" id="id15" name="id15">2.9 unescape_bytea -- unescape <cite>bytea</cite> data that has been retrieved as text</a></li>
<li><a class="reference" href="#module-constants" id="id16" name="id16">2.10 Module constants</a></li>
</ul>
</li>
<li><a class="reference" href="#connection-objects-pgobject" id="id17" name="id17">3 Connection objects: pgobject</a><ul class="auto-toc">
<li><a class="reference" href="#query-executes-a-sql-command-string" id="id18" name="id18">3.1 query - executes a SQL command string</a></li>
<li><a class="reference" href="#reset-resets-the-connection" id="id19" name="id19">3.2 reset - resets the connection</a></li>
<li><a class="reference" href="#cancel-abandon-processing-of-current-sql-command" id="id20" name="id20">3.3 cancel - abandon processing of current SQL command</a></li>
<li><a class="reference" href="#close-close-the-database-connection" id="id21" name="id21">3.4 close - close the database connection</a></li>
<li><a class="reference" href="#fileno-returns-the-socket-used-to-connect-to-the-database" id="id22" name="id22">3.5 fileno - returns the socket used to connect to the database</a></li>
<li><a class="reference" href="#getnotify-gets-the-last-notify-from-the-server" id="id23" name="id23">3.6 getnotify - gets the last notify from the server</a></li>
<li><a class="reference" href="#inserttable-insert-a-list-into-a-table" id="id24" name="id24">3.7 inserttable - insert a list into a table</a></li>
<li><a class="reference" href="#putline-writes-a-line-to-the-server-socket-da" id="id25" name="id25">3.8 putline - writes a line to the server socket [DA]</a></li>
<li><a class="reference" href="#getline-gets-a-line-from-server-socket-da" id="id26" name="id26">3.9 getline - gets a line from server socket [DA]</a></li>
<li><a class="reference" href="#endcopy-synchronizes-client-and-server-da" id="id27" name="id27">3.10 endcopy - synchronizes client and server [DA]</a></li>
<li><a class="reference" href="#locreate-create-a-large-object-in-the-database-lo" id="id28" name="id28">3.11 locreate - create a large object in the database [LO]</a></li>
<li><a class="reference" href="#getlo-build-a-large-object-from-given-oid-lo" id="id29" name="id29">3.12 getlo - build a large object from given oid [LO]</a></li>
<li><a class="reference" href="#loimport-import-a-file-to-a-large-object-lo" id="id30" name="id30">3.13 loimport - import a file to a large object [LO]</a></li>
<li><a class="reference" href="#object-attributes" id="id31" name="id31">3.14 Object attributes</a></li>
</ul>
</li>
<li><a class="reference" href="#the-db-wrapper-class" id="id32" name="id32">4 The DB wrapper class</a><ul class="auto-toc">
<li><a class="reference" href="#initialization" id="id33" name="id33">4.1 Initialization</a></li>
<li><a class="reference" href="#pkey-return-the-primary-key-of-a-table" id="id34" name="id34">4.2 pkey - return the primary key of a table</a></li>
<li><a class="reference" href="#get-databases-get-list-of-databases-in-the-system" id="id35" name="id35">4.3 get_databases - get list of databases in the system</a></li>
<li><a class="reference" href="#get-relations-get-list-of-relations-in-connected-database" id="id36" name="id36">4.4 get_relations - get list of relations in connected database</a></li>
<li><a class="reference" href="#get-tables-get-list-of-tables-in-connected-database" id="id37" name="id37">4.5 get_tables - get list of tables in connected database</a></li>
<li><a class="reference" href="#get-attnames-get-the-attribute-names-of-a-table" id="id38" name="id38">4.6 get_attnames - get the attribute names of a table</a></li>
<li><a class="reference" href="#get-get-a-row-from-a-database-table-or-view" id="id39" name="id39">4.7 get - get a row from a database table or view</a></li>
<li><a class="reference" href="#insert-insert-a-row-into-a-database-table" id="id40" name="id40">4.8 insert - insert a row into a database table</a></li>
<li><a class="reference" href="#update-update-a-row-in-a-database-table" id="id41" name="id41">4.9 update - update a row in a database table</a></li>
<li><a class="reference" href="#clear-clears-row-values-in-memory" id="id42" name="id42">4.10 clear - clears row values in memory</a></li>
<li><a class="reference" href="#delete-delete-a-row-from-a-database-table" id="id43" name="id43">4.11 delete - delete a row from a database table</a></li>
<li><a class="reference" href="#id1" id="id44" name="id44">4.12 escape_string - escape a string for use within SQL</a></li>
<li><a class="reference" href="#id2" id="id45" name="id45">4.13 escape_bytea - escape binary data for use within SQL as type <cite>bytea</cite></a></li>
<li><a class="reference" href="#id3" id="id46" name="id46">4.14 unescape_bytea -- unescape <cite>bytea</cite> data that has been retrieved as text</a></li>
</ul>
</li>
<li><a class="reference" href="#pgqueryobject-methods" id="id47" name="id47">5 pgqueryobject methods</a><ul class="auto-toc">
<li><a class="reference" href="#getresult-get-query-values-as-list-of-tuples" id="id48" name="id48">5.1 getresult - get query values as list of tuples</a></li>
<li><a class="reference" href="#dictresult-get-query-values-as-list-of-dictionaries" id="id49" name="id49">5.2 dictresult - get query values as list of dictionaries</a></li>
<li><a class="reference" href="#listfields-lists-fields-names-of-previous-query-result" id="id50" name="id50">5.3 listfields - lists fields names of previous query result</a></li>
<li><a class="reference" href="#fieldname-fieldnum-field-name-number-conversion" id="id51" name="id51">5.4 fieldname, fieldnum - field name/number conversion</a></li>
<li><a class="reference" href="#ntuples-return-number-of-tuples-in-query-object" id="id52" name="id52">5.5 ntuples - return number of tuples in query object</a></li>
</ul>
</li>
<li><a class="reference" href="#large-objects-pglarge" id="id53" name="id53">6 Large objects: pglarge</a><ul class="auto-toc">
<li><a class="reference" href="#open-opens-a-large-object" id="id54" name="id54">6.1 open - opens a large object</a></li>
<li><a class="reference" href="#close-closes-a-large-object" id="id55" name="id55">6.2 close - closes a large object</a></li>
<li><a class="reference" href="#read-write-tell-seek-unlink-file-like-large-object-handling" id="id56" name="id56">6.3 read, write, tell, seek, unlink - file like large object handling</a></li>
<li><a class="reference" href="#size-gives-the-large-object-size" id="id57" name="id57">6.4 size - gives the large object size</a></li>
<li><a class="reference" href="#export-saves-a-large-object-to-a-file" id="id58" name="id58">6.5 export - saves a large object to a file</a></li>
<li><a class="reference" href="#id4" id="id59" name="id59">6.6 Object attributes</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="introduction">
<h1><a class="toc-backref" href="#id5" name="introduction">1 Introduction</a></h1>
<p>You may either choose to use the
<a class="reference" href="pg.html">"classic" PyGreSQL interface</a>
provided by the <cite>pg</cite> module or else the
<a class="reference" href="pgdb.html">DB-API 2.0 compliant interface</a>
provided by the <cite>pgdb</cite> module.</p>
<p>The following documentation covers only the older <cite>pg</cite> API.</p>
<p>The <cite>pg</cite> module handles three types of objects,</p>
<ul class="simple">
<li>the <cite>pgobject</cite>, which handles the connection
and all the requests to the database,</li>
<li>the <cite>pglarge</cite> object, which handles
all the accesses to PostgreSQL large objects,</li>
<li>the <cite>pgqueryobject</cite> that handles query results</li>
</ul>
<p>and it provides a convenient wrapper class <cite>DB</cite> for the <cite>pgobject</cite>.</p>
<p>If you want to see a simple example of the use of some of these functions,
see <a class="reference" href="http://ontario.bikerides.ca">http://ontario.bikerides.ca</a> where you can find a link at the bottom to the
actual Python code for the page.</p>
</div>
<div class="section" id="module-functions-and-constants">
<h1><a class="toc-backref" href="#id6" name="module-functions-and-constants">2 Module functions and constants</a></h1>
<p>The <cite>pg</cite> module defines a few functions that allow to connect
to a database and to define "default variables" that override
the environment variables used by PostgreSQL.</p>
<p>These "default variables" were designed to allow you to handle general
connection parameters without heavy code in your programs. You can prompt the
user for a value, put it in the default variable, and forget it, without
having to modify your environment. The support for default variables can be
disabled by setting the -DNO_DEF_VAR option in the Python setup file. Methods
relative to this are specified by the tag [DV].</p>
<p>All variables are set to <cite>None</cite> at module initialization, specifying that
standard environment variables should be used.</p>
<div class="section" id="connect-opens-a-pg-connection">
<h2><a class="toc-backref" href="#id7" name="connect-opens-a-pg-connection">2.1 connect - opens a pg connection</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
connect([dbname], [host], [port], [opt], [tty], [user], [passwd])
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">dbname:</th><td class="field-body">name of connected database (string/None)</td>
</tr>
<tr class="field"><th class="field-name">host:</th><td class="field-body">name of the server host (string/None)</td>
</tr>
<tr class="field"><th class="field-name">port:</th><td class="field-body">port used by the database server (integer/-1)</td>
</tr>
<tr class="field"><th class="field-name">opt:</th><td class="field-body">connection options (string/None)</td>
</tr>
<tr class="field"><th class="field-name">tty:</th><td class="field-body">debug terminal (string/None)</td>
</tr>
<tr class="field"><th class="field-name">user:</th><td class="field-body">PostgreSQL user (string/None)</td>
</tr>
<tr class="field"><th class="field-name">passwd:</th><td class="field-body">password for user (string/None)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">pgobject:</th><td class="field-body">If successful, the <cite>pgobject</cite> handling the connection</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">bad argument type, or too many arguments</td>
</tr>
<tr class="field"><th class="field-name">SyntaxError:</th><td class="field-body">duplicate argument definition</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">pg.InternalError:</th></tr>
<tr><td> </td><td class="field-body">some error occurred during pg connection definition</td>
</tr>
</tbody>
</table>
<p class="last">(plus all exceptions relative to object allocation)</p>
</dd>
<dt>Description:</dt>
<dd>This function opens a connection to a specified database on a given
PostgreSQL server. You can use keywords here, as described in the
Python tutorial. The names of the keywords are the name of the
parameters given in the syntax line. For a precise description
of the parameters, please refer to the PostgreSQL user manual.</dd>
</dl>
<p>Examples:</p>
<pre class="literal-block">
import pg
con1 = pg.connect('testdb', 'myhost', 5432, None, None, 'bob', None)
con2 = pg.connect(dbname='testdb', host='localhost', user='bob')
</pre>
</div>
<div class="section" id="get-defhost-set-defhost-default-server-host-dv">
<h2><a class="toc-backref" href="#id8" name="get-defhost-set-defhost-default-server-host-dv">2.2 get_defhost, set_defhost - default server host [DV]</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
get_defhost()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">string, None:</th><td class="field-body">default host specification</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method returns the current default host specification,
or <cite>None</cite> if the environment variables should be used.
Environment variables won't be looked up.</dd>
</dl>
<p>Syntax:</p>
<pre class="literal-block">
set_defhost(host)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">host:</th><td class="field-body">new default host (string/None)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">string, None:</th><td class="field-body">previous default host specification</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">bad argument type, or too many arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This methods sets the default host value for new connections.
If <cite>None</cite> is supplied as parameter, environment variables will
be used in future connections. It returns the previous setting
for default host.</dd>
</dl>
</div>
<div class="section" id="get-defport-set-defport-default-server-port-dv">
<h2><a class="toc-backref" href="#id9" name="get-defport-set-defport-default-server-port-dv">2.3 get_defport, set_defport - default server port [DV]</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
get_defport()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">integer, None:</th><td class="field-body">default port specification</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method returns the current default port specification,
or <cite>None</cite> if the environment variables should be used.
Environment variables won't be looked up.</dd>
</dl>
<p>Syntax:</p>
<pre class="literal-block">
set_defport(port)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">port:</th><td class="field-body">new default port (integer/-1)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">integer, None:</th><td class="field-body">previous default port specification</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This methods sets the default port value for new connections. If -1 is
supplied as parameter, environment variables will be used in future
connections. It returns the previous setting for default port.</dd>
</dl>
</div>
<div class="section" id="get-defopt-set-defopt-default-connection-options-dv">
<h2><a class="toc-backref" href="#id10" name="get-defopt-set-defopt-default-connection-options-dv">2.4 get_defopt, set_defopt - default connection options [DV]</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
get_defopt()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">string, None:</th><td class="field-body">default options specification</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method returns the current default connection options specification,
or <cite>None</cite> if the environment variables should be used. Environment variables
won't be looked up.</dd>
</dl>
<p>Syntax:</p>
<pre class="literal-block">
set_defopt(options)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">options:</th><td class="field-body">new default connection options (string/None)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">string, None:</th><td class="field-body">previous default options specification</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">bad argument type, or too many arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This methods sets the default connection options value for new connections.
If <cite>None</cite> is supplied as parameter, environment variables will be used in
future connections. It returns the previous setting for default options.</dd>
</dl>
</div>
<div class="section" id="get-deftty-set-deftty-default-debug-tty-dv">
<h2><a class="toc-backref" href="#id11" name="get-deftty-set-deftty-default-debug-tty-dv">2.5 get_deftty, set_deftty - default debug tty [DV]</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
get_deftty()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">string, None:</th><td class="field-body">default debug terminal specification</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method returns the current default debug terminal specification, or
<cite>None</cite> if the environment variables should be used. Environment variables
won't be looked up.</dd>
</dl>
<p>Syntax:</p>
<pre class="literal-block">
set_deftty(terminal)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">terminal:</th><td class="field-body">new default debug terminal (string/None)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">string, None:</th><td class="field-body">previous default debug terminal specification</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">bad argument type, or too many arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This methods sets the default debug terminal value for new connections. If
<cite>None</cite> is supplied as parameter, environment variables will be used in future
connections. It returns the previous setting for default terminal.</dd>
</dl>
</div>
<div class="section" id="get-defbase-set-defbase-default-database-name-dv">
<h2><a class="toc-backref" href="#id12" name="get-defbase-set-defbase-default-database-name-dv">2.6 get_defbase, set_defbase - default database name [DV]</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
get_defbase()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">string, None:</th><td class="field-body">default database name specification</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method returns the current default database name specification, or
<cite>None</cite> if the environment variables should be used. Environment variables
won't be looked up.</dd>
</dl>
<p>Syntax:</p>
<pre class="literal-block">
set_defbase(base)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">base:</th><td class="field-body">new default base name (string/None)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">string, None:</th><td class="field-body">previous default database name specification</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">bad argument type, or too many arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method sets the default database name value for new connections. If
<cite>None</cite> is supplied as parameter, environment variables will be used in
future connections. It returns the previous setting for default host.</dd>
</dl>
</div>
<div class="section" id="escape-string-escape-a-string-for-use-within-sql">
<h2><a class="toc-backref" href="#id13" name="escape-string-escape-a-string-for-use-within-sql">2.7 escape_string - escape a string for use within SQL</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
escape_string(string)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">string:</th><td class="field-body">the string that is to be escaped</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">str:</th><td class="field-body">the escaped string</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">bad argument type, or too many arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This function escapes a string for use within an SQL command.
This is useful when inserting data values as literal constants
in SQL commands. Certain characters (such as quotes and backslashes)
must be escaped to prevent them from being interpreted specially
by the SQL parser. <cite>escape_string</cite> performs this operation.</dd>
</dl>
<div class="caution">
<p class="first admonition-title">Caution!</p>
<p class="last">It is especially important to do proper escaping when
handling strings that were received from an untrustworthy source.
Otherwise there is a security risk: you are vulnerable to "SQL injection"
attacks wherein unwanted SQL commands are fed to your database.</p>
</div>
<p>Example:</p>
<pre class="literal-block">
name = raw_input("Name? ")
phone = con.query("select phone from employees"
" where name='%s'" % escape_string(name)).getresult()
</pre>
</div>
<div class="section" id="escape-bytea-escape-binary-data-for-use-within-sql-as-type-bytea">
<h2><a class="toc-backref" href="#id14" name="escape-bytea-escape-binary-data-for-use-within-sql-as-type-bytea">2.8 escape_bytea - escape binary data for use within SQL as type <cite>bytea</cite></a></h2>
<p>Syntax:</p>
<pre class="literal-block">
escape_bytea(datastring)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">datastring:</th><td class="field-body">string containing the binary data that is to be escaped</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">str:</th><td class="field-body">the escaped string</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">bad argument type, or too many arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>Escapes binary data for use within an SQL command with the type <cite>bytea</cite>.
As with <cite>escape_string</cite>, this is only used when inserting data directly
into an SQL command string.</dd>
</dl>
<p>Example:</p>
<pre class="literal-block">
picture = file('garfield.gif', 'rb').read()
con.query("update pictures set img='%s' where name='Garfield'"
% escape_bytea(picture))
</pre>
</div>
<div class="section" id="unescape-bytea-unescape-bytea-data-that-has-been-retrieved-as-text">
<h2><a class="toc-backref" href="#id15" name="unescape-bytea-unescape-bytea-data-that-has-been-retrieved-as-text">2.9 unescape_bytea -- unescape <cite>bytea</cite> data that has been retrieved as text</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
unescape_bytea(string)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">datastring:</th><td class="field-body">the <cite>bytea</cite> data string that has been retrieved as text</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">str:</th><td class="field-body">string containing the binary data</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">bad argument type, or too many arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>Converts an escaped string representation of binary data into binary
data - the reverse of <cite>escape_bytea</cite>. This is needed when retrieving
<cite>bytea</cite> data with the <cite>getresult()</cite> or <cite>dictresult()</cite> method.</dd>
</dl>
<p>Example:</p>
<pre class="literal-block">
picture = unescape_bytea(con.query(
"select img from pictures where name='Garfield'").getresult[0][0])
file('garfield.gif', 'wb').write(picture)
</pre>
</div>
<div class="section" id="module-constants">
<h2><a class="toc-backref" href="#id16" name="module-constants">2.10 Module constants</a></h2>
<p>Some constants are defined in the module dictionary.
They are intended to be used as parameters for methods calls.
You should refer to the libpq description in the PostgreSQL user manual
for more information about them. These constants are:</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name" colspan="2">version, __version__:</th></tr>
<tr><td> </td><td class="field-body">constants that give the current version.</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">INV_READ, INV_WRITE:</th></tr>
<tr><td> </td><td class="field-body">large objects access modes,
used by <cite>(pgobject.)locreate</cite> and <cite>(pglarge.)open</cite></td>
</tr>
<tr class="field"><th class="field-name" colspan="2">SEEK_SET, SEEK_CUR, SEEK_END:</th></tr>
<tr><td> </td><td class="field-body">positional flags,
used by <cite>(pglarge.)seek</cite></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="connection-objects-pgobject">
<h1><a class="toc-backref" href="#id17" name="connection-objects-pgobject">3 Connection objects: pgobject</a></h1>
<p>This object handles a connection to a PostgreSQL database. It embeds and
hides all the parameters that define this connection, thus just leaving really
significant parameters in function calls.</p>
<div class="caution">
<p class="first admonition-title">Caution!</p>
<p>Some methods give direct access to the connection socket.
<em>Do not use them unless you really know what you are doing.</em>
If you prefer disabling them,
set the -DNO_DIRECT option in the Python setup file.</p>
<p class="last"><strong>These methods are specified by the tag [DA].</strong></p>
</div>
<div class="note">
<p class="first admonition-title">Note</p>
<p>Some other methods give access to large objects
(refer to PostgreSQL user manual for more information about these).
If you want to forbid access to these from the module,
set the -DNO_LARGE option in the Python setup file.</p>
<p class="last"><strong>These methods are specified by the tag [LO].</strong></p>
</div>
<div class="section" id="query-executes-a-sql-command-string">
<h2><a class="toc-backref" href="#id18" name="query-executes-a-sql-command-string">3.1 query - executes a SQL command string</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
query(command)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">command:</th><td class="field-body">SQL command (string)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name" colspan="2">pgqueryobject, None:</th></tr>
<tr><td> </td><td class="field-body">result values</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">bad argument type, or too many arguments</td>
</tr>
<tr class="field"><th class="field-name">ValueError:</th><td class="field-body">empty SQL query or lost connection</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">pg.ProgrammingError:</th></tr>
<tr><td> </td><td class="field-body">error in query</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">pg.InternalError':</th></tr>
<tr><td> </td><td class="field-body">error during query processing</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method simply sends a SQL query to the database. If the query is
an insert statement, the return value is the OID of the newly
inserted row. If it is otherwise a query that does not return a result
(i.e., is not a some kind of SELECT statement), it returns <cite>None</cite>.
Otherwise, it returns a <cite>pgqueryobject</cite> that can be accessed via the
<cite>getresult()</cite> or <cite>dictresult()</cite> method or simply printed.</dd>
</dl>
</div>
<div class="section" id="reset-resets-the-connection">
<h2><a class="toc-backref" href="#id19" name="reset-resets-the-connection">3.2 reset - resets the connection</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
reset()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd>None</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many (any) arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method resets the current database connection.</dd>
</dl>
</div>
<div class="section" id="cancel-abandon-processing-of-current-sql-command">
<h2><a class="toc-backref" href="#id20" name="cancel-abandon-processing-of-current-sql-command">3.3 cancel - abandon processing of current SQL command</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
cancel()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd>None</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many (any) arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method requests that the server abandon processing
of the current SQL command.</dd>
</dl>
</div>
<div class="section" id="close-close-the-database-connection">
<h2><a class="toc-backref" href="#id21" name="close-close-the-database-connection">3.4 close - close the database connection</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
close()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd>None</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many (any) arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method closes the database connection. The connection will
be closed in any case when the connection is deleted but this
allows you to explicitly close it. It is mainly here to allow
the DB-SIG API wrapper to implement a close function.</dd>
</dl>
</div>
<div class="section" id="fileno-returns-the-socket-used-to-connect-to-the-database">
<h2><a class="toc-backref" href="#id22" name="fileno-returns-the-socket-used-to-connect-to-the-database">3.5 fileno - returns the socket used to connect to the database</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
fileno()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many (any) arguments</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method returns the underlying socket id used to connect
to the database. This is useful for use in select calls, etc.</dd>
</dl>
</div>
<div class="section" id="getnotify-gets-the-last-notify-from-the-server">
<h2><a class="toc-backref" href="#id23" name="getnotify-gets-the-last-notify-from-the-server">3.6 getnotify - gets the last notify from the server</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
getnotify()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">tuple, None:</th><td class="field-body">last notify from server</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many parameters</td>
</tr>
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This methods try to get a notify from the server (from the SQL statement
NOTIFY). If the server returns no notify, the methods returns None.
Otherwise, it returns a tuple (couple) <cite>(relname, pid)</cite>, where <cite>relname</cite>
is the name of the notify and <cite>pid</cite> the process id of the connection that
triggered the notify. Remember to do a listen query first otherwise
getnotify() will always return <cite>None</cite>.</dd>
</dl>
</div>
<div class="section" id="inserttable-insert-a-list-into-a-table">
<h2><a class="toc-backref" href="#id24" name="inserttable-insert-a-list-into-a-table">3.7 inserttable - insert a list into a table</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
inserttable(table, values)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">table:</th><td class="field-body">the table name (string)</td>
</tr>
<tr class="field"><th class="field-name">values:</th><td class="field-body">list of rows values (list)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd>None</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection, bad argument type, or too many arguments</td>
</tr>
<tr class="field"><th class="field-name">MemoryError:</th><td class="field-body">insert buffer could not be allocated</td>
</tr>
<tr class="field"><th class="field-name">ValueError:</th><td class="field-body">unsupported values</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method allow to <em>quickly</em> insert large blocks of data in a table:
It inserts the whole values list into the given table. Internally, it
uses the COPY command of the PostgreSQL database. The list is a list
of tuples/lists that define the values for each inserted row. The rows
values may contain string, integer, long or double (real) values.</dd>
</dl>
<div class="caution">
<p class="first admonition-title">Caution!</p>
<p class="last"><em>Be very careful</em>:
This method doesn't typecheck the fields according to the table definition;
it just look whether or not it knows how to handle such types.</p>
</div>
</div>
<div class="section" id="putline-writes-a-line-to-the-server-socket-da">
<h2><a class="toc-backref" href="#id25" name="putline-writes-a-line-to-the-server-socket-da">3.8 putline - writes a line to the server socket [DA]</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
putline(line)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">line:</th><td class="field-body">line to be written (string)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd>None</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection, bad parameter type, or too many parameters</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method allows to directly write a string to the server socket.</dd>
</dl>
</div>
<div class="section" id="getline-gets-a-line-from-server-socket-da">
<h2><a class="toc-backref" href="#id26" name="getline-gets-a-line-from-server-socket-da">3.9 getline - gets a line from server socket [DA]</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
getline()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">string:</th><td class="field-body">the line read</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection</td>
</tr>
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many parameters</td>
</tr>
<tr class="field"><th class="field-name">MemoryError:</th><td class="field-body">buffer overflow</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method allows to directly read a string from the server socket.</dd>
</dl>
</div>
<div class="section" id="endcopy-synchronizes-client-and-server-da">
<h2><a class="toc-backref" href="#id27" name="endcopy-synchronizes-client-and-server-da">3.10 endcopy - synchronizes client and server [DA]</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
endcopy()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd>None</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection</td>
</tr>
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many parameters</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>The use of direct access methods may desynchonize client and server.
This method ensure that client and server will be synchronized.</dd>
</dl>
</div>
<div class="section" id="locreate-create-a-large-object-in-the-database-lo">
<h2><a class="toc-backref" href="#id28" name="locreate-create-a-large-object-in-the-database-lo">3.11 locreate - create a large object in the database [LO]</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
locreate(mode)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">mode:</th><td class="field-body">large object create mode</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">pglarge:</th><td class="field-body">object handling the PostGreSQL large object</td>
</tr>
</tbody>
</table>
</dd>
</dl>
<p>Exceptions raised:</p>
<blockquote>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection, bad parameter type, or too many parameters</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">pg.OperationalError:</th></tr>
<tr><td> </td><td class="field-body">creation error</td>
</tr>
</tbody>
</table>
</blockquote>
<dl class="docutils">
<dt>Description:</dt>
<dd>This method creates a large object in the database. The mode can be defined
by OR-ing the constants defined in the pg module (INV_READ, INV_WRITE and
INV_ARCHIVE). Please refer to PostgreSQL user manual for a description of
the mode values.</dd>
</dl>
</div>
<div class="section" id="getlo-build-a-large-object-from-given-oid-lo">
<h2><a class="toc-backref" href="#id29" name="getlo-build-a-large-object-from-given-oid-lo">3.12 getlo - build a large object from given oid [LO]</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
getlo(oid)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">oid:</th><td class="field-body">OID of the existing large object (integer)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">pglarge:</th><td class="field-body">object handling the PostGreSQL large object</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection, bad parameter type, or too many parameters</td>
</tr>
<tr class="field"><th class="field-name">ValueError:</th><td class="field-body">bad OID value (0 is invalid_oid)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method allows to reuse a formerly created large object through the
<cite>pglarge</cite> interface, providing the user have its OID.</dd>
</dl>
</div>
<div class="section" id="loimport-import-a-file-to-a-large-object-lo">
<h2><a class="toc-backref" href="#id30" name="loimport-import-a-file-to-a-large-object-lo">3.13 loimport - import a file to a large object [LO]</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
loimport(name)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">name:</th><td class="field-body">the name of the file to be imported (string)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">pglarge:</th><td class="field-body">object handling the PostGreSQL large object</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection, bad argument type, or too many arguments</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">pg.OperationalError:</th></tr>
<tr><td> </td><td class="field-body">error during file import</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This methods allows to create large objects in a very simple way. You just
give the name of a file containing the data to be use.</dd>
</dl>
</div>
<div class="section" id="object-attributes">
<h2><a class="toc-backref" href="#id31" name="object-attributes">3.14 Object attributes</a></h2>
<p>Every <cite>pgobject</cite> defines a set of read-only attributes that describe the
connection and its status. These attributes are:</p>
<blockquote>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">host:</th><td class="field-body">the host name of the server (string)</td>
</tr>
<tr class="field"><th class="field-name">port:</th><td class="field-body">the port of the server (integer)</td>
</tr>
<tr class="field"><th class="field-name">db:</th><td class="field-body">the selected database (string)</td>
</tr>
<tr class="field"><th class="field-name">options:</th><td class="field-body">the connection options (string)</td>
</tr>
<tr class="field"><th class="field-name">tty:</th><td class="field-body">the connection debug terminal (string)</td>
</tr>
<tr class="field"><th class="field-name">user:</th><td class="field-body">user name on the database system (string)</td>
</tr>
<tr class="field"><th class="field-name">status:</th><td class="field-body">the status of the connection (integer: 1 - OK, 0 - bad)</td>
</tr>
<tr class="field"><th class="field-name">error:</th><td class="field-body">the last warning/error message from the server (string)</td>
</tr>
</tbody>
</table>
</blockquote>
</div>
</div>
<div class="section" id="the-db-wrapper-class">
<h1><a class="toc-backref" href="#id32" name="the-db-wrapper-class">4 The DB wrapper class</a></h1>
<p>The <cite>pgobject</cite> methods are wrapped in the class <cite>DB</cite>.
The preferred way to use this module is as follows:</p>
<pre class="literal-block">
import pg
db = pg.DB(...) # see below
for r in db.query( # just for example
"""SELECT foo,bar
FROM foo_bar_table
WHERE foo !~ bar"""
).dictresult():
print '%(foo)s %(bar)s' % r
</pre>
<p>The following describes the methods and variables of this class.</p>
<div class="section" id="initialization">
<h2><a class="toc-backref" href="#id33" name="initialization">4.1 Initialization</a></h2>
<p>The DB class is initialized with the same arguments as the connect
function described in section 2. It also initializes a few
internal variables. The statement <tt class="docutils literal"><span class="pre">db</span> <span class="pre">=</span> <span class="pre">DB()</span></tt> will open the
local database with the name of the user just like connect() does.</p>
</div>
<div class="section" id="pkey-return-the-primary-key-of-a-table">
<h2><a class="toc-backref" href="#id34" name="pkey-return-the-primary-key-of-a-table">4.2 pkey - return the primary key of a table</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
pkey(table)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">table:</th><td class="field-body">name of table</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">string:</th><td class="field-body">Name of the field which is the primary key of the table</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method returns the primary key of a table. Note that this raises
an exception if the table does not have a primary key.</dd>
</dl>
</div>
<div class="section" id="get-databases-get-list-of-databases-in-the-system">
<h2><a class="toc-backref" href="#id35" name="get-databases-get-list-of-databases-in-the-system">4.3 get_databases - get list of databases in the system</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
get_databases()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">list:</th><td class="field-body">all databases in the system</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>Although you can do this with a simple select, it is added here for
convenience.</dd>
</dl>
</div>
<div class="section" id="get-relations-get-list-of-relations-in-connected-database">
<h2><a class="toc-backref" href="#id36" name="get-relations-get-list-of-relations-in-connected-database">4.4 get_relations - get list of relations in connected database</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
get_relations(kinds)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">kinds:</th><td class="field-body">a string or sequence of type letters</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>The type letters are <cite>r</cite> = ordinary table, <cite>i</cite> = index, <cite>S</cite> = sequence,
<cite>v</cite> = view, <cite>c</cite> = composite type, <cite>s</cite> = special, <cite>t</cite> = TOAST table.
If <cite>kinds</cite> is None or an empty string, all relations are returned (this is
also the default). Although you can do this with a simple select, it is
added here for convenience.</dd>
</dl>
</div>
<div class="section" id="get-tables-get-list-of-tables-in-connected-database">
<h2><a class="toc-backref" href="#id37" name="get-tables-get-list-of-tables-in-connected-database">4.5 get_tables - get list of tables in connected database</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
get_tables()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Returns:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">list:</th><td class="field-body">all tables in connected database</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>Although you can do this with a simple select, it is added here for
convenience.</dd>
</dl>
</div>
<div class="section" id="get-attnames-get-the-attribute-names-of-a-table">
<h2><a class="toc-backref" href="#id38" name="get-attnames-get-the-attribute-names-of-a-table">4.6 get_attnames - get the attribute names of a table</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
get_attnames(table)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">table:</th><td class="field-body">name of table</td>
</tr>
</tbody>
</table>
</dd>
<dt>Returns:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">dictionary:</th><td class="field-body">The keys are the attribute names,
the values are the type names of the attributes.</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>Given the name of a table, digs out the set of attribute names.</dd>
</dl>
</div>
<div class="section" id="get-get-a-row-from-a-database-table-or-view">
<h2><a class="toc-backref" href="#id39" name="get-get-a-row-from-a-database-table-or-view">4.7 get - get a row from a database table or view</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
get(table, arg, [keyname])
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">table:</th><td class="field-body">name of table or view</td>
</tr>
<tr class="field"><th class="field-name">arg:</th><td class="field-body">either a dictionary or the value to be looked up</td>
</tr>
<tr class="field"><th class="field-name">keyname:</th><td class="field-body">name of field to use as key (optional)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">dictionary:</th><td class="field-body">The keys are the attribute names,
the values are the row values.</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method is the basic mechanism to get a single row. It assumes
that the key specifies a unique row. If keyname is not specified
then the primary key for the table is used. If <cite>arg</cite> is a dictionary
then the value for the key is taken from it and it is modified to
include the new values, replacing existing values where necessary.
The OID is also put into the dictionary, but in order to allow the
caller to work with multiple tables, it is munged as <cite>oid(schema.table)</cite>.</dd>
</dl>
</div>
<div class="section" id="insert-insert-a-row-into-a-database-table">
<h2><a class="toc-backref" href="#id40" name="insert-insert-a-row-into-a-database-table">4.8 insert - insert a row into a database table</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
insert(table, a)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">table:</th><td class="field-body">name of table</td>
</tr>
<tr class="field"><th class="field-name">a:</th><td class="field-body">a dictionary of values</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">integer:</th><td class="field-body">the OID of the newly inserted row</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd><p class="first">This method inserts values into the table specified filling in the
values from the dictionary. It then reloads the dictionary with the
values from the database. This causes the dictionary to be updated
with values that are modified by rules, triggers, etc.</p>
<p class="last">Due to the way that this function works you will find inserts taking
longer and longer as your table gets bigger. To overcome this problem
simply add an index onto the OID of any table that you think may get
large over time. You may also consider using the inserttable() method
described in section 3.</p>
</dd>
</dl>
</div>
<div class="section" id="update-update-a-row-in-a-database-table">
<h2><a class="toc-backref" href="#id41" name="update-update-a-row-in-a-database-table">4.9 update - update a row in a database table</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
update(table, a)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">table:</th><td class="field-body">name of table</td>
</tr>
<tr class="field"><th class="field-name">a:</th><td class="field-body">a dictionary of values</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">dictionary:</th><td class="field-body">the new row</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>Similar to insert but updates an existing row. The update is based
on the OID value as munged by get. The array returned is the
one sent modified to reflect any changes caused by the update due
to triggers, rules, defaults, etc.</dd>
</dl>
</div>
<div class="section" id="clear-clears-row-values-in-memory">
<h2><a class="toc-backref" href="#id42" name="clear-clears-row-values-in-memory">4.10 clear - clears row values in memory</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
clear(table, [a])
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">table:</th><td class="field-body">name of table</td>
</tr>
<tr class="field"><th class="field-name">a:</th><td class="field-body">a dictionary of values</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">dictionary:</th><td class="field-body">an empty row</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method clears all the attributes to values determined by the types.
Numeric types are set to 0, Booleans are set to 'f', dates are set
to 'now()' and everything else is set to the empty string.
If the array argument is present, it is used as the array and any entries
matching attribute names are cleared with everything else left unchanged.</dd>
</dl>
</div>
<div class="section" id="delete-delete-a-row-from-a-database-table">
<h2><a class="toc-backref" href="#id43" name="delete-delete-a-row-from-a-database-table">4.11 delete - delete a row from a database table</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
delete(table, [a])
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">table:</th><td class="field-body">name of table</td>
</tr>
<tr class="field"><th class="field-name">a:</th><td class="field-body">a dictionary of values</td>
</tr>
</tbody>
</table>
</dd>
<dt>Returns:</dt>
<dd>None</dd>
<dt>Description:</dt>
<dd>This method deletes the row from a table. It deletes based on the OID
as munged as described above.</dd>
</dl>
</div>
<div class="section" id="id1">
<h2><a class="toc-backref" href="#id44" name="id1">4.12 escape_string - escape a string for use within SQL</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
escape_string(string)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">string:</th><td class="field-body">the string that is to be escaped</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">str:</th><td class="field-body">the escaped string</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>See the module function with the same name.</dd>
</dl>
</div>
<div class="section" id="id2">
<h2><a class="toc-backref" href="#id45" name="id2">4.13 escape_bytea - escape binary data for use within SQL as type <cite>bytea</cite></a></h2>
<p>Syntax:</p>
<pre class="literal-block">
escape_bytea(datastring)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">datastring:</th><td class="field-body">string containing the binary data that is to be escaped</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">str:</th><td class="field-body">the escaped string</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>See the module function with the same name.</dd>
</dl>
</div>
<div class="section" id="id3">
<h2><a class="toc-backref" href="#id46" name="id3">4.14 unescape_bytea -- unescape <cite>bytea</cite> data that has been retrieved as text</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
unescape_bytea(string)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">datastring:</th><td class="field-body">the <cite>bytea</cite> data string that has been retrieved as text</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">str:</th><td class="field-body">string containing the binary data</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>See the module function with the same name.</dd>
</dl>
</div>
</div>
<div class="section" id="pgqueryobject-methods">
<h1><a class="toc-backref" href="#id47" name="pgqueryobject-methods">5 pgqueryobject methods</a></h1>
<div class="section" id="getresult-get-query-values-as-list-of-tuples">
<h2><a class="toc-backref" href="#id48" name="getresult-get-query-values-as-list-of-tuples">5.1 getresult - get query values as list of tuples</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
getresult()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">list:</th><td class="field-body">result values as a list of tuples</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many parameters</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">pg.InternalError:</th></tr>
<tr><td> </td><td class="field-body">invalid previous result</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method returns the list of the values returned by the query.
More information about this result may be accessed using listfields(),
fieldname() and fieldnum() methods.</dd>
</dl>
</div>
<div class="section" id="dictresult-get-query-values-as-list-of-dictionaries">
<h2><a class="toc-backref" href="#id49" name="dictresult-get-query-values-as-list-of-dictionaries">5.2 dictresult - get query values as list of dictionaries</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
dictresult()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">list:</th><td class="field-body">result values as a list of dictionaries</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many parameters</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">pg.InternalError:</th></tr>
<tr><td> </td><td class="field-body">invalid previous result</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method returns the list of the values returned by the query
with each tuple returned as a dictionary with the field names
used as the dictionary index.</dd>
</dl>
</div>
<div class="section" id="listfields-lists-fields-names-of-previous-query-result">
<h2><a class="toc-backref" href="#id50" name="listfields-lists-fields-names-of-previous-query-result">5.3 listfields - lists fields names of previous query result</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
listfields()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">list:</th><td class="field-body">field names</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many parameters</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">pg.InternalError:</th></tr>
<tr><td> </td><td class="field-body">invalid previous result, or lost connection</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method returns the list of names of the fields defined for the
query result. The fields are in the same order as the result values.</dd>
</dl>
</div>
<div class="section" id="fieldname-fieldnum-field-name-number-conversion">
<h2><a class="toc-backref" href="#id51" name="fieldname-fieldnum-field-name-number-conversion">5.4 fieldname, fieldnum - field name/number conversion</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
fieldname(i)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">i:</th><td class="field-body">field number (integer)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">string:</th><td class="field-body">field name</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection, bad parameter type, or too many parameters</td>
</tr>
<tr class="field"><th class="field-name">ValueError:</th><td class="field-body">invalid field number</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">pg.InternalError:</th></tr>
<tr><td> </td><td class="field-body">invalid previous result, or lost connection</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method allows to find a field name from its rank number. It can be
useful for displaying a result. The fields are in the same order as the
result values.</dd>
</dl>
<p>Syntax:</p>
<pre class="literal-block">
fieldnum(name)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">name:</th><td class="field-body">field name (string)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">integer:</th><td class="field-body">field number</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection, bad parameter type, or too many parameters</td>
</tr>
<tr class="field"><th class="field-name">ValueError:</th><td class="field-body">unknown field name</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">pg.InternalError:</th></tr>
<tr><td> </td><td class="field-body">invalid previous result, or lost connection</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method returns a field number from its name. It can be used to
build a function that converts result list strings to their correct
type, using a hardcoded table definition. The number returned is the
field rank in the result values list.</dd>
</dl>
</div>
<div class="section" id="ntuples-return-number-of-tuples-in-query-object">
<h2><a class="toc-backref" href="#id52" name="ntuples-return-number-of-tuples-in-query-object">5.5 ntuples - return number of tuples in query object</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
ntuples()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">integer:</th><td class="field-body">number of tuples in <cite>pgqueryobject</cite></td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">Too many arguments.</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method returns the number of tuples found in a query.</dd>
</dl>
</div>
</div>
<div class="section" id="large-objects-pglarge">
<h1><a class="toc-backref" href="#id53" name="large-objects-pglarge">6 Large objects: pglarge</a></h1>
<p>This object handles all the request concerning a PostgreSQL large object. It
embeds and hides all the "recurrent" variables (object OID and connection),
exactly in the same way <cite>pgobjects</cite> do, thus only keeping significant
parameters in function calls. It keeps a reference to the <cite>pgobject</cite> used for
its creation, sending requests though with its parameters. Any modification but
dereferencing the <cite>pgobject</cite> will thus affect the <cite>pglarge</cite> object.
Dereferencing the initial <cite>pgobject</cite> is not a problem since Python won't
deallocate it before the <cite>pglarge</cite> object dereference it.
All functions return a generic error message on call error, whatever the
exact error was. The <cite>error</cite> attribute of the object allow to get the exact
error message.</p>
<p>See also the PostgreSQL programmer's guide for more information about the
large object interface.</p>
<div class="section" id="open-opens-a-large-object">
<h2><a class="toc-backref" href="#id54" name="open-opens-a-large-object">6.1 open - opens a large object</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
open(mode)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">mode:</th><td class="field-body">open mode definition (integer)</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd>None</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection, bad parameter type, or too many parameters</td>
</tr>
<tr class="field"><th class="field-name">IOError:</th><td class="field-body">already opened object, or open error</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method opens a large object for reading/writing, in the same way than
the Unix open() function. The mode value can be obtained by OR-ing the
constants defined in the pgmodule (INV_READ, INV_WRITE).</dd>
</dl>
</div>
<div class="section" id="close-closes-a-large-object">
<h2><a class="toc-backref" href="#id55" name="close-closes-a-large-object">6.2 close - closes a large object</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
close()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd>None</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection</td>
</tr>
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many parameters</td>
</tr>
<tr class="field"><th class="field-name">IOError:</th><td class="field-body">object is not opened, or close error</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method closes a previously opened large object, in the same way than
the Unix close() function.</dd>
</dl>
</div>
<div class="section" id="read-write-tell-seek-unlink-file-like-large-object-handling">
<h2><a class="toc-backref" href="#id56" name="read-write-tell-seek-unlink-file-like-large-object-handling">6.3 read, write, tell, seek, unlink - file like large object handling</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
read(size)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">size:</th><td class="field-body">maximal size of the buffer to be read</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">sized string:</th><td class="field-body">the read buffer</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection, invalid object,
bad parameter type, or too many parameters</td>
</tr>
<tr class="field"><th class="field-name">ValueError:</th><td class="field-body">if <cite>size</cite> is negative</td>
</tr>
<tr class="field"><th class="field-name">IOError:</th><td class="field-body">object is not opened, or read error</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This function allows to read data from a large object, starting at current
position.</dd>
</dl>
<p>Syntax:</p>
<pre class="literal-block">
write(string)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>(sized) string - buffer to be written</dd>
<dt>Return type:</dt>
<dd>None</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection, bad parameter type, or too many parameters</td>
</tr>
<tr class="field"><th class="field-name">IOError:</th><td class="field-body">object is not opened, or write error</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This function allows to write data to a large object, starting at current
position.</dd>
</dl>
<p>Syntax:</p>
<pre class="literal-block">
seek(offset, whence)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">offset:</th><td class="field-body">position offset</td>
</tr>
<tr class="field"><th class="field-name">whence:</th><td class="field-body">positional parameter</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">integer:</th><td class="field-body">new position in object</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">binvalid connection or invalid object,
bad parameter type, or too many parameters</td>
</tr>
<tr class="field"><th class="field-name">IOError:</th><td class="field-body">object is not opened, or seek error</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method allows to move the position cursor in the large object. The
whence parameter can be obtained by OR-ing the constants defined in the
<cite>pg</cite> module (<cite>SEEK_SET</cite>, <cite>SEEK_CUR</cite>, <cite>SEEK_END</cite>).</dd>
</dl>
<p>Syntax:</p>
<pre class="literal-block">
tell()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">integer:</th><td class="field-body">current position in large object</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection or invalid object</td>
</tr>
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many parameters</td>
</tr>
<tr class="field"><th class="field-name">IOError:</th><td class="field-body">object is not opened, or seek error</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This method allows to get the current position in the large object.</dd>
</dl>
<p>Syntax:</p>
<pre class="literal-block">
unlink()
</pre>
<dl class="docutils">
<dt>Parameter:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd>None</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection or invalid object</td>
</tr>
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many parameters</td>
</tr>
<tr class="field"><th class="field-name">IOError:</th><td class="field-body">object is not closed, or unlink error</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This methods unlinks (deletes) the PostgreSQL large object.</dd>
</dl>
</div>
<div class="section" id="size-gives-the-large-object-size">
<h2><a class="toc-backref" href="#id57" name="size-gives-the-large-object-size">6.4 size - gives the large object size</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
size()
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd>None</dd>
<dt>Return type:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">integer:</th><td class="field-body">the large object size</td>
</tr>
</tbody>
</table>
</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection or invalid object</td>
</tr>
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">too many parameters</td>
</tr>
<tr class="field"><th class="field-name">IOError:</th><td class="field-body">object is not opened, or seek/tell error</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This (composite) method allows to get the size of a large object. It was
implemented because this function is very useful for a web interfaced
database. Currently, the large object needs to be opened first.</dd>
</dl>
</div>
<div class="section" id="export-saves-a-large-object-to-a-file">
<h2><a class="toc-backref" href="#id58" name="export-saves-a-large-object-to-a-file">6.5 export - saves a large object to a file</a></h2>
<p>Syntax:</p>
<pre class="literal-block">
export(name)
</pre>
<dl class="docutils">
<dt>Parameters:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">name:</th><td class="field-body">file to be created</td>
</tr>
</tbody>
</table>
</dd>
<dt>Return type:</dt>
<dd>None</dd>
<dt>Exceptions raised:</dt>
<dd><table class="first last docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">TypeError:</th><td class="field-body">invalid connection or invalid object,
bad parameter type, or too many parameters</td>
</tr>
<tr class="field"><th class="field-name">IOError:</th><td class="field-body">object is not closed, or export error</td>
</tr>
</tbody>
</table>
</dd>
<dt>Description:</dt>
<dd>This methods allows to dump the content of a large object in a very simple
way. The exported file is created on the host of the program, not the
server host.</dd>
</dl>
</div>
<div class="section" id="id4">
<h2><a class="toc-backref" href="#id59" name="id4">6.6 Object attributes</a></h2>
<p><cite>pglarge</cite> objects define a read-only set of attributes that allow to get
some information about it. These attributes are:</p>
<blockquote>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">oid:</th><td class="field-body">the OID associated with the object</td>
</tr>
<tr class="field"><th class="field-name">pgcnx:</th><td class="field-body">the <cite>pgobject</cite> associated with the object</td>
</tr>
<tr class="field"><th class="field-name">error:</th><td class="field-body">the last warning/error message of the connection</td>
</tr>
</tbody>
</table>
</blockquote>
<div class="caution">
<p class="first admonition-title">Caution!</p>
<p class="last"><em>Be careful</em>:
In multithreaded environments, <cite>error</cite> may be modified by another thread
using the same pgobject. Remember these object are shared, not duplicated.
You should provide some locking to be able if you want to check this.
The <cite>oid</cite> attribute is very interesting because it allow you reuse the OID
later, creating the <cite>pglarge</cite> object with a <cite>pgobject</cite> getlo() method call.</p>
</div>
</div>
</div>
</div>
</body>
</html>
|