]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tdb.tex
v2.2.1 TAGGED copy ported over to main trunk of CVS
[wxWidgets.git] / docs / latex / wx / tdb.tex
CommitLineData
a660d684
KB
1\section{Database classes overview}\label{odbcoverview}
2
3Classes: \helpref{wxDatabase}{wxdatabase}, \helpref{wxRecordSet}{wxrecordset}, \helpref{wxQueryCol}{wxquerycol},
4\rtfsp\helpref{wxQueryField}{wxqueryfield}
5
9a05fd8d
JS
6\normalboxd{Note that more sophisticated ODBC classes are provided by the Remstar
7database classes: please see the separate HTML and Word documentation.}
a660d684
KB
8
9wxWindows provides a set of classes for accessing a subset of Microsoft's ODBC (Open Database Connectivity)
10product. Currently, this wrapper is available under MS Windows only, although
11ODBC may appear on other platforms, and a generic or product-specific SQL emulator for the ODBC
12classes may be provided in wxWindows at a later date.
13
14ODBC presents a unified API (Application Programmer's Interface) to a
15wide variety of databases, by interfacing indirectly to each database or
16file via an ODBC driver. The language for most of the database
17operations is SQL, so you need to learn a small amount of SQL as well as
18the wxWindows ODBC wrapper API. Even though the databases may not be
19SQL-based, the ODBC drivers translate SQL into appropriate operations
f6bcfd97 20for the database or file: even text files have rudimentary ODBC support,
a660d684
KB
21along with dBASE, Access, Excel and other file formats.
22
23The run-time files for ODBC are bundled with many existing database
24packages, including MS Office. The required header files, sql.h and
25sqlext.h, are bundled with several compilers including MS VC++ and
26Watcom C++. The only other way to obtain these header files is from the
27ODBC SDK, which is only available with the MS Developer Network CD-ROMs
28-- at great expense. If you have odbc.dll, you can make the required
29import library odbc.lib using the tool `implib'. You need to have odbc.lib
30in your compiler library path.
31
32The minimum you need to distribute with your application is odbc.dll, which must
33go in the Windows system directory. For the application to function correctly,
34ODBC drivers must be installed on the user's machine. If you do not use the database
35classes, odbc.dll will be loaded but not called (so ODBC does not need to be
36setup fully if no ODBC calls will be made).
37
38A sample is distributed with wxWindows in {\tt samples/odbc}. You will need to install
39the sample dbf file as a data source using the ODBC setup utility, available from
40the control panel if ODBC has been fully installed.
41
42\subsection{Procedures for writing an ODBC application}
43
44You first need to create a wxDatabase object. If you want to get information
45from the ODBC manager instead of from a particular database (for example
46using \helpref{wxRecordSet::GetDataSources}{wxrecordsetgetdatasources}), then you
47do not need to call \helpref{wxDatabase::Open}{wxdatabaseopen}.
48If you do wish to connect to a datasource, then call wxDatabase::Open.
49You can reuse your wxDatabase object, calling wxDatabase::Close and wxDatabase::Open
50multiple times.
51
52Then, create a wxRecordSet object for retrieving or sending information.
53For ODBC manager information retrieval, you can create it as a dynaset (retrieve the
54information as needed) or a snapshot (get all the data at once).
55If you are going to call \helpref{wxRecordSet::ExecuteSQL}{wxrecordsetexecutesql}, you need to create it as a snapshot.
56Dynaset mode is not yet implemented for user data.
57
58Having called a function such as wxRecordSet::ExecuteSQL or
59wxRecordSet::GetDataSources, you may have a number of records
60associated with the recordset, if appropriate to the operation. You can
61now retrieve information such as the number of records retrieved and the
62actual data itself. Use \helpref{wxRecordSet::GetFieldData}{wxrecordsetgetfielddata} or
63\helpref{wxRecordSet::GetFieldDataPtr}{wxrecordsetgetfielddataptr} to get the data or a pointer to it, passing
64a column index or name. The data returned will be for the current
65record. To move around the records, use \helpref{wxRecordSet::MoveNext}{wxrecordsetmovenext},
66\rtfsp\helpref{wxRecordSet::MovePrev}{wxrecordsetmoveprev} and associated functions.
67
68You can use the same recordset for multiple operations, or delete
69the recordset and create a new one.
70
71Note that when you delete a wxDatabase, any associated recordsets
72also get deleted, so beware of holding onto invalid pointers.
73
74\subsection{wxDatabase overview}\label{wxdatabaseoverview}
75
a660d684
KB
76Class: \helpref{wxDatabase}{wxdatabase}
77
78Every database object represents an ODBC connection. To do anything useful
79with a database object you need to bind a wxRecordSet object to it. All you
80can do with wxDatabase is opening/closing connections and getting some info
81about it (users, passwords, and so on).
82
fa482912 83\wxheading{See also}
a660d684 84
fa482912
JS
85\helpref{Database classes overview}{odbcoverview}
86
87\subsection{wxQueryCol overview}\label{wxquerycoloverview}
a660d684
KB
88
89Class: \helpref{wxQueryCol}{wxquerycol}
90
91Every data column is represented by an instance of this class.
92It contains the name and type of a column and a list of wxQueryFields where
93the real data is stored. The links to user-defined variables are stored
94here, as well.
95
fa482912 96\wxheading{See also}
a660d684 97
fa482912
JS
98\helpref{Database classes overview}{odbcoverview}
99
100\subsection{wxQueryField overview}\label{wxqueryfieldoverview}
a660d684
KB
101
102Class: \helpref{wxQueryField}{wxqueryfield}
103
104As every data column is represented by an instance of the class wxQueryCol,
105every data item of a specific column is represented by an instance of
106wxQueryField. Each column contains a list of wxQueryFields. If wxRecordSet is
107of the type wxOPEN\_TYPE\_DYNASET, there will be only one field for each column,
108which will be updated every time you call functions like wxRecordSet::Move
109or wxRecordSet::GoTo. If wxRecordSet is of the type wxOPEN\_TYPE\_SNAPSHOT,
110all data returned by an ODBC function will be loaded at once and the number
111of wxQueryField instances for each column will depend on the number of records.
112
fa482912 113\wxheading{See also}
a660d684 114
fa482912
JS
115\helpref{Database classes overview}{odbcoverview}
116
117\subsection{wxRecordSet overview}\label{wxrecordsetoverview}
a660d684
KB
118
119Class: \helpref{wxRecordSet}{wxrecordset}
120
121Each wxRecordSet represents a database query. You can make multiple queries
122at a time by using multiple wxRecordSets with a wxDatabase or you can make
123your queries in sequential order using the same wxRecordSet.
124
fa482912
JS
125\wxheading{See also}
126
127\helpref{Database classes overview}{odbcoverview}
a660d684 128
fa482912 129\subsection{ODBC SQL data types}\label{sqltypes}
a660d684
KB
130
131These are the data types supported in ODBC SQL. Note that there are other, extended level conformance
132types, not currently supported in wxWindows.
133
134\begin{twocollist}\itemsep=0pt
135\twocolitem{CHAR(n)}{A character string of fixed length {\it n}.}
136\twocolitem{VARCHAR(n)}{A varying length character string of maximum length {\it n}.}
137\twocolitem{LONG VARCHAR(n)}{A varying length character string: equivalent to VARCHAR for the purposes
138of ODBC.}
139\twocolitem{DECIMAL(p, s)}{An exact numeric of precision {\it p} and scale {\it s}.}
140\twocolitem{NUMERIC(p, s)}{Same as DECIMAL.}
141\twocolitem{SMALLINT}{A 2 byte integer.}
142\twocolitem{INTEGER}{A 4 byte integer.}
143\twocolitem{REAL}{A 4 byte floating point number.}
144\twocolitem{FLOAT}{An 8 byte floating point number.}
145\twocolitem{DOUBLE PRECISION}{Same as FLOAT.}
146\end{twocollist}
147
148These data types correspond to the following ODBC identifiers:
149
150\begin{twocollist}\itemsep=0pt
151\twocolitem{SQL\_CHAR}{A character string of fixed length.}
152\twocolitem{SQL\_VARCHAR}{A varying length character string.}
153\twocolitem{SQL\_DECIMAL}{An exact numeric.}
154\twocolitem{SQL\_NUMERIC}{Same as SQL\_DECIMAL.}
155\twocolitem{SQL\_SMALLINT}{A 2 byte integer.}
156\twocolitem{SQL\_INTEGER}{A 4 byte integer.}
157\twocolitem{SQL\_REAL}{A 4 byte floating point number.}
158\twocolitem{SQL\_FLOAT}{An 8 byte floating point number.}
159\twocolitem{SQL\_DOUBLE}{Same as SQL\_FLOAT.}
160\end{twocollist}
161
fa482912
JS
162\wxheading{See also}
163
164\helpref{Database classes overview}{odbcoverview}
a660d684 165
fa482912 166\subsection{A selection of SQL commands}\label{sqlcommands}
a660d684
KB
167
168The following is a very brief description of some common SQL commands, with
169examples.
170
fa482912
JS
171\wxheading{See also}
172
173\helpref{Database classes overview}{odbcoverview}
174
a660d684
KB
175\subsubsection{Create}
176
177Creates a table.
178
179Example:
180
181\begin{verbatim}
182CREATE TABLE Book
183 (BookNumber INTEGER PRIMARY KEY
184 , CategoryCode CHAR(2) DEFAULT 'RO' NOT NULL
185 , Title VARCHAR(100) UNIQUE
186 , NumberOfPages SMALLINT
187 , RetailPriceAmount NUMERIC(5,2)
188 )
189\end{verbatim}
190
191\subsubsection{Insert}
192
193Inserts records into a table.
194
195Example:
196
197\begin{verbatim}
198INSERT INTO Book
199 (BookNumber, CategoryCode, Title)
200 VALUES(5, 'HR', 'The Lark Ascending')
201\end{verbatim}
202
203\subsubsection{Select}
204
205The Select operation retrieves rows and columns from a table. The criteria
206for selection and the columns returned may be specified.
207
208Examples:
209
210\verb$SELECT * FROM Book$
211
212Selects all rows and columns from table Book.
213
214\verb$SELECT Title, RetailPriceAmount FROM Book WHERE RetailPriceAmount > 20.0$
215
216Selects columns Title and RetailPriceAmount from table Book, returning only
217the rows that match the WHERE clause.
218
219\verb$SELECT * FROM Book WHERE CatCode = 'LL' OR CatCode = 'RR'$
220
221Selects all columns from table Book, returning only
222the rows that match the WHERE clause.
223
224\verb$SELECT * FROM Book WHERE CatCode IS NULL$
225
226Selects all columns from table Book, returning only rows where the CatCode column
227is NULL.
228
229\verb$SELECT * FROM Book ORDER BY Title$
230
231Selects all columns from table Book, ordering by Title, in ascending order. To specify
232descending order, add DESC after the ORDER BY Title clause.
233
234\verb$SELECT Title FROM Book WHERE RetailPriceAmount >= 20.0 AND RetailPriceAmount <= 35.0$
235
236Selects records where RetailPriceAmount conforms to the WHERE expression.
237
238\subsubsection{Update}
239
240Updates records in a table.
241
242Example:
243
244\verb$UPDATE Incident SET X = 123 WHERE ASSET = 'BD34'$
245
246This example sets a field in column `X' to the number 123, for the record
247where the column ASSET has the value `BD34'.
248