]>
Commit | Line | Data |
---|---|---|
1 | \section{Database classes overview}\label{odbcoverview} | |
2 | ||
3 | Following is a detailed overview of how to use the wxWindows ODBC classes - \helpref{wxDb}{wxdb} | |
4 | and \helpref{wxDbTable}{wxdbtable} and their associated functions. These are | |
5 | the ODBC classes donated by Remstar International, and are collectively | |
6 | referred to herein as the wxODBC classes. | |
7 | ||
8 | \subsection{wxDb/wxDbTable wxODBC Overview}\label{wxodbcoverview} | |
9 | ||
10 | Classes: \helpref{wxDb}{wxdb}, \helpref{wxDbTable}{wxdbtable} | |
11 | ||
12 | The wxODBC classes were designed for database independence. Although SQL and | |
13 | ODBC both have standards which define the minimum requirements they must | |
14 | support to be in compliance with specifications, different database vendors | |
15 | may implement things slightly differently. One example of this is that Oracle | |
16 | requires all user names for the datasources to be supplied in uppercase | |
17 | characters. In situations like this, the wxODBC classes have been written | |
18 | to make this transparent to the programmer when using functions that require | |
19 | database-specific syntax. | |
20 | ||
21 | Currently several major databases, along with other widely used databases, | |
22 | have been tested and supported through the wxODBC classes. The list of | |
23 | supported databases is certain to grow as more users start implementing | |
24 | software with these classes, but at the time of the writing of this document, | |
25 | users have successfully used the classes with the following datasources: | |
26 | ||
27 | \begin{itemize}\itemsep=0pt | |
28 | \item Oracle (v7, v8, v8i) | |
29 | \item Sybase (ASA and ASE) | |
30 | \item MS SQL Server (v7 - minimal testing) | |
31 | \item MS Access (97 and 2000) | |
32 | \item MySQL | |
33 | \item DBase (IV, V)** | |
34 | \item PostgreSQL | |
35 | \item INFORMIX | |
36 | \item VIRTUOSO | |
37 | \item DB2 | |
38 | \item Interbase | |
39 | \item Pervasive SQL | |
40 | \end{itemize} | |
41 | ||
42 | An up-to-date list can be obtained by looking in the comments of the function | |
43 | \helpref{wxDb::Dbms}{wxdbdbms} in db.cpp, or in the enumerated type | |
44 | \helpref{wxDBMS}{wxdbenumeratedtypes} in db.h. | |
45 | ||
46 | **dBase is not truly an ODBC datasource, but there are drivers which can | |
47 | emulate much of the functionality of an ODBC connection to a dBase table. | |
48 | See the \helpref{wxODBC Known Issues}{wxodbcknownissues} section of this | |
49 | overview for details. | |
50 | ||
51 | ||
52 | \subsection{wxODBC Where To Start}\label{wxodbcwheretostart} | |
53 | ||
54 | First, if you are not familiar with SQL and ODBC, go to your local bookstore | |
55 | and pick up a good book on each. This documentation is not meant to teach | |
56 | you many details about SQL or ODBC, though you may learn some just from | |
57 | immersion in the subject. | |
58 | ||
59 | If you have worked with non-SQL/ODBC datasources before, there are some | |
60 | things you will need to un-learn. First some terminology as these phrases will | |
61 | be used heavily in this section of the manual. | |
62 | ||
63 | \begin{twocollist}\itemsep=0pt | |
64 | \twocolitem{Datasource}{(usually a database) that contains the data that will be | |
65 | accessed by the wxODBC classes.} | |
66 | \twocolitem{Data table}{The section of the datasource that contains the rows and | |
67 | columns of data.} | |
68 | \twocolitem{ODBC driver}{The middle-ware software that interprets the ODBC | |
69 | commands sent by your application and converts them to the SQL format expected | |
70 | by the target datasource.} | |
71 | \twocolitem{Datasource connection}{An open pipe between your application and | |
72 | the ODBC driver which in turn has a connection to the target datasource. | |
73 | Datasource connections can have a virtually unlimited number of wxDbTable | |
74 | instances using the same connect (dependent on the ODBC driver). A separate | |
75 | connection is not needed for each table (the exception is for isolating | |
76 | commits/rollbacks on different tables from affecting more than the desired | |
77 | table. See the class documentation on | |
78 | \helpref{wxDb::CommitTrans}{wxdbcommittrans} and | |
79 | \helpref{wxDb::RollbackTrans}{wxdbrollbacktrans}.)} | |
80 | \twocolitem{Rows}{Similar to records in old relational databases, a row is a | |
81 | collection of one instance of each column of the data table that are all | |
82 | associated with each other.} | |
83 | \twocolitem{Columns}{Individual fields associated with each row of a data | |
84 | table.} | |
85 | \twocolitem{Query}{Request from the client to the datasource asking for | |
86 | the data that matches the requirements specified in the users request. When | |
87 | a query is performed, the datasource performs the lookup of the rows with | |
88 | satisfy the query, and creates a result set.} | |
89 | \twocolitem{Result set}{The data which matches the requirements specified | |
90 | in a query sent to the datasource. Dependent on drivers, a result set | |
91 | typically remains at the datasource (no data is transmitted to the ODBC driver) | |
92 | until the client actually instructs the ODBC driver to retrieve it.} | |
93 | \twocolitem{Cursor}{A logical pointer into the result set that a query | |
94 | generates, indicating the next record that will be returned to the client | |
95 | when a request for the next record is made.} | |
96 | \twocolitem{Scrolling cursors}{Scrolling refers to the movement of cursors | |
97 | through the result set. Cursors can always scroll forward sequentially in | |
98 | the result set (FORWARD ONLY scrolling cursors). With Forward only scrolling | |
99 | cursors, once a row in the result set has been returned to the ODBC driver | |
100 | and on to the client, there is no way to have the cursor move backward in | |
101 | the result set to look at the row that is previous to the current row in | |
102 | the result set. If BACKWARD scrolling cursors are supported by both the | |
103 | ODBC driver and the datasource that are being used, then backward | |
104 | scrolling cursor functions may be used ( | |
105 | \helpref{wxDbTable::GetPrev}{wxdbtablegetprev}, | |
106 | \helpref{wxDbTable::GetFirst}{wxdbtablegetfirst}, and | |
107 | \helpref{wxDbTable::GetLast}{wxdbtablegetlast}). If the datasource or the | |
108 | ODBC driver only support forward scrolling cursors, your program and logic | |
109 | must take this in to account.} | |
110 | \twocolitem{Commit/Rollback}{Commit will physically save | |
111 | insertions/deletions/updates, while rollback basically does an undo of | |
112 | everything done against the datasource connection that has not been | |
113 | previously committed. Note that Commit and Rollbacks are done on a | |
114 | connection, not on individual tables. All tables which use a shared | |
115 | connection to the datasource are all committed/rolled back at the same | |
116 | time when a call to | |
117 | \helpref{wxDb::CommitTrans}{wxdbcommittrans} or | |
118 | \helpref{wxDb::RollbackTrans}{wxdbrollbacktrans} is made.} | |
119 | \twocolitem{Index}{Indexes are datasource-maintained lookup structures | |
120 | that allow the datasource to quickly locate data rows based on the values | |
121 | of certain columns. Without indexes, the datasource would need to do a | |
122 | sequential search of a table every time a query request is made. Proper | |
123 | unique key index construction can make datasource queries nearly instantaneous.} | |
124 | \end{twocollist} | |
125 | ||
126 | Before you are able to read data from a data table in a datasource, you must | |
127 | have a connection to the datasource. Each datasource connection may be used | |
128 | to open multiple tables all on the same connection (number of tables open are | |
129 | dependent on the driver, datasource configuration and the amount of memory on | |
130 | the client workstation). Multiple connections can be opened to the same | |
131 | datasource by the same client (number of concurrent connections is dependent | |
132 | on the driver and datasource configuration). | |
133 | ||
134 | When a query is performed, the client passes the query to the ODBC driver, | |
135 | and the driver then translates it and passes it along to the datasource. The | |
136 | database engine (in most cases - exceptions are text and dBase files) running | |
137 | on the machine hosting the database does all the work of performing the search | |
138 | for the requested data. The client simply waits for a status to come back | |
139 | through the ODBC driver from the datasource. | |
140 | ||
141 | Depending on the ODBC driver, the result set either remains "queued" on the | |
142 | database server side, or is transferred to the machine that the driver is | |
143 | queued on. The client does not receive this data. The client must request | |
144 | some or all of the result set to be returned before any data rows are | |
145 | returned to the client application. | |
146 | ||
147 | Result sets do not need to include all columns of every row matching the | |
148 | query. In fact, result sets can actually be joinings of columns from two | |
149 | or more data tables, may have derived column values, or calculated values | |
150 | returned. | |
151 | ||
152 | For each result set, a cursor is maintained (typically by the database) | |
153 | which keeps track of where in the result set the user currently is. | |
154 | Depending on the database, ODBC driver, and how you configured the | |
155 | wxWindows ODBC settings in setup.h (see \helpref{wxODBC - Compiling}{wxodbccompiling}), cursors can be | |
156 | either forward or backward scrolling. At a minimum, cursors must scroll | |
157 | forward. For example, if a query resulted in a result set with 100 rows, | |
158 | as the data is read by the client application, it will read row 1, then 2, | |
159 | then 3, etc. With forward only cursors, once the cursor has moved to | |
160 | the next row, the previous row cannot be accessed again without re-querying | |
161 | the datasource for the result set over again. Backward scrolling cursors | |
162 | allow you to request the previous row from the result set, actually | |
163 | scrolling the cursor backward. | |
164 | ||
165 | Backward scrolling cursors are not supported on all database/driver | |
166 | combinations. For this reason, forward-only cursors are the default in | |
167 | the wxODBC classes. If your datasource does support backward scrolling | |
168 | cursors and you wish to use them, make the appropriate changes in setup.h | |
169 | to enable them (see \helpref{wxODBC - Compiling}{wxodbccompiling}). For greatest portability between | |
170 | datasources, writing your program in such a way that it only requires | |
171 | forward scrolling cursors is your best bet. On the other hand, if you are | |
172 | focusing on using only datasources that support backward scrolling cursors, | |
173 | potentially large performance benefits can be gained from using them. | |
174 | ||
175 | There is a limit to the number of cursors that can be open on each connection | |
176 | to the datasource, and usually a maximum number of cursors for the datasource | |
177 | itself. This is all dependent on the database. Each connection that is | |
178 | opened (each instance of a wxDb) opens a minimum of 5 cursors on creation | |
179 | that are required for things such as updates/deletions/rollbacks/queries. | |
180 | Cursors are a limited resource, so use care in creating large numbers of | |
181 | cursors. | |
182 | ||
183 | Additional cursors can be created if necessary with the | |
184 | \helpref{wxDbTable::GetNewCursor}{wxdbtablegetnewcursor} function. One example | |
185 | use for additional cursors is to track multiple scroll points in result | |
186 | sets. By creating a new cursor, a program could request a second result set | |
187 | from the datasource while still maintaining the original cursor position in | |
188 | the first result set. | |
189 | ||
190 | Different than non-SQL/ODBC datasources, when a program performs an | |
191 | insertion, deletion, or update (or other SQL functions like altering | |
192 | tables, etc) through ODBC, the program must issue a "commit" to the | |
193 | datasource to tell the datasource that the action(s) it has been told to | |
194 | perform are to be recorded as permanent. Until a commit is performed, | |
195 | any other programs that query the datasource will not see the changes that | |
196 | have been made (although there are databases that can be configured to | |
197 | auto-commit). NOTE: With most datasources, until the commit is | |
198 | performed, any cursor that is open on that same datasource connection | |
199 | will be able to see the changes that are uncommitted. Check your | |
200 | database's documentation/configuration to verify this before relying on it | |
201 | though. | |
202 | ||
203 | A rollback is basically an UNDO command on the datasource connection. When | |
204 | a rollback is issued, the datasource will flush all commands it has been told | |
205 | to do since the last commit that was performed. | |
206 | ||
207 | NOTE: Commits/Rollbacks are done on datasource connections (wxDb instances) | |
208 | not on the wxDbTable instances. This means that if more than one table | |
209 | shares the same connection, and a commit or rollback is done on that | |
210 | connection, all pending changes for ALL tables using that connection are | |
211 | committed/rolled back. | |
212 | ||
213 | \subsection{wxODBC - Configuring your system for ODBC use}\label{wxodbcconfiguringyoursystem} | |
214 | ||
215 | Before you are able to access a datasource, you must have installed and | |
216 | configured an ODBC driver. Doing this is system specific, so it will not be | |
217 | covered in detail here. But here are a few details to get you started. | |
218 | ||
219 | Most database vendors provide at least a minimal ODBC driver with their | |
220 | database product. In practice, many of these drivers have proven to be slow | |
221 | and/or incomplete. Rumour has it that this is because the vendors do not want | |
222 | you using the ODBC interface to their products; they want you to use their | |
223 | applications to access the data. | |
224 | ||
225 | Whatever the reason, for database-intensive applications, you may want to | |
226 | consider using a third-party ODBC driver for your needs. One example of a | |
227 | third-party set of ODBC drivers that has been heavily tested and used is | |
228 | Rogue Wave's drivers. Rogue Wave has drivers available for many different | |
229 | platforms and databases. | |
230 | ||
231 | Under Microsoft Windows, install the ODBC driver you are planning to use. You | |
232 | will then use the ODBC Administrator in the Control Panel to configure an | |
233 | instance of the driver for your intended datasource. Note that with all | |
234 | flavors of NT, this configuration can be set up as a System or User DSN | |
235 | (datasource name). Configuring it as a system resource will make it | |
236 | available to all users (if you are logged in as 'administrator'), otherwise | |
237 | the datasource will only be available to the user who configured the DSN. | |
238 | ||
239 | Under Unix, iODBC is used for implementation of the ODBC API. To compile the | |
240 | wxODBC classes, you must first obtain iODBC from \urlref{http://www.iodbc.org}{www.iodbc.org} and install it. | |
241 | (Note: wxWindows currently includes a version of iODBC.) Then you must create the file "~/.odbc.ini" (or optionally create | |
242 | "/etc/odbc.ini" for access for all users on the system). This file contains | |
243 | the settings for your system/datasource. Below is an example section of a | |
244 | odbc.ini file for use with the "samples/db" sample program using MySQL: | |
245 | ||
246 | \begin{verbatim} | |
247 | [contacts] | |
248 | Trace = Off | |
249 | TraceFile= stderr | |
250 | Driver = /usr/local/lib/libmyodbc.so | |
251 | DSN = contacts | |
252 | SERVER = 192.168.1.13 | |
253 | USER = qet | |
254 | PASSWORD = | |
255 | PORT = 3306 | |
256 | \end{verbatim} | |
257 | ||
258 | \subsection{wxODBC - Compiling}\label{wxodbccompiling} | |
259 | ||
260 | The wxWindows setup.h file has several settings in it pertaining to compiling | |
261 | the wxODBC classes. | |
262 | ||
263 | \begin{twocollist}\itemsep=0pt | |
264 | \twocolitem{wxUSE\_ODBC}{This must be set to 1 in order for the compiler to | |
265 | compile the wxODBC classes. Without setting this to 1, there will be no | |
266 | access to any of the wxODBC classes. The default is 0.} | |
267 | \twocolitem{wxODBC\_FWD\_ONLY\_CURSORS}{When a new database connection is | |
268 | requested, this setting controls the default of whether the connection allows | |
269 | only forward scrolling cursors, or forward and backward scrolling cursors | |
270 | (see the section in "WHERE TO START" on cursors for more information on | |
271 | cursors). This default can be overridden by passing a second parameter to | |
272 | either the \helpref{wxDbGetConnection}{wxdbfunctions} or | |
273 | \helpref{wxDb constructor}{wxdbconstr}. The default is 1.} | |
274 | \twocolitem{wxODBC\_BACKWARD\_COMPATABILITY}{Between v2.0 and 2.2, massive | |
275 | renaming efforts were done to the ODBC classes to get naming conventions | |
276 | similar to those used throughout wxWindows, as well as to preface all wxODBC | |
277 | classes names and functions with a wxDb preface. Because this renaming would | |
278 | affect applications written using the v2.0 names, this compile-time directive | |
279 | was added to allow those programs written for v2.0 to still compile using the | |
280 | old naming conventions. These deprecated names are all {\tt\#}define'd to their | |
281 | corresponding new function names at the end of the db.cpp/dbtable.cpp source | |
282 | files. These deprecated class/function names should not be used in future | |
283 | development, as at some point in the future they will be removed. The default | |
284 | is 0.} | |
285 | \end{twocollist} | |
286 | ||
287 | {\it Under MS Windows} | |
288 | ||
289 | You are required to include the "odbc32.lib" provided by your compiler vendor | |
290 | in the list of external libraries to be linked in. If using the makefiles | |
291 | supplied with wxWindows, this library should already be included for use with | |
292 | makefile.b32, makefile.vc, and makefile.g95. | |
293 | ||
294 | You cannot compile the wxODBC classes under Win16 - sorry. | |
295 | ||
296 | \normalbox{MORE TO COME} | |
297 | ||
298 | {\it Under Unix} | |
299 | --with-odbc flag for configure | |
300 | ||
301 | \normalbox{MORE TO COME} | |
302 | ||
303 | \subsection{wxODBC - Basic Step-By-Step Guide}\label{wxodbcstepbystep} | |
304 | ||
305 | To use the classes in an application, there are eight basic steps: | |
306 | ||
307 | \begin{itemize}\itemsep=0pt | |
308 | \item Define datasource connection information | |
309 | \item Get a datasource connection | |
310 | \item Create table definition | |
311 | \item Open the table | |
312 | \item Use the table | |
313 | \item Close the table | |
314 | \item Close the datasource connection | |
315 | \item Release the ODBC environment handle | |
316 | \end{itemize} | |
317 | ||
318 | Following each of these steps is detailed to explain the step, and to | |
319 | hopefully mention as many of the pitfalls that beginning users fall in | |
320 | to when first starting to use the classes. Throughout the steps, small | |
321 | snippets of code are provided to show the syntax of performing the step. A | |
322 | complete code snippet is provided at the end of this overview that shows a | |
323 | complete working flow of all these steps (see | |
324 | \helpref{wxODBC - Sample Code}{wxodbcsamplecode1}). | |
325 | ||
326 | {\bf Define datasource connection information} | |
327 | ||
328 | To be able to connect to a datasource through the ODBC driver, a program must | |
329 | supply a minimum of three pieces of information: Datasource name, User ID, and | |
330 | Authorization string (password). A fourth piece of information, a default | |
331 | directory indicating where the data file is stored, is required for Text and | |
332 | dBase drivers for ODBC. | |
333 | ||
334 | The wxWindows data class wxDbConnectInf exists for holding all of these | |
335 | values, plus some others that may be desired. | |
336 | ||
337 | The 'Henv' member is the environment handle used to access memory for use by the | |
338 | ODBC driver. Use of this member is described below in the "Getting a Connection | |
339 | to the Datasource" section. | |
340 | ||
341 | The 'Dsn' must exactly match the datasource name used to configure the ODBC | |
342 | datasource (in the ODBC Administrator (MSW only) or in the .odbc.ini file). | |
343 | ||
344 | The 'Uid' is the User ID that is to be used to log in to the datasource. This | |
345 | User ID must already have been created and assigned rights within the | |
346 | datasource to which you are connecting. The user that the connection is | |
347 | establish by will determine what rights and privileges the datasource | |
348 | connection will allow the program to have when using the connection that | |
349 | this connection information was used to establish. Some datasources are | |
350 | case sensitive for User IDs, and though the wxODBC classes attempt to hide | |
351 | this from you by manipulating whatever data you pass in to match the | |
352 | datasource's needs, it is always best to pass the 'Uid' in the case that | |
353 | the datasource requires. | |
354 | ||
355 | The 'AuthStr' is the password for the User ID specified in the 'Uid' member. | |
356 | As with the 'Uid', some datasources are case sensitive (in fact most are). | |
357 | The wxODBC classes do NOT try to manage the case of the 'AuthStr' at all. | |
358 | It is passed verbatim to the datasource, so you must use the case that the | |
359 | datasource is expecting. | |
360 | ||
361 | The 'defaultDir' member is used with file based datasources (i.e. dBase, | |
362 | FoxPro, text files). It contains a full path to the location where the | |
363 | data table or file is located. When setting this value, use forward | |
364 | slashes '/' rather than backslashes '\' to avoid compatibility differences | |
365 | between ODBC drivers. | |
366 | ||
367 | The other fields are currently unused. The intent of these fields are that | |
368 | they will be used to write our own ODBC Administrator type program that will | |
369 | work on both MSW and Un*x systems, regardless of the datasource. Very little | |
370 | work has been done on this to date. | |
371 | ||
372 | {\bf Get a Datasource Connection} | |
373 | ||
374 | There are two methods of establishing a connection to a datasource. You | |
375 | may either manually create your own wxDb instance and open the connection, | |
376 | or you may use the caching functions provided with the wxODBC classes to | |
377 | create/maintain/delete the connections. | |
378 | ||
379 | Regardless of which method you use, you must first have a fully populated | |
380 | wxDbConnectInf object. In the wxDbConnectInf instance, provide a valid | |
381 | Dns, Uid, and AuthStr (along with a 'defaultDir' if necessary). Before | |
382 | using this though, you must allocate an environment handle to the 'Henv' | |
383 | member. | |
384 | ||
385 | \begin{verbatim} | |
386 | wxDbConnectInf DbConnectInf; | |
387 | DbConnectInf.SetDsn("MyDSN"); | |
388 | DbConnectInf.SetUserID("MyUserName"); | |
389 | DbConnectInf.SetPassword("MyPassword"); | |
390 | DbConnectInf.SetDefaultDir(""); | |
391 | \end{verbatim} | |
392 | ||
393 | To allocate an environment handle for the ODBC connection to use, the | |
394 | wxDbConnectInf class has a datasource independent method for creating | |
395 | the necessary handle: | |
396 | ||
397 | \begin{verbatim} | |
398 | if (DbConnectInf.AllocHenv()) | |
399 | { | |
400 | wxMessageBox("Unable to allocate an ODBC environment handle", | |
401 | "DB CONNECTION ERROR", wxOK | wxICON_EXCLAMATION); | |
402 | return; | |
403 | } | |
404 | \end{verbatim} | |
405 | ||
406 | When the wxDbConnectInf::AllocHenv() function is called successfully, a | |
407 | value of true will be returned. A value of false means allocation failed, | |
408 | and the handle will be undefined. | |
409 | ||
410 | A shorter form of doing the above steps is encapsulated into the | |
411 | long form of the constructor for wxDbConnectInf. | |
412 | ||
413 | \begin{verbatim} | |
414 | wxDbConnectInf *DbConnectInf; | |
415 | ||
416 | DbConnectInf = new wxDbConnectInf(NULL, "MyDSN", "MyUserName", | |
417 | "MyPassword", ""); | |
418 | \end{verbatim} | |
419 | ||
420 | This shorthand form of initializing the constructor passes a NULL for the SQL | |
421 | environment handle, telling the constructor to allocate a handle during | |
422 | construction. This handle is also managed for the life of wxDbConnectInf | |
423 | instance, and is freed automatically upon destruction of the instance. | |
424 | ||
425 | Once the wxDbConnectInf instance is initialized, you are ready to | |
426 | connect to the datasource. | |
427 | ||
428 | To manually create datasource connections, you must create a wxDb | |
429 | instance, and then open it. | |
430 | ||
431 | \begin{verbatim} | |
432 | wxDb *db = new wxDb(DbConnectInf->GetHenv()); | |
433 | ||
434 | opened = db->Open(DbConnectInf); | |
435 | \end{verbatim} | |
436 | ||
437 | The first line does the house keeping needed to initialize all | |
438 | the members of the wxDb class. The second line actually sends the request | |
439 | to the ODBC driver to open a connection to its associated datasource using | |
440 | the parameters supplied in the call to \helpref{wxDb::Open}{wxdbopen}. | |
441 | ||
442 | A more advanced form of opening a connection is to use the connection | |
443 | caching functions that are included with the wxODBC classes. The caching | |
444 | mechanisms perform the same functions as the manual approach to opening a | |
445 | connection, but they also manage each connection they have created, | |
446 | re-using them and cleaning them up when they are closed, without you | |
447 | needing to do the coding. | |
448 | ||
449 | To use the caching function \helpref{wxDbGetConnection}{wxdbfunctions} to get | |
450 | a connection to a datasource, simply call it with a single parameter of the | |
451 | type wxDbConnectInf: | |
452 | ||
453 | \begin{verbatim} | |
454 | db = wxDbGetConnection(DbConnectInf); | |
455 | \end{verbatim} | |
456 | ||
457 | The wxDb pointer that is returned is both initialized and opened. If | |
458 | something failed in creating or opening the connection, the return value | |
459 | from \helpref{wxDbGetConnection}{wxdbfunctions} will be NULL. | |
460 | ||
461 | The connection that is returned is either a new connection, or it is a | |
462 | "free" connection from the cache of connections that the class maintains | |
463 | that was no longer in use. Any wxDb instance created with a call to | |
464 | \helpref{wxDbGetConnection}{wxdbfunctions} is recorded in a linked list of established | |
465 | connections. When a program is finished with a connection, a call to | |
466 | \helpref{wxDbFreeConnection}{wxdbfunctions} is made, and the datasource | |
467 | connection will then be tagged as FREE, making it available for the next | |
468 | call to \helpref{wxDbGetConnection}{wxdbfunctions} that needs a connection | |
469 | using the same connection information (Dsn, Uid, AuthStr). The cached | |
470 | connections remain cached until a call to \helpref{wxDbCloseConnections}{wxdbfunctions} is made, | |
471 | at which time all cached connections are closed and deleted. | |
472 | ||
473 | Besides the obvious advantage of using the single command caching routine to | |
474 | obtain a datasource connection, using cached connections can be quite a | |
475 | performance boost as well. Each time that a new connection is created | |
476 | (not retrieved from the cache of free connections), the wxODBC classes | |
477 | perform many queries against the datasource to determine the datasource's | |
478 | datatypes and other fundamental behaviours. Depending on the hardware, | |
479 | network bandwidth, and datasource speed, this can in some cases take a | |
480 | few seconds to establish the new connection (with well-balanced systems, | |
481 | it should only be a fraction of a second). Re-using already established | |
482 | datasource connections rather than creating/deleting, creating/deleting | |
483 | connections can be quite a time-saver. | |
484 | ||
485 | Another time-saver is the "copy connection" features of both | |
486 | \helpref{wxDb::Open}{wxdbopen} and \helpref{wxDbGetConnection}{wxdbfunctions}. | |
487 | If manually creating a wxDb instance and opening it, you must pass an existing | |
488 | connection to the \helpref{wxDb::Open}{wxdbopen} function yourself to gain the performance | |
489 | benefit of copying existing connection settings. The | |
490 | \helpref{wxDbGetConnection}{wxdbfunctions} function automatically does this | |
491 | for you, checking the Dsn, Uid, and AuthStr parameters when you request | |
492 | a connection for any existing connections that use those same settings. | |
493 | If one is found, \helpref{wxDbGetConnection}{wxdbfunctions} copies the datasource settings for | |
494 | datatypes and other datasource specific information that was previously | |
495 | queried, rather than re-querying the datasource for all those same settings. | |
496 | ||
497 | One final note on creating a connection. When a connection is created, it | |
498 | will default to only allowing cursor scrolling to be either forward only, | |
499 | or both backward and forward scrolling. The default behavior is | |
500 | determined by the setting {\tt wxODBC\_FWD\_ONLY\_CURSORS} in setup.h when you | |
501 | compile the wxWindows library. The library default is to only support | |
502 | forward scrolling cursors only, though this can be overridden by parameters | |
503 | for wxDb() constructor or the \helpref{wxDbGetConnection}{wxdbfunctions} | |
504 | function. All datasources and ODBC drivers must support forward scrolling | |
505 | cursors. Many datasources support backward scrolling cursors, and many | |
506 | ODBC drivers support backward scrolling cursors. Before planning on using | |
507 | backward scrolling cursors, you must be certain that both your datasource | |
508 | and ODBC driver fully support backward scrolling cursors. See the small | |
509 | blurb about "Scrolling cursors" in the definitions at the beginning of | |
510 | this overview, or other details of setting the cursor behavior in the wxDb | |
511 | class documentation. | |
512 | ||
513 | {\bf Create Table Definition} | |
514 | ||
515 | Data can be accessed in a datasource's tables directly through various | |
516 | functions of the wxDb class (see \helpref{wxDb::GetData}{wxdbgetdata}). But to make life much | |
517 | simpler, the wxDbTable class encapsulates all of the SQL specific API calls | |
518 | that would be necessary to do this, wrapping it in an intuitive class of APIs. | |
519 | ||
520 | The first step in accessing data in a datasource's tables via the wxDbTable | |
521 | class is to create a wxDbTable instance. | |
522 | ||
523 | \begin{verbatim} | |
524 | table = new wxDbTable(db, tableName, numTableColumns, "", | |
525 | !wxDB_QUERY_ONLY, ""); | |
526 | \end{verbatim} | |
527 | ||
528 | When you create the instance, you indicate the previously established | |
529 | datasource connection to be used to access the table, the name of the | |
530 | primary table that is to be accessed with the datasource's tables, how many | |
531 | columns of each row are going to be returned, the name of the view of the | |
532 | table that will actually be used to query against (works with Oracle only | |
533 | at this time), whether the data returned is for query purposes only, and | |
534 | finally the path to the table, if different than the path specified when | |
535 | connecting to the datasource. | |
536 | ||
537 | Each of the above parameters are described in detail in the wxDbTable | |
538 | class' description, but one special note here about the fifth | |
539 | parameter - the queryOnly setting. If a wxDbTable instance is created as | |
540 | {\tt wxDB\_QUERY\_ONLY}, then no inserts/deletes/updates can be performed | |
541 | using this instance of the wxDbTable. Any calls to \helpref{wxDb::CommitTrans}{wxdbcommittrans} | |
542 | or \helpref{wxDb::RollbackTrans}{wxdbrollbacktrans} against the datasource | |
543 | connection used by this wxDbTable instance are ignored by this instance. If | |
544 | the wxDbTable instance is created with {\tt !wxDB\_QUERY\_ONLY} as shown above, | |
545 | then all the cursors and other overhead associated with being able to | |
546 | insert/update/delete data in the table are created, and thereby those | |
547 | operations can then be performed against the associated table with this | |
548 | wxDbTable instance. | |
549 | ||
550 | If a table is to be accessed via a wxDbTable instance, and the table will | |
551 | only be read from, not written to, there is a performance benefit (not as | |
552 | many cursors need to be maintained/updated, hence speeding up access times), | |
553 | as well as a resource savings due to fewer cursors being created for the | |
554 | wxDbTable instance. Also, with some datasources, the number of | |
555 | simultaneous cursors is limited. | |
556 | ||
557 | When defining the columns to be retrievable by the wxDbTable instance, you | |
558 | can specify anywhere from one column up to all columns in the table. | |
559 | ||
560 | \begin{verbatim} | |
561 | table->SetColDefs(0, "FIRST_NAME", DB_DATA_TYPE_VARCHAR, FirstName, | |
562 | SQL_C_CHAR, sizeof(name), true, true); | |
563 | table->SetColDefs(1, "LAST_NAME", DB_DATA_TYPE_VARCHAR, LastName, | |
564 | SQL_C_CHAR, sizeof(LastName), true, true); | |
565 | \end{verbatim} | |
566 | ||
567 | Notice that column definitions start at index 0 and go up to one less than | |
568 | the number of columns specified when the wxDbTable instance was created | |
569 | (in this example, two columns - one with index 0, one with index 1). | |
570 | ||
571 | The above lines of code "bind" the datasource columns specified to the | |
572 | memory variables in the client application. So when the application | |
573 | makes a call to \helpref{wxDbTable::GetNext}{wxdbtablegetnext} (or any other function that retrieves | |
574 | data from the result set), the variables that are bound to the columns will | |
575 | have the column value stored into them. See the | |
576 | \helpref{wxDbTable::SetColDefs}{wxdbtablesetcoldefs} | |
577 | class documentation for more details on all the parameters for this function. | |
578 | ||
579 | The bound memory variables have undefined data in them until a call to a | |
580 | function that retrieves data from a result set is made | |
581 | (e.g. \helpref{wxDbTable::GetNext}{wxdbtablegetnext}, | |
582 | \helpref{wxDbTable::GetPrev}{wxdbtablegetprev}, etc). The variables are not | |
583 | initialized to any data by the wxODBC classes, and they still contain | |
584 | undefined data after a call to \helpref{wxDbTable::Query}{wxdbtablequery}. Only | |
585 | after a successful call to one of the ::GetXxxx() functions is made do the | |
586 | variables contain valid data. | |
587 | ||
588 | It is not necessary to define column definitions for columns whose data is | |
589 | not going to be returned to the client. For example, if you want to query | |
590 | the datasource for all users with a first name of 'GEORGE', but you only want | |
591 | the list of last names associated with those rows (why return the FIRST\_NAME | |
592 | column every time when you already know it is 'GEORGE'), you would only have | |
593 | needed to define one column above. | |
594 | ||
595 | You may have as many wxDbTable instances accessing the same table using the | |
596 | same wxDb instance as you desire. There is no limit imposed by the classes | |
597 | on this. All datasources supported (so far) also have no limitations on this. | |
598 | ||
599 | {\bf Open the table} | |
600 | ||
601 | Opening the table is not technically doing anything with the datasource | |
602 | itself. Calling \helpref{wxDbTable::Open}{wxdbtableopen} simply does all the | |
603 | housekeeping of checking that the specified table exists, that the current | |
604 | connected user has at least SELECT privileges for accessing the table, | |
605 | setting up the requisite cursors, binding columns and cursors, and | |
606 | constructing the default INSERT statement that is used when a new row is | |
607 | inserted into the table (non-wxDB\_QUERY\_ONLY tables only). | |
608 | ||
609 | \begin{verbatim} | |
610 | if (!table->Open()) | |
611 | { | |
612 | // An error occurred opening (setting up) the table | |
613 | } | |
614 | \end{verbatim} | |
615 | ||
616 | The only reason that a call to \helpref{wxDbTable::Open}{wxdbtableopen} is likely to fail is if the | |
617 | user has insufficient privileges to even SELECT the table. Other problems | |
618 | could occur, such as being unable to bind columns, but these other reason | |
619 | point to some lack of resource (like memory). Any errors generated | |
620 | internally in the \helpref{wxDbTable::Open}{wxdbtableopen} function are logged to the error log | |
621 | if SQL logging is turned on for the classes. | |
622 | ||
623 | {\bf Use the table} | |
624 | ||
625 | To use the table and the definitions that are now set up, we must first | |
626 | define what data we want the datasource to collect in to a result set, tell | |
627 | it where to get the data from, and in what sequence we want the data returned. | |
628 | ||
629 | \begin{verbatim} | |
630 | // the WHERE clause limits/specifies which rows in the table | |
631 | // are to be returned in the result set | |
632 | table->SetWhereClause("FIRST_NAME = 'GEORGE'"); | |
633 | ||
634 | // Result set will be sorted in ascending alphabetical | |
635 | // order on the data in the 'LAST_NAME' column of each row | |
636 | // If the same last name is in the table for two rows, | |
637 | // sub-sort on the 'AGE' column | |
638 | table->SetOrderByClause("LAST_NAME, AGE"); | |
639 | ||
640 | // No other tables (joins) are used for this query | |
641 | table->SetFromClause(""); | |
642 | \end{verbatim} | |
643 | ||
644 | The above lines will be used to tell the datasource to return in the result | |
645 | all the rows in the table whose column "FIRST\_NAME" contains the name | |
646 | 'GEORGE' (note the required use of the single quote around the string | |
647 | literal) and that the result set will return the rows sorted by ascending | |
648 | last names (ascending is the default, and can be overridden with the | |
649 | "DESC" keyword for datasources that support it - "LAST\_NAME DESC"). | |
650 | ||
651 | Specifying a blank WHERE clause will result in the result set containing | |
652 | all rows in the datasource. | |
653 | ||
654 | Specifying a blank ORDERBY clause means that the datasource will return | |
655 | the result set in whatever sequence it encounters rows which match the | |
656 | selection criteria. What this sequence is can be hard to determine. | |
657 | Typically it depends on the index that the datasource used to find the | |
658 | rows which match the WHERE criteria. BEWARE - relying on the datasource | |
659 | to return data in a certain sequence when you have not provided an ORDERBY | |
660 | clause will eventually cause a problem for your program. Databases can be | |
661 | tuned to be COST-based, SPEED-based, or some other basis for how it gets | |
662 | your result set. In short, if you need your result set returned in a | |
663 | specific sequence, ask for it that way by providing an ORDERBY clause. | |
664 | ||
665 | Using an ORDERBY clause can be a performance hit, as the database must | |
666 | sort the items before making the result set available to the client. | |
667 | Creating efficient indexes that cause the data to be "found" in the correct | |
668 | ORDERBY sequence can be a big performance benefit. Also, in the large | |
669 | majority of cases, the database will be able to sort the records faster | |
670 | than your application can read all the records in (unsorted) and then sort | |
671 | them. Let the database do the work for you! | |
672 | ||
673 | Notice in the example above, a column that is not included in the bound | |
674 | data columns ('AGE') will be used to sub-sort the result set. | |
675 | ||
676 | The FROM clause in this example is blanked, as we are not going to be | |
677 | performing any table joins with this simple query. When the FROM clause | |
678 | is blank, it is assumed that all columns referenced are coming from | |
679 | the default table for the wxDbTable instance. | |
680 | ||
681 | After the selection criteria have been specified, the program can now | |
682 | ask the datasource to perform the search and create a result set that | |
683 | can be retrieved: | |
684 | ||
685 | \begin{verbatim} | |
686 | // Instruct the datasource to perform a query based on the | |
687 | // criteria specified above in the where/orderBy/from clauses. | |
688 | if (!table->Query()) | |
689 | { | |
690 | // An error occurred performing the query | |
691 | } | |
692 | \end{verbatim} | |
693 | ||
694 | Typically, when an error occurs when calling \helpref{wxDbTable::Query}{wxdbtablequery}, it is a | |
695 | syntax problem in the WHERE clause that was specified. The exact SQL | |
696 | (datasource-specific) reason for what caused the failure of \helpref{wxDbTable::Query}{wxdbtablequery} | |
697 | (and all other operations against the datasource can be found by | |
698 | parsing the table's database connection's "errorList[]" array member for | |
699 | the stored text of the error. | |
700 | ||
701 | When the \helpref{wxDbTable::Query}{wxdbtablequery} returns true, the | |
702 | database was able to successfully complete the requested query using the | |
703 | provided criteria. This does not mean that there are any rows in the | |
704 | result set, it just mean that the query was successful. | |
705 | ||
706 | \normalbox{IMPORTANT: The result created by the call to | |
707 | \helpref{wxDbTable::Query}{wxdbtablequery} can take one of two forms. It is | |
708 | either a snapshot of the data at the exact moment that the database | |
709 | determined the record matched the search criteria, or it is a pointer to | |
710 | the row that matched the selection criteria. Which form of behavior is | |
711 | datasource dependent. If it is a snapshot, the data may have changed | |
712 | since the result set was constructed, so beware if your datasource | |
713 | uses snapshots and call \helpref{wxDbTable::Refresh}{wxdbtablerefresh}. Most larger brand databases | |
714 | do not use snapshots, but it is important to mention so that your application | |
715 | can handle it properly if your datasource does.} | |
716 | ||
717 | To retrieve the data, one of the data fetching routines must be used to | |
718 | request a row from the result set, and to store the data from the result | |
719 | set into the bound memory variables. After \helpref{wxDbTable::Query}{wxdbtablequery} | |
720 | has completed successfully, the default/current cursor is placed so it | |
721 | is pointing just before the first record in the result set. If the | |
722 | result set is empty (no rows matched the criteria), then any calls to | |
723 | retrieve data from the result set will return false. | |
724 | ||
725 | \begin{verbatim} | |
726 | wxString msg; | |
727 | ||
728 | while (table->GetNext()) | |
729 | { | |
730 | msg.Printf("Row #%lu -- First Name : %s Last Name is %s", | |
731 | table->GetRowNum(), FirstName, LastName); | |
732 | wxMessageBox(msg, "Data", wxOK | wxICON_INFORMATION, NULL); | |
733 | } | |
734 | \end{verbatim} | |
735 | ||
736 | The sample code above will read the next record in the result set repeatedly | |
737 | until the end of the result set has been reached. The first time that | |
738 | \helpref{wxDbTable::GetNext}{wxdbtablegetnext} is called right after the successful | |
739 | call to \helpref{wxDbTable::Query}{wxdbtablequery}, it actually returns the first record | |
740 | in the result set. | |
741 | ||
742 | When \helpref{wxDbTable::GetNext}{wxdbtablegetnext} is called and there are | |
743 | no rows remaining in the result set after the current cursor position, | |
744 | \helpref{wxDbTable::GetNext}{wxdbtablegetnext} (as well as all the other | |
745 | wxDbTable::GetXxxxx() functions) will return false. | |
746 | ||
747 | {\bf Close the table} | |
748 | ||
749 | When the program is done using a wxDbTable instance, it is as simple as | |
750 | deleting the table pointer (or if declared statically, letting the | |
751 | variable go out of scope). Typically the default destructor will take | |
752 | care of all that is required for cleaning up the wxDbTable instance. | |
753 | ||
754 | \begin{verbatim} | |
755 | if (table) | |
756 | { | |
757 | delete table; | |
758 | table = NULL; | |
759 | } | |
760 | \end{verbatim} | |
761 | ||
762 | Deleting a wxDbTable instance releases all of its cursors, deletes the | |
763 | column definitions and frees the SQL environment handles used by the | |
764 | table (but not the environment handle used by the datasource connection | |
765 | that the wxDbTable instance was using). | |
766 | ||
767 | {\bf Close the datasource connection} | |
768 | ||
769 | After all tables that have been using a datasource connection have been | |
770 | closed (this can be verified by calling \helpref{wxDb::GetTableCount}{wxdbgettablecount} | |
771 | and checking that it returns 0), then you may close the datasource | |
772 | connection. The method of doing this is dependent on whether the | |
773 | non-caching or caching method was used to obtain the datasource connection. | |
774 | ||
775 | If the datasource connection was created manually (non-cached), closing the | |
776 | connection is done like this: | |
777 | ||
778 | \begin{verbatim} | |
779 | if (db) | |
780 | { | |
781 | db->Close(); | |
782 | delete db; | |
783 | db = NULL; | |
784 | } | |
785 | \end{verbatim} | |
786 | ||
787 | If the program used the \helpref{wxDbGetConnection}{wxdbfunctions} function to get a datasource | |
788 | connection, the following is the code that should be used to free the | |
789 | connection(s): | |
790 | ||
791 | \begin{verbatim} | |
792 | if (db) | |
793 | { | |
794 | wxDbFreeConnection(db); | |
795 | db = NULL; | |
796 | } | |
797 | \end{verbatim} | |
798 | ||
799 | Note that the above code just frees the connection so that it can be | |
800 | re-used on the next call the \helpref{wxDbGetConnection}{wxdbfunctions}. To actually dispose | |
801 | of the connection, releasing all of its resources (other than the | |
802 | environment handle), do the following: | |
803 | ||
804 | \begin{verbatim} | |
805 | wxDbCloseConnections(); | |
806 | \end{verbatim} | |
807 | ||
808 | {\bf Release the ODBC environment handle} | |
809 | ||
810 | Once all of the connections that used the ODBC environment handle (in | |
811 | this example it was stored in "DbConnectInf.Henv") have been closed, then | |
812 | it is safe to release the environment handle: | |
813 | ||
814 | \begin{verbatim} | |
815 | DbConnectInf->FreeHenv(); | |
816 | \end{verbatim} | |
817 | ||
818 | Or, if the long form of the constructor was used and the constructor was allowed | |
819 | to allocate its own SQL environment handle, leaving scope or destruction of the | |
820 | wxDbConnectInf will free the handle automatically. | |
821 | ||
822 | \begin{verbatim} | |
823 | delete DbConnectInf; | |
824 | \end{verbatim} | |
825 | ||
826 | \normalbox{Remember to never release this environment handle if there are any | |
827 | connections still using the handle.} | |
828 | ||
829 | \subsection{wxODBC - Known Issues}\label{wxodbcknownissues} | |
830 | ||
831 | As with creating wxWindows, writing the wxODBC classes was not the simple | |
832 | task of writing an application to run on a single type of computer system. | |
833 | The classes need to be cross-platform for different operating systems, and | |
834 | they also needed to take in to account different database manufacturers and | |
835 | different ODBC driver manufacturers. Because of all the possible combinations | |
836 | of OS/database/drivers, it is impossible to say that these classes will work | |
837 | perfectly with datasource ABC, ODBC driver XYZ, on platform LMN. You may run | |
838 | in to some incompatibilities or unsupported features when moving your | |
839 | application from one environment to another. But that is what makes | |
840 | cross-platform programming fun. It is also pinpoints one of the great | |
841 | things about open source software. It can evolve! | |
842 | ||
843 | The most common difference between different database/ODBC driver | |
844 | manufacturers in regards to these wxODBC classes is the lack of | |
845 | standard error codes being returned to the calling program. Sometimes | |
846 | manufacturers have even changed the error codes between versions of | |
847 | their databases/drivers. | |
848 | ||
849 | In all the tested databases, every effort has been made to determine | |
850 | the correct error codes and handle them in the class members that need | |
851 | to check for specific error codes (such as TABLE DOES NOT EXIST when | |
852 | you try to open a table that has not been created yet). Adding support | |
853 | for additional databases in the future requires adding an entry for the | |
854 | database in the \helpref{wxDb::Dbms}{wxdbdbms} function, and then handling any error codes | |
855 | returned by the datasource that do not match the expected values. | |
856 | ||
857 | {\bf Databases} | |
858 | ||
859 | Following is a list of known issues and incompatibilities that the | |
860 | wxODBC classes have between different datasources. An up to date | |
861 | listing of known issues can be seen in the comments of the source | |
862 | for \helpref{wxDb::Dbms}{wxdbdbms}. | |
863 | ||
864 | {\it ORACLE} | |
865 | \begin{itemize}\itemsep=0pt | |
866 | \item Currently the only database supported by the wxODBC classes to support VIEWS | |
867 | \end{itemize} | |
868 | ||
869 | {\it DBASE} | |
870 | ||
871 | NOTE: dBase is not a true ODBC datasource. You only have access to as much | |
872 | functionality as the driver can emulate. | |
873 | ||
874 | \begin{itemize}\itemsep=0pt | |
875 | \item Does not support the SQL\_TIMESTAMP structure | |
876 | \item Supports only one cursor and one connect (apparently? with Microsoft driver only?) | |
877 | \item Does not automatically create the primary index if the 'keyField' param of SetColDef is true. The user must create ALL indexes from their program with calls to \helpref{wxDbTable::CreateIndex}{wxdbtablecreateindex} | |
878 | \item Table names can only be 8 characters long | |
879 | \item Column names can only be 10 characters long | |
880 | \item Currently cannot CREATE a dBase table - bug or limitation of the drivers used?? | |
881 | \item Currently cannot insert rows that have integer columns - bug?? | |
882 | \end{itemize} | |
883 | ||
884 | {\it SYBASE (all)} | |
885 | \begin{itemize}\itemsep=0pt | |
886 | \item To lock a record during QUERY functions, the reserved word 'HOLDLOCK' must be added after every table name involved in the query/join if that table's matching record(s) are to be locked | |
887 | \item Ignores the keywords 'FOR UPDATE'. Use the HOLDLOCK functionality described above | |
888 | \end{itemize} | |
889 | ||
890 | {\it SYBASE (Enterprise)} | |
891 | \begin{itemize}\itemsep=0pt | |
892 | \item If a column is part of the Primary Key, the column cannot be NULL | |
893 | \item Maximum row size is somewhere in the neighborhood of 1920 bytes | |
894 | \end{itemize} | |
895 | ||
896 | {\it mySQL} | |
897 | \begin{itemize}\itemsep=0pt | |
898 | \item If a column is part of the Primary Key, the column cannot be NULL. | |
899 | \item Cannot support selecting for update [\helpref{wxDbTable::CanSelectForUpdate}{wxdbtablecanselectforupdate}]. Always returns false. | |
900 | \item Columns that are part of primary or secondary keys must be defined as being NOT NULL when they are created. Some code is added in \helpref{wxDbTable::CreateIndex}{wxdbtablecreateindex} to try to adjust the column definition if it is not defined correctly, but it is experimental (as of wxWindows v2.2.1) | |
901 | \item Does not support sub-queries in SQL statements | |
902 | \end{itemize} | |
903 | ||
904 | {\it POSTGRES} | |
905 | \begin{itemize}\itemsep=0pt | |
906 | \item Does not support the keywords 'ASC' or 'DESC' as of release v6.5.0 | |
907 | \item Does not support sub-queries in SQL statements | |
908 | \end{itemize} | |
909 | ||
910 | {\it DB2} | |
911 | \begin{itemize}\itemsep=0pt | |
912 | \item Columns which are part of a primary key must be declared as NOT NULL | |
913 | \end{itemize} | |
914 | ||
915 | {\bf UNICODE with wxODBC classes} | |
916 | ||
917 | The ODBC classes support for Unicode is yet in early experimental stage and | |
918 | hasn't been tested extensively. It might work for you or it might not: please | |
919 | report the bugs/problems you have encountered in the latter case. | |
920 | ||
921 | \subsection{wxODBC - Sample Code}\label{wxodbcsamplecode1} | |
922 | ||
923 | Simplest example of establishing/opening a connection to an ODBC datasource, | |
924 | binding variables to the columns for read/write usage, opening an | |
925 | existing table in the datasource, setting the query parameters | |
926 | (where/orderBy/from), querying the datasource, reading each row of the | |
927 | result set, then cleaning up. | |
928 | ||
929 | NOTE: Not all error trapping is shown here, to reduce the size of the | |
930 | code and to make it more easily readable. | |
931 | ||
932 | \begin{verbatim} | |
933 | wxDbConnectInf *DbConnectInf = NULL; | |
934 | ||
935 | wxDb *db = NULL; // The database connection | |
936 | wxDbTable *table = NULL; // The data table to access | |
937 | ||
938 | wxChar FirstName[50+1]; // buffer for data from column "FIRST_NAME" | |
939 | wxChar LastName[50+1]; // buffer for data from column "LAST_NAME" | |
940 | ||
941 | bool errorOccured = false; | |
942 | ||
943 | const wxChar tableName[] = "CONTACTS"; | |
944 | const UWORD numTableColumns = 2; // Number of bound columns | |
945 | ||
946 | FirstName[0] = 0; | |
947 | LastName[0] = 0; | |
948 | ||
949 | DbConnectInf = new wxDbConnectInf(NULL,"MyDSN","MyUserName", "MyPassword"); | |
950 | ||
951 | if (!DbConnectInf || !DbConnectInf->GetHenv()) | |
952 | { | |
953 | wxMessageBox("Unable to allocate an ODBC environment handle", | |
954 | "DB CONNECTION ERROR", wxOK | wxICON_EXCLAMATION); | |
955 | return; | |
956 | } | |
957 | ||
958 | // Get a database connection from the cached connections | |
959 | db = wxDbGetConnection(DbConnectInf); | |
960 | ||
961 | // Create the table connection | |
962 | table = new wxDbTable(db, tableName, numTableColumns, "", | |
963 | !wxDB_QUERY_ONLY, ""); | |
964 | ||
965 | // | |
966 | // Bind the columns that you wish to retrieve. Note that there must be | |
967 | // 'numTableColumns' calls to SetColDefs(), to match the wxDbTable definition | |
968 | // | |
969 | // Not all columns need to be bound, only columns whose values are to be | |
970 | // returned back to the client. | |
971 | // | |
972 | table->SetColDefs(0, "FIRST_NAME", DB_DATA_TYPE_VARCHAR, FirstName, | |
973 | SQL_C_CHAR, sizeof(name), true, true); | |
974 | table->SetColDefs(1, "LAST_NAME", DB_DATA_TYPE_VARCHAR, LastName, | |
975 | SQL_C_CHAR, sizeof(LastName), true, true); | |
976 | ||
977 | // Open the table for access | |
978 | table->Open(); | |
979 | ||
980 | // Set the WHERE clause to limit the result set to return | |
981 | // all rows that have a value of 'GEORGE' in the FIRST_NAME | |
982 | // column of the table. | |
983 | table->SetWhereClause("FIRST_NAME = 'GEORGE'"); | |
984 | ||
985 | // Result set will be sorted in ascending alphabetical | |
986 | // order on the data in the 'LAST_NAME' column of each row | |
987 | table->SetOrderByClause("LAST_NAME"); | |
988 | ||
989 | // No other tables (joins) are used for this query | |
990 | table->SetFromClause(""); | |
991 | ||
992 | // Instruct the datasource to perform a query based on the | |
993 | // criteria specified above in the where/orderBy/from clauses. | |
994 | if (!table->Query()) | |
995 | { | |
996 | wxMessageBox("Error on Query()","ERROR!", | |
997 | wxOK | wxICON_EXCLAMATION); | |
998 | errorOccured = true; | |
999 | } | |
1000 | ||
1001 | wxString msg; | |
1002 | ||
1003 | // Start and continue reading every record in the table | |
1004 | // displaying info about each record read. | |
1005 | while (table->GetNext()) | |
1006 | { | |
1007 | msg.Printf("Row #%lu -- First Name : %s Last Name is %s", | |
1008 | table->GetRowNum(), FirstName, LastName); | |
1009 | wxMessageBox(msg, "Data", wxOK | wxICON_INFORMATION, NULL); | |
1010 | } | |
1011 | ||
1012 | ||
1013 | // | |
1014 | // Select the row which has FIRST_NAME of 'GEORGE' and LAST_NAME | |
1015 | // of 'TASKER', then delete the retrieved row | |
1016 | // | |
1017 | table->SetWhereClause("FIRST_NAME = 'GEORGE' and "LAST_NAME = 'TASKER'"); | |
1018 | if (table->Query()) | |
1019 | { | |
1020 | table->Delete(); | |
1021 | ||
1022 | // Must commit the deletion | |
1023 | table->GetDb()->CommitTrans(); | |
1024 | } | |
1025 | ||
1026 | ||
1027 | // | |
1028 | // Insert a new row into the table | |
1029 | // | |
1030 | wxStrcpy(FirstName, "JULIAN"); | |
1031 | wxStrcpy(LastName, "SMART"); | |
1032 | table->Insert(); | |
1033 | ||
1034 | // Must commit the insert | |
1035 | table->GetDb()->CommitTrans(); | |
1036 | ||
1037 | ||
1038 | // If the wxDbTable instance was successfully created | |
1039 | // then delete it as we are done with it now. | |
1040 | if (table) | |
1041 | { | |
1042 | wxDelete(table); | |
1043 | } | |
1044 | ||
1045 | // If we have a valid wxDb instance, then free the connection | |
1046 | // (meaning release it back in to the cache of datasource | |
1047 | // connections) for the next time a call to wxDbGetConnection() | |
1048 | // is made. | |
1049 | if (db) | |
1050 | { | |
1051 | wxDbFreeConnection(db); | |
1052 | db = NULL; | |
1053 | } | |
1054 | ||
1055 | // The program is now ending, so we need to close | |
1056 | // any cached connections that are still being | |
1057 | // maintained. | |
1058 | wxDbCloseConnections(); | |
1059 | ||
1060 | // Release the environment handle that was created | |
1061 | // for use with the ODBC datasource connections | |
1062 | wxDelete(DbConnectInf); | |
1063 | ||
1064 | \end{verbatim} | |
1065 | ||
1066 | \subsection{A selection of SQL commands}\label{sqlcommands} | |
1067 | ||
1068 | The following is a very brief description of some common SQL commands, with | |
1069 | examples. | |
1070 | ||
1071 | \wxheading{See also} | |
1072 | ||
1073 | \helpref{Database classes overview}{odbcoverview} | |
1074 | ||
1075 | \subsubsection{Create} | |
1076 | ||
1077 | Creates a table. | |
1078 | ||
1079 | Example: | |
1080 | ||
1081 | \begin{verbatim} | |
1082 | CREATE TABLE Book | |
1083 | (BookNumber INTEGER PRIMARY KEY | |
1084 | , CategoryCode CHAR(2) DEFAULT 'RO' NOT NULL | |
1085 | , Title VARCHAR(100) UNIQUE | |
1086 | , NumberOfPages SMALLINT | |
1087 | , RetailPriceAmount NUMERIC(5,2) | |
1088 | ) | |
1089 | \end{verbatim} | |
1090 | ||
1091 | \subsubsection{Insert} | |
1092 | ||
1093 | Inserts records into a table. | |
1094 | ||
1095 | Example: | |
1096 | ||
1097 | \begin{verbatim} | |
1098 | INSERT INTO Book | |
1099 | (BookNumber, CategoryCode, Title) | |
1100 | VALUES(5, 'HR', 'The Lark Ascending') | |
1101 | \end{verbatim} | |
1102 | ||
1103 | \subsubsection{Select} | |
1104 | ||
1105 | The Select operation retrieves rows and columns from a table. The criteria | |
1106 | for selection and the columns returned may be specified. | |
1107 | ||
1108 | Examples: | |
1109 | ||
1110 | {\tt SELECT * FROM Book} | |
1111 | ||
1112 | Selects all rows and columns from table Book. | |
1113 | ||
1114 | {\tt SELECT Title, RetailPriceAmount FROM Book WHERE RetailPriceAmount > 20.0} | |
1115 | ||
1116 | Selects columns Title and RetailPriceAmount from table Book, returning only | |
1117 | the rows that match the WHERE clause. | |
1118 | ||
1119 | {\tt SELECT * FROM Book WHERE CatCode = 'LL' OR CatCode = 'RR'} | |
1120 | ||
1121 | Selects all columns from table Book, returning only | |
1122 | the rows that match the WHERE clause. | |
1123 | ||
1124 | {\tt SELECT * FROM Book WHERE CatCode IS NULL} | |
1125 | ||
1126 | Selects all columns from table Book, returning only rows where the CatCode column | |
1127 | is NULL. | |
1128 | ||
1129 | {\tt SELECT * FROM Book ORDER BY Title} | |
1130 | ||
1131 | Selects all columns from table Book, ordering by Title, in ascending order. To specify | |
1132 | descending order, add DESC after the ORDER BY Title clause. | |
1133 | ||
1134 | {\tt SELECT Title FROM Book WHERE RetailPriceAmount >= 20.0 AND RetailPriceAmount <= 35.0} | |
1135 | ||
1136 | Selects records where RetailPriceAmount conforms to the WHERE expression. | |
1137 | ||
1138 | \subsubsection{Update} | |
1139 | ||
1140 | Updates records in a table. | |
1141 | ||
1142 | Example: | |
1143 | ||
1144 | {\tt UPDATE Incident SET X = 123 WHERE ASSET = 'BD34'} | |
1145 | ||
1146 | This example sets a field in column `X' to the number 123, for the record | |
1147 | where the column ASSET has the value `BD34'. | |
1148 |