📄 db.tex
字号:
connection does NOT close the connection, it only makes the connectionavailable again.\func{wxDb *}{wxDbGetConnection}{\param{wxDbConnectInf *}{pDbConfig},\param{bool }{FwdOnlyCursors=(bool)wxODBC\_FWD\_ONLY\_CURSORS}}\wxheading{Remarks}This function is used to request a "new" wxDb instance for use by the program.The wxDb instance returned is also opened (see \helpref{wxDb::Open}{wxdbopen}).This function (along with wxDbFreeConnection() and wxDbCloseConnection())maintain a cache of wxDb instances for user/re-use by a program. When aprogram needs a wxDb instance, it may call this function to obtain a wxDbinstance. If there is a wxDb instance in the cache that is currently unusedthat matches the connection requirements specified in {\it'pDbConfig'} thenthat cached connection is marked as no longer being free, and a pointer tothe wxDb instance is returned.If there are no connections available in the cache that meet the requirementsgiven in {\it'pDbConfig'}, then a new wxDb instance is created to connectto the datasource specified in {\it'pDbConfig'} using the userID and passwordgiven in {\it'pDbConfig'}.NOTE: The caching routine also uses the \helpref{wxDb::Open}{wxdbopen}connection datatype copying code. If the call to wxDbGetConnection()requests a connection to a datasource, and there is not one available in thecache, a new connection is created. But when the connection is opened,instead of polling the datasource over again for its datatypes, if aconnection to the same datasource (using the same userID/password) has alreadybeen done previously, the new connection skips querying the datasource forits datatypes, and uses the same datatypes determined previously by theother connection(s) for that same datasource. This cuts down greatly onnetwork traffic, database load, and connection creation time.When the program is done using a connection created through a call towxDbGetConnection(), the program should call wxDbFreeConnection() to releasethe wxDb instance back to the cache. DO NOT DELETE THE wxDb INSTANCE!Deleting the wxDb instance returned can cause a crash/memory corruptionlater in the program when the cache is cleaned up.When exiting the program, call wxDbCloseConnections() to close all thecached connections created by calls to wxDbGetConnection().\func{const wxChar *}{wxDbLogExtendedErrorMsg}{\param{const wxChar *}{userText}, \param{wxDb *}{pDb}, \param{wxChar *}{ErrFile}, \param{int }{ErrLine}}Writes a message to the wxLog window (stdout usually) when an internalerror situation occurs.\func{bool}{wxDbSqlLog}{\param{wxDbSqlLogState }{state}, \param{const wxString \&}{filename = SQL\_LOG\_FILENAME}}\wxheading{Remarks}This function sets the sql log state for all open wxDb objects\func{bool}{wxDbGetDataSource}{\param{HENV }{henv}, \param{wxChar *}{Dsn}, \param{SWORD }{DsnMax}, \param{wxChar *}{DsDesc}, \param{SWORD }{DsDescMax}, \param{UWORD }{direction = SQL\_FETCH\_NEXT}}\wxheading{Remarks}This routine queries the ODBC driver manager for a list of availabledatasources. Repeatedly call this function to obtain all the datasourcesavailable through the ODBC driver manager on the current workstation.\begin{verbatim} wxArrayString strArray; while (wxDbGetDataSource(DbConnectInf.GetHenv(), Dsn, SQL_MAX_DSN_LENGTH+1, DsDesc, 255)) strArray.Add(Dsn);\end{verbatim}\latexignore{\rtfignore{\wxheading{Members}}}\membersection{wxDb::wxDb}\label{wxdbctor}\func{}{wxDb}{\void}Default constructor.\func{}{wxDb}{\param{const HENV \&}{aHenv}, \param{bool }{FwdOnlyCursors=(bool)wxODBC\_FWD\_ONLY\_CURSORS}}Constructor, used to create an ODBC connection to a datasource.\wxheading{Parameters}\docparam{aHenv}{Environment handle used for this connection. See\helpref{wxDConnectInf::AllocHenv}{wxdbconnectinfallochenv}}\docparam{FwdOnlyCursors}{Will cursors created for use with this datasourceconnection only allow forward scrolling cursors.}\wxheading{Remarks}This is the constructor for the wxDb class. The wxDb object mustbe created and opened before any database activity can occur.\wxheading{Example}\begin{verbatim} wxDbConnectInf ConnectInf; ....Set values for member variables of ConnectInf here wxDb sampleDB(ConnectInf.GetHenv()); if (!sampleDB.Open(ConnectInf.GetDsn(), ConnectInf.GetUserID(), ConnectInf.GetPassword())) { // Error opening datasource }\end{verbatim}\wxheading{See also}\helpref{wxDbGetConnection}{wxdbfunctions}\membersection{wxDb::Catalog}\label{wxdbcatalog}\func{bool}{Catalog}{\param{wxChar *}{ userID}, \param{const wxString \&}{fileName =SQL\_CATALOG\_FILENAME}}Allows a data "dictionary" of the datasource to be created, dumping pertinentinformation about all data tables to which the user specified in userID hasaccess.\wxheading{Parameters}\docparam{userID}{Database user name to use in accessing the database. Alltables to which this user has rights will be evaluated in the catalog.}\docparam{fileName}{{\it OPTIONAL}. Name of the text file to create and writethe DB catalog to. Default is SQL\_CATALOG\_FILENAME.}\wxheading{Return value}Returns true if the catalog request was successful, or false if there was somereason that the catalog could not be generated.\wxheading{Example}\begin{verbatim}============== ============== ================ ========= =======TABLE NAME COLUMN NAME DATA TYPE PRECISION LENGTH============== ============== ================ ========= =======EMPLOYEE RECID (0008)NUMBER 15 8EMPLOYEE USER_ID (0012)VARCHAR2 13 13EMPLOYEE FULL_NAME (0012)VARCHAR2 26 26EMPLOYEE PASSWORD (0012)VARCHAR2 26 26EMPLOYEE START_DATE (0011)DATE 19 16\end{verbatim}\membersection{wxDb::Close}\label{wxdbclose}\func{void}{Close}{\void}Closes the database connection.\wxheading{Remarks}At the end of your program, when you have finished all of your database work,you must close the ODBC connection to the datasource. There are actuallyfour steps involved in doing this as illustrated in the example.Any wxDbTable instances which use this connection must be deleted beforeclosing the database connection.\wxheading{Example}\begin{verbatim} // Commit any open transactions on the datasource sampleDB.CommitTrans(); // Delete any remaining wxDbTable objects allocated with new delete parts; // Close the wxDb connection when finished with it sampleDB.Close();\end{verbatim}\membersection{wxDb::CommitTrans}\label{wxdbcommittrans}\func{bool}{CommitTrans}{\void}Permanently "commits" changes (insertions/deletions/updates) to the database.\wxheading{Return value}Returns true if the commit was successful, or false if the commit failed.\wxheading{Remarks}Transactions begin implicitly as soon as you make a change to the databasewith an insert/update/delete, or any other direct SQL command that performsone of these operations against the datasource.At any time thereafter, to save the changes to disk permanently, "commit"them by calling this function.Calling this member function commits ALL open transactions on this ODBCconnection. For example, if three different wxDbTable instances used thesame connection to the datasource, committing changes made on one of thosewxDbTable instances commits any pending transactions on all three wxDbTableinstances.Until a call to wxDb::CommitTrans() is made, no other user or cursor is ableto see any changes made to the row(s) that have been inserted/modified/deleted.\wxheading{Special Note : {\it Cursors} }\normalbox{It is important to understand that different database/ODBC drivercombinations handle transactions differently. One thing in particular thatyou must pay attention to is cursors, in regard to transactions. Cursors arewhat allow you to scroll through records forward and backward and tomanipulate records as you scroll through them. When you issue a query, acursor is created behind the scenes. The cursor keeps track of the query andkeeps track of the current record pointer. After you commit or rollback atransaction, the cursor may be closed automatically. This is databasedependent, and with some databases this behavior can be controlled throughmanagement functions. This means you would need to requery the datasourcebefore you can perform any additional work using this cursor. This is onlynecessary however if the datasource closes the cursor after a commit orrollback. Use the\helpref{wxDbTable::IsCursorClosedOnCommit}{wxdbtableiscursorclosedoncommit}member function to determine the datasource's transaction behavior. Note, inmany situations it is very inefficient to assume the cursor is closed andalways requery. This could put a significant, unnecessary load on datasourcesthat leave the cursors open after a transaction.}\membersection{wxDb::CreateView}\label{wxdbcreateviews}\func{bool}{CreateView}{\param{const wxString \&}{ viewName},\param{const wxString \&}{ colList}, \param{const wxString \&}{pSqlStmt}}Creates a SQL VIEW of one or more tables in a single datasource. Note thatthis function will only work against databases which support views (currentlyonly Oracle as of November 21 2000).\wxheading{Parameters}\docparam{viewName}{The name of the view. e.g. PARTS\_V}\docparam{colList}{{\it OPTIONAL} Pass in a comma delimited list of columnnames if you wish to explicitly name each column in the result set. If notdesired, pass in an empty string and the column names from the associatedtable(s) will be used.}\docparam{pSqlStmt}{Pointer to the select statement portion of the CREATEVIEW statement. Must be a complete, valid SQL SELECT statement.}\wxheading{Remarks}A 'view' is a logical table that derives columns from one or more othertables or views. Once the view is created, it can be queried exactly likeany other table in the database.NOTE: Views are not available with all datasources. Oracle is one exampleof a datasource which does support views.\wxheading{Example}\begin{verbatim} // Incomplete code sample db.CreateView("PARTS_SD1", "PN, PD, QTY", "SELECT PART_NUM, PART_DESC, QTY_ON_HAND * 1.1 FROM PARTS \ WHERE STORAGE_DEVICE = 1"); // PARTS_SD1 can now be queried just as if it were a data table. // e.g. SELECT PN, PD, QTY FROM PARTS_SD1\end{verbatim}\membersection{wxDb::Dbms}\label{wxdbdbms}\func{wxDBMS }{Dbms}{\void}\wxheading{Remarks}The return value will be of the enumerated type wxDBMS. This enumeratedtype contains a list of all the currently tested and supported databases.Additional databases may work with these classes, but the databasesreturned by this function have been tested and confirmed to work withthese ODBC classes.Possible values returned by this function can be viewed in the\helpref{Enumerated types}{wxdbenumeratedtypes} section of wxDb.There are known issues with conformance to the ODBC standards with severaldatasources supported by the wxWidgets ODBC classes. Please see the overviewfor specific details on which datasource have which issues.\wxheading{Return value}The return value will indicate which of the supported datasources iscurrently connected to by this connection. In the event that thedatasource is not recognized, a value of 'dbmsUNIDENTIFIED' is returned.\membersection{wxDb::DispAllErrors}\label{wxdbdispallerrors}\func{bool}{DispAllErrors}{\param{HENV}{ aHenv}, \param{HDBC}{ aHdbc = SQL\_NULL\_HDBC}, \param{HSTMT}{ aHstmt = SQL\_NULL\_HSTMT}}Used to log all database errors that occurred as a result of an executeddatabase command. This logging is automatic and also includes debug loggingwhen compiled in debug mode via \helpref{wxLogDebug}{wxlogdebug}. If loggingis turned on via \helpref{wxDb::SetSqlLogging}{wxdbsetsqllogging}, then anentry is also logged to the defined log file.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -