How to Use YC ++ to operate databases
In YC ++, you can operate databases in multiple ways. Here is an example of database programming using ODBC specifications.
ODBC provides many API functions. These functions and their structures, macros, and other declarations are included in the header file sqlucode. H. These functions
The binary code is in the odbc32.dll library.
In VC ++, To Use odbc api functions, you must include sqlucode. h and specify the link file odbc32.lib.
In YC ++, the compiler contains the header file yc01/include/YCA. h by default. You can see that YCA. H has a header file sqlucode. h,
YC ++ automatically links to YCA. all library functions declared in H, so in YC ++, you do not have to include YCA. you do not need to specify the dll library.
/*************************************** **************************************** **********************************/
Example:
/*************************************** **************************************** **********************************/
To run this example, You need to upload the following YC ++ files to the directory where your source program is located:
Yc01/yxbapi. dll C/C ++ compiler, browser kernel Library
Yc01/yxbimg. dll image, animation decoding library
Yc01/yxbext. dll browser kernel interaction code library
Yc01/examples/Y. mdb database file, which is created using MS Access, with only one table: Me
/*************************************** **************************************** **********************************/
Save the following code to a file named after it, such as blog4a. cpp.
In YC ++, call <file/open or create CPP source program> blog4a. cpp, and then run <tool/execute> blog4a. cpp
In dos, use ycc blog4a. cpp to generate blog4a.exe, and then run blog4a.exe
In VC ++, use Cl blog4a. cpp to generate blog4a.exe, and then run blog4a.exe
/*************************************** **************************************** **********************************/
# The ycc identifier is defined in the header file ycansi. h of ifndef ycc // YC ++, which is not defined by other compilers.
# Include <windows. h>
# Include <stdio. h>
# Include <sqlucode. h>
// # Include <sqltypes. h>
// # Include <sqlext. h>
// # Include <SQL. h>
# Pragma comment (Lib, "user32.lib ")
# Pragma comment (Lib, "odbc32.lib ")
# Endif
# Define max_db_fileds 100 // maximum number of fields
# Define max_db_strlen 1024 // Maximum length of the field
Struct dbobject
{
Udword fieldlen [max_db_fileds];
Sword fsqltype [max_db_fileds];
Sqlchar fieldstr [max_db_fileds] [max_db_strlen];
Sqlsmallint fieldnum;
Sqlhenv henv;
Sqlhdbc hdbc;
Sqlhstmt hstmt;
Sqlchar colname [max_db_fileds] [256];
};
Void main ()
{
Char mdb_name [] = "Y. mdb"; // name of the database file to be operated
Char table_name [] = "me"; // name of the database table to be operated
Dbobject RDB;
If (sqlallocenv (& RDB. henv )! = SQL _success) return; // allocate an ODBC Environment
If (sqlallocconnect (RDB. henv, & RDB. hdbc )! = SQL _success) // allocate the connection memory
{
Sqlfreeenv (RDB. henv );
Return;
}
Sqlchar szconnstr [1024], constr [max_path + 128];
Sprintf (char *) constr, "DBQ = % s; driver = {Microsoft Access Driver (*. mdb)}", mdb_name );
// Load the driver and connect to the source
If (sqldriverconnect (RDB. hdbc, null, constr, strlen (char *) constr), szconnstr, sizeof (szconnstr), null, SQL _driver_noprompt )! = SQL _success)
{
Sqlfreeconnect (RDB. hdbc );
Sqlfreeenv (RDB. henv );
Return;
}
If (sqlallocstmt (RDB. hdbc, & RDB. hstmt )! = SQL _success) // allocate memory for SQL statements
{
Sqldisconnect (RDB. hdbc );
Sqlfreeconnect (RDB. hdbc );
Sqlfreeenv (RDB. henv );
Return;
}
// Set SQL statements
Sqlchar szstmt [255];
Strcpy (char *) szstmt, "select * from ");
Strcat (char *) szstmt, table_name );
Sqlexecdirect (RDB. hstmt, szstmt, SQL _ets); // execute ODBC statements
Sqlnumresultcols (RDB. hstmt, & RDB. fieldnum );
RDB. fieldnum = min (RDB. fieldnum, max_db_fileds );
For (int ii = 0; II <RDB. fieldnum; II ++)
{
Sqldescribecol (RDB. hstmt, II + 1, RDB. colname [II], sizeof RDB. colname [II], null, & RDB. fsqltype [II], & RDB. fieldlen [II], null, null );
Sqlbindcol (RDB. hstmt, II + 1, SQL _c_default, RDB. fieldstr [II], max_db_strlen, null );
}
// Print
Void print_db (dbobject * prdb );
Print_db (& RDB );
// Exit
Sqlclosecursor (RDB. hstmt );
Sqlfreestmt (RDB. hstmt, SQL _close );
Sqldisconnect (RDB. hdbc );
Sqlfreeconnect (RDB. hdbc );
Sqlfreeenv (RDB. henv );
}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////
Void bintochar (char * outstr, char * instr, sdword count)
{
Char * ostr = outstr;
While (count --)
{
Uchar uletter = (* instr & 0xf0)> 4;
If (uletter <= 9) * ostr ++ = uletter + '0 ';
Else * ostr ++ = 'A' + (uletter-10 );
Uletter = * instr ++ & 0x0f;
If (uletter <= 9) * ostr ++ = uletter + '0 ';
Else * ostr ++ = 'A' + (uletter-10 );
}
* Ostr = '/0 ';
}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////
Void get_db_field_text (dbobject * prdb, int Col, char * outbuff)
{
Static char szdate [] = "% 02u/% 02u/% 02u ";
Static char sztime [] = "% 02u: % 02u: % 02u ";
Static char sztimestmp [] = "% 02u/% 02u/% 02u % 02u: % 02u: % 02u. % lu ";
Char * inbuff = (char *) prdb-> fieldstr [col];
* Outbuff = '/0 ';
Switch (prdb-> fsqltype [col])
{
Case SQL _char:
Case SQL _varchar:
Case SQL _longvarchar:
Strcpy (outbuff, inbuff );
Break;
Case SQL _binary:
Case SQL _varbinary:
Case SQL _longvarbinary:
Strcpy (outbuff, "0x ");
Bintochar (outbuff + 2, (char *) inbuff, prdb-> fieldlen [col]);
Break;
Case SQL _tinyint:
Case SQL _smallint:
Sword far * tmpsword;
Tmpsword = (sword far *) inbuff;
Wsprintf (outbuff, "% d", * tmpsword );
Break;
Case SQL _bit:
Tmpsword = (sword far *) inbuff;
Strcpy (outbuff, (* tmpsword )? "1": "0 ");
Break;
Case SQL _integer:
Case SQL _bigint:
Sdword far * tmpsdword;
Tmpsdword = (sdword far *) inbuff;
Wsprintf (outbuff, "% lD", * tmpsdword );
Break;
Case SQL _float:
Case SQL _double:
Sdouble far * tmpsdouble;
Tmpsdouble = (sdouble far *) inbuff;
Sprintf (outbuff, "% FG", * tmpsdouble );
Break;
Case SQL _real:
Sfloat far * tmpsfloat;
Tmpsfloat = (sfloat far *) inbuff;
Sprintf (outbuff, "% FG", * tmpsfloat );
Break;
Case SQL _decimal:
Case SQL _numeric:
Strcpy (outbuff, inbuff );
Break;
Case SQL _date:
Date_struct far * tmpdate;
Tmpdate = (date_struct far *) inbuff;
Wsprintf (outbuff, szdate, tmpdate-> month, tmpdate-> day, tmpdate-> year );
Break;
Case SQL _time:
Time_struct far * tmptime;
Tmptime = (time_struct far *) inbuff;
Wsprintf (outbuff, sztime, tmptime-> hour, tmptime-> minute, tmptime-> second );
Break;
Case SQL _timestamp:
Timestamp_struct far * tmptimestmp;
Tmptimestmp = (timestamp_struct far *) inbuff;
Wsprintf (outbuff, sztimestmp, tmptimestmp-> year, tmptimestmp-> month, tmptimestmp-> day, tmptimestmp-> hour, tmptimestmp-> minute, tmptimestmp-> second, tmptimestmp-> fraction );
Break;
}
}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////
Void print_db (dbobject * prdb)
{
Printf ("/N ");
For (INT Col = 0; Col <prdb-> fieldnum; Col ++)
{
Printf (char *) prdb-> colname [col]);
Printf ("/t ");
}
For (;;)
{
If (sqlfetch (prdb-> hstmt )! = SQL _success) break;
Printf ("/N ");
For (INT Col = 0; Col <prdb-> fieldnum; Col ++)
{
Char des_buf [max_db_strlen];
Get_db_field_text (prdb, Col, des_buf );
Printf (des_buf );
Printf ("/t ");
}
}
Printf ("/N ");
}