1)First, the wxWidgets library is required.
When compiling the wxWidgets library, set wxWidgets \ include \ wx \ MSW \ setup. h to 1.
2)The LIB of ODBC that requires wxWidgets
\ WxWidgets \ Lib \ vc_lib \ wxbase28d_odbc.lib (wxbase28_odbc.lib )\
\ Microsoft Visual Studio \ vc98 \ Lib \ odbc32.lib
3)Configure data sources
It can be divided into user DSN, system DSN, and file DSN, but generally it is effective for configuring user DSN and system DSN
4)Example
# Include <wx/DB. h>
# Include <wx/dbtable. h>
// Configure the environment
Wxdbconnectinf * Env = NULL;
ENV = new wxdbconnectinf (null, wxt ("DSN"), wxt ("username"), wxt ("password "));
// Establish a connection
Wxdb * conn = NULL;
Conn = wxdbgetconnection (ENV );
Wxdbtable * TBL = new wxdbtable (Conn, wxt ("mine"), 2, wxt (""), wxdb_query_only, wxt (""));
// Define the variable for saving the column content
Wxstring SST;
Wxchar ID [100];
Wxchar STR [100 + 1];
// Define several columns if there are several columns
TBL-> setcoldefs (0, wxt ("ID"), db_data_type_varchar, ID, SQL _c_numeric, sizeof (ID), true, true );
TBL-> setcoldefs (1, wxt ("name"), db_data_type_varchar, STR, SQL _c_wxchar, sizeof (STR), true, true );
// Open the dbtable object
If (! TBL-> open ())
{
Wxmessagebox ("error ");
Return;
}
// Query
If (! TBL-> query ())
{
Wxmessagebox ("error ");
Return;
}
// Read the variable
While (TBL-> getnext ())
{
SST. printf (wxt ("% S | % s"), ID, STR );
Wxmessagebox (SST );
}
// Disable the dbtable object
If (TBL)
{
Delete TBL;
TBL = 0;
}
// Close the connection
If (conn)
{
Wxdbfreeeconnection (conn );
Conn = 0;
}
// Release the environment
Env-> freehenv ()
5)SetcolsdefsData Relationship in
-------------------------------------------------------------------
C ++ datatype (Logical Data Type) ctype (Data Type in SQL)
-------------------------------------------------------------------
Wxchar db_data_type_varchar SQL _varchar
SQL _longvarchar
Db_data_type_date SQL _c_date
Db_data_type_integer SQL _c_bit
Wxchar db_data_type_varchar SQL _numeric
Db_data_type_float SQL _real
-------------------------------------------------------------------
6)Two ways to open a database connection
Wxdbconnectinf * Env = NULL;
Wxdb * conn = NULL;
ENV = new wxdbconnectinf (null, wxt ("DSN"), wxt ("username"), wxt ("password "));
// Method 1
Conn = new wxdb (env-> gethenv ());
If (! Conn-> open (ENV ))
{
Wxmessagebox ("cannot establish a connection", "DB connection error", wxok | wxicon_exclamation );
}
// Method 2
Conn = wxdbgetconnection (ENV );
Steps for using wxtable to access the database
1)Define datasource connection information// Set the data source connection information
// Use the wxdbconnectinf class object to set the data source connection information:
Wxdbconnectinf (henv, // ODBC environment handle
Const wxstring & DSN, // Data Source
Const wxstring & userid = "", // database username
Const wxstring & password, // Database Password
Const wxstring & defaultdir = "", // specific path (optional)
Const wxsting & Description = "", // null by default
Const wxstring & filetype = "") // null by default
/*************************************** **************************************** **
See wxdbconnectinf in Wx help. henv is generally set to null and is automatically created during construction. Of course, you can also use
Allochenv () Allocation
**************************************** **************************************** */
If (! Dbconnectinf |! Dbconnect-> gethenv ())
{
// Connect and determine whether the connection result is successful
}
2)Get a datasource connection// Obtain the data connection
// Use the wxdbgetconnection () method and wxdb () Class Object to obtain the data connection
Wxdbgetconnection (wxdb * infconfig); // infconfig: Data Source settings
Wxdb * DB = NULL;
DB = wxdbgetconnection (dbconnectinf );
If (! DB)
{
// Determine whether the connection is obtained, that is, whether the database is true;
}
3)Create Table Definition// Create a table object
Wxdbtable (wxdb * pwxdb, // wxdb pointer
Const wxstring & tblname, // database table name
Const uword numcolumns, // Number of table columns
Const wxstring & qrytblname = "", // name of the queried data table
Bool qryonly =! Wxdb_query_only, // obtain the read/write permission of the data.
Const wxstring & tablpath = "") // some data sources require specific paths
// This step can be automatically generated using the dbwizard generation tool. If you manually writeCodeAs follows:
Wxdbtable * Table = NULL;
Const wxchar tablename [] = wxt ("test/* Table name */");
Const uword numtablecolumns = 2;
Wxchar fiestname [50 + 1]; // data member of the table, used to bind fields in the database
Wxchar lastname [50 + 1];
Table = new wxdbtbale (dB,
Tablename,
Numtablecolumns,
Wxt (""),
! Wxdb_query_only,
Wxt (""));
/* Bind the variables in the table with fields in the database */
Table-> setcoldefs (0, wxt ("first_name"), db_data_type_varchar, firsname, SQL _c_wxchar, sizeof (firstname), true, true );
Table-> setcoldefs (0, wxt ("last_name"), db_data_type_varchar, firsname, SQL _c_wxchar, sizeof (lastname), true, true );
/*************************************** **************************************** **
When you understand a table, you must take it as an object. This object is created based on your needs.
The tables in the Database correspond. Our control over this object is actually the control of tables in the database, and the data of this object
For example, when we create an object, we do not need all fields in the database table.
Setcoldefs () is required to bind the required field. Note that tablename is the table name in the database.
Create a vwxdbtable object when you need to access the table. It is simple to understand this operation.
If we create a new table name, we can use the creat () function to create a new table in the database.
**************************************** **************************************** */
4)Open the table// Open the table
If (! Table-> open ())
{
// Determine whether the table is successfully opened
}
5)User the table// Use table
// Add a new record
Wxstrcpy (firstname, wxt ("Julian"); // The Field bound to firstname will add a record
/*************************************** **************************************** **
Similarly, note that the wxchar field cannot be assigned with a = value, but wxstrcpy is the only option. Int, long, and so on.
Direct = value assignment
**************************************** **************************************** */
If (! Table-> insert ())
{
// Determine whether the addition is successful
}
// Submit an update database
Table-> getdb ()-> committrans ();
// Query records, which are actually executed using SQL statements
Wxstring SQL = "select * from table" // write a query statement based on your needs
If (Table-> querybysqlstmt (SQL ))
{
// Determine whether the query is successful
}
// Delete a record
If (! Table-> deletewhere (where) // Where indicates that the deletion condition can be an SQL statement.
{
// Determine whether the deletion is successful
}
6)Close the table// Close the table
// Delete the used table
Wxdelete (table );
7)Close the datasource connection// Close the connection
// Release the connection
Wxdbfreeconnection (db );
DB = NULL;
// Close all connections
Wxdbcloseconnections ();
8)Release the ODBC environment handle// Release the ODBC environment handle
Wxdelete (dbconnectinf );
Access the database without using wxtable
The database connection is still in use.
Then, use the methods of the wxdb object such as exesql (), getdata (), and getnext () to implement
Database Operations
1)Execsql ()Function:// Actually execute the SQL statement
Bool execsql (const wxstring & psqlstmt) // SQL statement
Bool execsql (const wxstring & psqlstmt, // SQL statement
Wxdbcolinf ** columns, // wxdbcolinf object pointer
Short & numcols) // numcols: parameter variables involved
2)Getdata ()Function:// Query results
Bool getdata (uword colnumber, // column of the result to be queried
Sword ctype, // data type of the returned value
PTR pdata, // store the query result Buffer Pool
Sdword maxlen, // buffer pool size
Sdword far * cbreturned) // the size of the query result in the buffer pool
3)Getnext ()Function:// Return the downlink of the cursor at the current position
// The getfirst () and getlast () functions. For more information, see the Wx help;
4)Sample Code:
Wxdbconnectinf * dbconnectinf = NULL;
Wxdb * PDB = NULL;
Dbconnectinf = new wxdbconnectinf (null, // henv is generally null and can be set by yourself
Wxt (""), // This is the data source (DSN)
Wxt (""), // database username
Wxt (""); // Database Password
If (! Dbconnectinf |! Dbconnctinf-> gethenv ())
{
// Check whether the connection is successful
}
PDB = wxdbgetconnection (dbconnectinf );
If (! DB)
{
// Determine whether the connection is successful
}
/* Query the database to query the large database tables in the database */
Sdword CB; // the size of the returned result to determine whether the query result exists.
Wxchar reqqty [50 + 1]; // store the query results
Wxchar reqqty1 [50 + 1]; // store the query results
Wxstring sqlstmt; // SQL statement
Sqlstmt = "select * from" // table where condition;
Sqlstmt + = SQL _users; // # define SQL _users "users"
// Wxmessagebox (sqlstmt); // test whether the SQL statement is correct
If (! PDB-> execsql (sqlstmt. c_str () // execute the SQL statement
{
// Determine whether execution is successful
}
Int n = 0; // Number of table rows
While (PDB-> getnext () // obtain the next record at the current cursor position
{
If (! PDB-> getdata (2, db_data_type_varchar, & repqty1, sizeof (reqqty1), & CB ))
{
// Determine whether data is obtained successfully
}
If (cb = SQL _null_data)
{
// Determine whether the content is read
}
/* Put the obtained data into the control. Grid is used as an example */
M_grid-> appendrows (1 );
M_grid-> appendcols (2 );
M_grid-> setcellvalue (reqqty, N, 0 );
M_grid-> setcellvalue (reqqty, N, 1 );
N ++;
}
5)Clean up the database
Wxdbfreeconnection (PDB );
PDB = NULL;
Wxdbcloseconnections ();
Wxdelete (dbconnectinf );