]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tdb.tex
1. wxMSW seems to work (please test and send your bug reports!)
[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
20for the database or file: even text files have rudimentry ODBC support,
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
76\overview{Database classes overview}{odbcoverview}
77
78Class: \helpref{wxDatabase}{wxdatabase}
79
80Every database object represents an ODBC connection. To do anything useful
81with a database object you need to bind a wxRecordSet object to it. All you
82can do with wxDatabase is opening/closing connections and getting some info
83about it (users, passwords, and so on).
84
85\subsection{wxQueryCol overview}\label{wxquerycoloverview}
86
87\overview{Database classes overview}{odbcoverview}
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
96\subsection{wxQueryField overview}\label{wxqueryfieldoverview}
97
98\overview{Database classes overview}{odbcoverview}
99
100Class: \helpref{wxQueryField}{wxqueryfield}
101
102As every data column is represented by an instance of the class wxQueryCol,
103every data item of a specific column is represented by an instance of
104wxQueryField. Each column contains a list of wxQueryFields. If wxRecordSet is
105of the type wxOPEN\_TYPE\_DYNASET, there will be only one field for each column,
106which will be updated every time you call functions like wxRecordSet::Move
107or wxRecordSet::GoTo. If wxRecordSet is of the type wxOPEN\_TYPE\_SNAPSHOT,
108all data returned by an ODBC function will be loaded at once and the number
109of wxQueryField instances for each column will depend on the number of records.
110
111\subsection{wxRecordSet overview}\label{wxrecordsetoverview}
112
113\overview{Database classes overview}{odbcoverview}
114
115Class: \helpref{wxRecordSet}{wxrecordset}
116
117Each wxRecordSet represents a database query. You can make multiple queries
118at a time by using multiple wxRecordSets with a wxDatabase or you can make
119your queries in sequential order using the same wxRecordSet.
120
121\subsection{ODBC SQL data types}\label{sqltypes}
122
123\overview{Database classes overview}{odbcoverview}
124
125These are the data types supported in ODBC SQL. Note that there are other, extended level conformance
126types, not currently supported in wxWindows.
127
128\begin{twocollist}\itemsep=0pt
129\twocolitem{CHAR(n)}{A character string of fixed length {\it n}.}
130\twocolitem{VARCHAR(n)}{A varying length character string of maximum length {\it n}.}
131\twocolitem{LONG VARCHAR(n)}{A varying length character string: equivalent to VARCHAR for the purposes
132of ODBC.}
133\twocolitem{DECIMAL(p, s)}{An exact numeric of precision {\it p} and scale {\it s}.}
134\twocolitem{NUMERIC(p, s)}{Same as DECIMAL.}
135\twocolitem{SMALLINT}{A 2 byte integer.}
136\twocolitem{INTEGER}{A 4 byte integer.}
137\twocolitem{REAL}{A 4 byte floating point number.}
138\twocolitem{FLOAT}{An 8 byte floating point number.}
139\twocolitem{DOUBLE PRECISION}{Same as FLOAT.}
140\end{twocollist}
141
142These data types correspond to the following ODBC identifiers:
143
144\begin{twocollist}\itemsep=0pt
145\twocolitem{SQL\_CHAR}{A character string of fixed length.}
146\twocolitem{SQL\_VARCHAR}{A varying length character string.}
147\twocolitem{SQL\_DECIMAL}{An exact numeric.}
148\twocolitem{SQL\_NUMERIC}{Same as SQL\_DECIMAL.}
149\twocolitem{SQL\_SMALLINT}{A 2 byte integer.}
150\twocolitem{SQL\_INTEGER}{A 4 byte integer.}
151\twocolitem{SQL\_REAL}{A 4 byte floating point number.}
152\twocolitem{SQL\_FLOAT}{An 8 byte floating point number.}
153\twocolitem{SQL\_DOUBLE}{Same as SQL\_FLOAT.}
154\end{twocollist}
155
156\subsection{A selection of SQL commands}\label{sqlcommands}
157
158\overview{Database classes overview}{odbcoverview}
159
160The following is a very brief description of some common SQL commands, with
161examples.
162
163\subsubsection{Create}
164
165Creates a table.
166
167Example:
168
169\begin{verbatim}
170CREATE TABLE Book
171 (BookNumber INTEGER PRIMARY KEY
172 , CategoryCode CHAR(2) DEFAULT 'RO' NOT NULL
173 , Title VARCHAR(100) UNIQUE
174 , NumberOfPages SMALLINT
175 , RetailPriceAmount NUMERIC(5,2)
176 )
177\end{verbatim}
178
179\subsubsection{Insert}
180
181Inserts records into a table.
182
183Example:
184
185\begin{verbatim}
186INSERT INTO Book
187 (BookNumber, CategoryCode, Title)
188 VALUES(5, 'HR', 'The Lark Ascending')
189\end{verbatim}
190
191\subsubsection{Select}
192
193The Select operation retrieves rows and columns from a table. The criteria
194for selection and the columns returned may be specified.
195
196Examples:
197
198\verb$SELECT * FROM Book$
199
200Selects all rows and columns from table Book.
201
202\verb$SELECT Title, RetailPriceAmount FROM Book WHERE RetailPriceAmount > 20.0$
203
204Selects columns Title and RetailPriceAmount from table Book, returning only
205the rows that match the WHERE clause.
206
207\verb$SELECT * FROM Book WHERE CatCode = 'LL' OR CatCode = 'RR'$
208
209Selects all columns from table Book, returning only
210the rows that match the WHERE clause.
211
212\verb$SELECT * FROM Book WHERE CatCode IS NULL$
213
214Selects all columns from table Book, returning only rows where the CatCode column
215is NULL.
216
217\verb$SELECT * FROM Book ORDER BY Title$
218
219Selects all columns from table Book, ordering by Title, in ascending order. To specify
220descending order, add DESC after the ORDER BY Title clause.
221
222\verb$SELECT Title FROM Book WHERE RetailPriceAmount >= 20.0 AND RetailPriceAmount <= 35.0$
223
224Selects records where RetailPriceAmount conforms to the WHERE expression.
225
226\subsubsection{Update}
227
228Updates records in a table.
229
230Example:
231
232\verb$UPDATE Incident SET X = 123 WHERE ASSET = 'BD34'$
233
234This example sets a field in column `X' to the number 123, for the record
235where the column ASSET has the value `BD34'.
236
237
238