Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 123 lines (90 sloc) 4.562 kb
c3f6b46 Michael Kleehammer Import from Subversion 2.0.63; reworked versioning
authored
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <title>pyodbc</title>
5 <link rel="stylesheet" type="text/css" href="styles.css" />
6 </head>
7 <body>
8
9 <div id="titlebox">
10 pyodbc - A Python DB API module for ODBC
11 </div>
12
13 <div id="nav">
14 <a href="index.html">Home</a>
15 : <a href="docs.html">Documentation</a>
16 : <a href="tutorial.html">Tutorial</a>
17 : <a href="http://sourceforge.net/project/showfiles.php?group_id=162557">Download</a>
18 : <a href="http://sourceforge.net/news/?group_id=162557">News</a>
19 : <a href="http://github.com/mkleehammer/pyodbc">Source Code (github)</a>
20 : <a href="http://sourceforge.net/tracker/?group_id=162557&amp;atid=824254">Bug&nbsp;Tracker</a>
21 : <a href="http://sourceforge.net/projects/pyodbc">SourceForge</a>
22 : <a href="http://www.python.org/peps/pep-0249.html">Python DB API</a>
23 </div>
24
25 <div id="contents">
26
27 <h1>Introduction</h1>
28
29 <p>This document is high-level introduction to using pyodbc and does not cover all its
30 details. pyodbc implements the
31 <a href="http://www.python.org/peps/pep-0249.html">Python Database API Specification
32 v2.0</a>, so you should read this specification for more information.</p>
33
34 <p>If you haven't installed pyodbc,
35 <a href="http://sourceforge.net/project/showfiles.php?group_id=162557">download</a> and
36 install it.</p>
37
38 <h1>Connecting</h1>
39
40 <p>First, you must import pyodbc. If you get errors here, make sure you have pyodbc installed.</p>
41
42 <pre>
43 import pyodbc</pre>
44
45 <p>Next, create a connection by passing an ODBC connection string to the connect method. This
46 step causes ODBC to load the database driver (the SQL Server driver in this example) and
47 connect to the database.</p>
48
49 <pre>
50 cnxn = pyodbc.connect('DSN=northwind')</pre>
51
52 <p>The ODBC connection string format is specified by ODBC in the
53 <a
54 href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcsqldriverconnect.asp">SQLDriverConnect</a>
55 documentation. Unfortunately, this is for C programmers, but the comments section discussion
56 of the connection string format is useful.</p>
57
58 <p>ODBC itself recognizes the following keywords in a connection string:</p>
59
60 <dl>
61 <dt>DRIVER</dt>
62 <dd>The ODBC driver to use. Make sure the driver you want to use is installed.</dd>
63
64 <dt>DSN</dt>
65 <dd>The name of a DSN configured in the control panel Data Sources applet. This allows
66 database information to be specified in an application-independent manner and location.</dd>
67
68 <dt>UID</dt>
69 <dd>The user name when a login is required.</dd>
70
71 <dt>PWD</dt>
72 <dd>The password when a login is required. DSNs cannot contain passwords, so you may need
73 this even when using the DSN keyword.</dt>
74
75 <dt>FILEDSN</dt>
76 <dd>The name of a .dsn file, used when the DSN information is stored in a file.</dd>
77 <dl>
78
79 <p>Each database driver may support additional keywords. For example, the SQL Server driver
80 allows you to specify the machine SQL Server is running on using the SERVER keyword and the
81 database to connect to using the DATABASE keyword. These two allow you to connect to the
82 database without registering a DSN in the control panel. (The ODBC section of the SQL Native
83 Client <a href="http://msdn2.microsoft.com/en-us/library/ms130822(SQL.90).aspx#">Using
84 Connection String Keywords with SQL Native Client</a> documentation may be useful when using SQL Server.)</p>
85
86 <pre>
87 cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=testdb;UID=user;PWD=password')</pre>
88
89 <h1>Create an Example Table</h1>
90
91 <p>Next, we'll create a table and populate it with some example values. First, make a cursor
92 and execute the necessary SQL. (The SQL may need to be modified for your database,
93 particularly the type names like 'int'. I'm testing this using SQL Server.) Finally, commit
94 the changes.</p>
95
96 <pre>
97 cursor = cnxn.cursor()
98 cursor.execute("create table tmp(a int, b varchar(30))")
99 cnxn.commit()</pre>
100
101 <p>First, notice that the commit is applied to the connection, not the cursor. Changes from
102 all cursors attached to the same connection will be commited. Also note that the commit
103 is <i>required</i>. If you do not commit, the changes will be rolled back when the connection
104 is closed.</p>
105
106 <h1>Insert Some Values</h1>
107
108
109
110 <h1>Selecting Values</h1>
111
112 <p>Once you have a connection, obtain a cursor from it and execute a select statement via the
113 cursor's <code>execute</code> method:</p>
114
115 <pre>
116 cursor = cnxn.cursor()
117 cursor.execute('select a, b from tmp')
118 </pre>
119
120
121 </body>
122 </html>
Something went wrong with that request. Please try again.