Ознакомиться с базовыми функциями выборки данных в ODBC и разработать с их использованием программу получения данных из результирующего множества.
Задание
1. Убедиться в наличии и заполненности базы данных поставщиков, деталей, изделий, поставок.
2. Разработать ODBC-программу для решения задачи из соответствующего варианта с помощью функций SQLBindCol(), SQLFetch().
3. Разработать ODBC-программу для решения задачи из соответствующего варианта с помощью функций SQLFetch(), SQLGetData().
4. После выполнения лабораторной работы привести базу данных в исходное состояние.
Выполнение
Вывести информацию о поставщиках, поставивших указанную деталь в заданный период (с использованием функций SQLBinCol() и SQLFetch()).
Если задать деталь с номером Р1, то из таблицы поставок можно получить, что поставщиками, которые поставляют данную деталь являются поставщики с номерами S1 и S5.
Программа, реализующая выполнение.
#include <sqlext.h>
#include <sqltypes.h>
#include <strings.h>
#include <stdio.h>
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
void End(HENV env); /* Exit with rollback transaction */
int main (void)
{
HENV env; /* Environment descriptor */
HDBC dbc; /* Connection descriptor */
HSTMT stmt; /* Operator's identifier */
UCHAR driver[32]; /* Server name */
RETCODE code; /* Return code */
UCHAR errmsg[SQL_MAX_MESSAGE_LENGTH], sqlstate[10];
SDWORD nativeerr;
SWORD actualmsglen;
UCHAR sql[255]; /* Query string */
SDWORD cbTest = SQL_NTS;
UCHAR n_det[7]; /* Detail number */
UCHAR n_post[7]; /* Supplier number */
UCHAR surname[21]; /* Supplier surname */
SDWORD rating; /* Supplier rating */
UCHAR city[21]; /* Supplier city */
SDWORD val_n_post,val_surname,val_rating,val_city;
//-----------------------------------------------------------------------------
printf("Enter detail name: ");
scanf("%s",n_det);
//----------------------------------------------------------------------------/* Set enviroment descriptor */
code = SQLAllocEnv(&env);
if ((code == SQL_SUCCESS)||(code == SQL_SUCCESS_WITH_INFO))
printf("Enviroment descriptor is allocated...\n");
else
{
printf("Error when try to set enviroment descriptor!\n");
End(env);
}
//----------------------------------------------------------------------------/* Set connection descriptor */
code = SQLAllocConnect(env,&dbc);
if ((code == SQL_SUCCESS)||(code == SQL_SUCCESS_WITH_INFO))
printf("Connection descriptor is allocated...\n");
else
{
printf("Error when try to set connection descriptor!\n");
End(env);
}
//----------------------------------------------------------------------------/* Connection with data source */
code = SQLConnect(dbc,"MYDB",SQL_NTS,"pm8304",SQL_NTS,"novosibirsk",SQL_NTS);
if (code != SQL_SUCCESS)
{
printf("Error when try to connect to data source!\n");
End(env);
}
printf("Connection with data source is successful...\n");
//----------------------------------------------------------------------------/* Set operator's identifier */
code = SQLAllocStmt (dbc, &stmt);
if ((code == SQL_SUCCESS) || (code == SQL_SUCCESS_WITH_INFO))
printf("Operator's identifier is allocated...\n");
else
{
printf("Error is occured while allocated operator's identifier!\n");
SQLError(env,dbc,stmt,sqlstate, &nativeerr, errmsg,
SQL_MAX_MESSAGE_LENGTH - 1, &actualmsglen);
printf("SQLSTATE = %s\nNATIVE ERROR = %d\n", sqlstate, nativeerr);
errmsg[actualmsglen] = '\0';
printf("MSG = %s\n", errmsg);
End(env);
}
//----------------------------------------------------------------------------/* Create query */
strcpy((char*)sql,"\0");
strcat((char*)sql,"SELECT n_post,surname,rating,city FROM s WHERE n_post in
(SELECT DISTINCT n_post FROM spj WHERE n_det = ?)");
/* Prepare query */
code = SQLPrepare(stmt,sql,(SDWORD)strlen((char*)sql));
if (code == SQL_ERROR) { printf("Query is not prepared...\n"); End(env);}
printf("Query is prepared...\n");
/* Set parameters values */
code = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,6,0,
n_det,0,&cbTest);
if (code == SQL_ERROR) End(env);
printf("Parameters are defined...\n");
/* Execute query */
code = SQLExecute(stmt);
if ((code == SQL_SUCCESS) || (code == SQL_SUCCESS_WITH_INFO))
printf("Query has been executed successful...\n");
else
{
printf("Error is occured during query execution!\n");
SQLError(env,dbc,stmt,sqlstate, &nativeerr, errmsg,
SQL_MAX_MESSAGE_LENGTH - 1, &actualmsglen);
printf("SQLSTATE = %s\nNATIVE ERROR = %d\n", sqlstate, nativeerr);
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.