#include #include #include #include #define DATA_SOURCE "postgres" // .odbc.ini に記述したデータソース名 #define USER "is2008tl003" // postgresqlのユーザ名 #define PASSWD "nanifu03" // postgresqlのパスワード #define SQL_INSERT_ZAIKO "insert into zaiko (item_id,quantity) values (?,?)"//在庫への挿入 #define SQL_UP_ZAIKO "update zaiko set quantity = ? where item_id = ?"//在庫の更新 #define SQL_SELECT_ZAIKO "select quantity from zaiko where item_id = ?"//在庫から量を取得 #define SQL_INSERT_NYUSYUTU "insert into nyusyutu (date,dealer_id,item_id,operation,price,quantity) values (?,?,?,?,?,?)"//入出庫管理表への挿入 #define SQL_SELECT_D "select dealer_id , dealer_name , person_name , tell_number from dealer_table"//dealer一覧表示 #define SQL_SELECT_I "select dealer_id , dealer_name , person_name , tell_number from dealer_table"//item一覧表示 #define SQL_SELECT_TIME "select present_time from calender" #define MAX_DATA_LENGTH (256) #define MAX_DATA (256) struct nyusyutu{ TIMESTAMP_STRUCT *present_time; char dealer_id[10];//ディーラID char item_id[10]; // char operation[256];// int price;//価格 int quantity;//量 }; struct zaiko{ char item_id[10]; int quantity; }; main() { HENV henv; // 環境ハンドル HDBC hdbc; // 接続ハンドル HSTMT hstmt; // ステートメント・ハンドル RETCODE rc; //ディーラ表 int count; char dealer_id[10]; // ディーラーID char dealer_name[256]; // ディーラー名 char person_name[256]; // 担当者名 char tell_number[30]; // 電話番号 char key[256]; //商品表 char item_id[MAX_DATA]; // 商品ID char item_name[MAX_DATA]; // 品名 char item_article[MAX_DATA]; // 品目 char item_maker [MAX_DATA]; // メーカー int select_quantity; int operation; int price; int quantity; char buf[256]; int judge = 1; char *source = DATA_SOURCE; char *user = USER; char *passwd = PASSWD; int source_length; int user_length; int passwd_length; char *sql_insert_nyusyutu = SQL_INSERT_NYUSYUTU;//入出庫管理表への挿入 char *sql_insert_zaiko = SQL_INSERT_ZAIKO;//在庫表への挿入 char *sql_up_zaiko = SQL_UP_ZAIKO;//在庫表を更新 char *sql_select_zaiko = SQL_SELECT_ZAIKO;//在庫の量を表示 char *sql_select_d = SQL_SELECT_D;//ディーラ表示 char *sql_select_i = SQL_SELECT_I;//商品表示 char *sql_select_time = SQL_SELECT_TIME;//現在の日時を取得 struct nyusyutu *nyusyutu; struct zaiko *zaiko; SDWORD parameterData; // 書き込むパラメータのためのデータ SDWORD resultLength; // 読み出されたデータの大きさ // int anyRecord; // レコードがあることを示すフラグ while(judge){ // nyusyutuにメモリを割り付け if((nyusyutu =(struct nyusyutu *)malloc(sizeof(struct nyusyutu))) == NULL){ fprintf(stderr, "レコードにメモリが確保できませんでした\n"); exit(0); } // 現在時刻へのメモリ割りつけ if((nyusyutu->present_time = (TIMESTAMP_STRUCT *)malloc(sizeof(TIMESTAMP_STRUCT))) == NULL) { fprintf(stderr,"現在時刻にメモリが確保できませんでした\n"); exit(0); } // zaikoにメモリを割り付け if((zaiko = (struct zaiko *)malloc(sizeof(struct zaiko))) == NULL) { fprintf(stderr, "レコードにメモリが確保できませんでした\n"); exit(0); } // 環境ハンドル割り付け SQLAllocEnv(&henv); // 接続ハンドル割り付け SQLAllocConnect(henv, &hdbc); // データソースに接続 source_length = strlen(source); user_length = strlen(user); passwd_length = strlen(passwd); rc = SQLConnect(hdbc, // 接続ハンドル DATA_SOURCE, source_length, // データソース名とその長さ user, user_length, // ユーザ名とその長さ passwd, passwd_length); // パスワードとその長さ if(rc != SQL_SUCCESS) { fprintf(stderr, "データソースに接続に失敗しました(user:%s, passwd:%s)\n", user, passwd); print_SQL_err(henv, hdbc, SQL_NULL_HSTMT); goto prologue; } //現在時刻の取得 // ステートメント・ハンドル割り付け SQLAllocStmt(hdbc, &hstmt); // パラメータと表内の列の結びつけ // 現在時刻 SQLBindCol(hstmt, 1, SQL_C_TIMESTAMP,nyusyutu->present_time,sizeof(TIMESTAMP_STRUCT), &resultLength); // SELECT文の実行 rc = SQLExecDirect(hstmt, sql_select_time, SQL_NTS); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } if((rc = SQLFetch(hstmt)) != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } printf("現在時刻は: %d-%d-%d %d:%d:%dです.\n", nyusyutu->present_time->year, nyusyutu->present_time->month, nyusyutu->present_time->day, nyusyutu->present_time->hour, nyusyutu->present_time->minute, nyusyutu->present_time->second); SQLFreeStmt(hstmt,SQL_DROP); //ディーラ一覧 // ステートメント・ハンドル割り付け SQLAllocStmt(hdbc, &hstmt); // 変数と表内の値の結びつけ // ディーラーID SQLBindCol(hstmt, 1,SQL_C_CHAR,dealer_id,256,&resultLength); // ディーラー名 SQLBindCol(hstmt, 2, SQL_C_CHAR, dealer_name, 256, &resultLength); // 担当者名 SQLBindCol(hstmt, 3, SQL_C_CHAR, person_name, 256, &resultLength); // 電話番号 SQLBindCol(hstmt, 4, SQL_C_CHAR, tell_number, 30 , &resultLength); //検索する担当者名 parameterData = SQL_NTS; // 整数型を書き込むときは0 rc = SQLBindParameter(hstmt, 1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR, 128, 0, key,256,¶meterData); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // SELECT文の実行 rc = SQLExecDirect(hstmt, sql_select_d, SQL_NTS); // SQL_NTS は, sql_statement がヌルコードで終了していることを示す if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // 1行ずつfetch(データを取る) count = 0; do { if((rc = SQLFetch(hstmt)) != SQL_SUCCESS) break; count++; // ディーラーID printf("ディーラーID : %s\t", dealer_id); // ディーラー名 printf("ディーラー名 : %s\t", dealer_name); // 担当者名 printf("担当者名 : %s\t", person_name); // 電話番号 printf("電話番号 : %s\t", tell_number); printf("\n"); } while(rc == SQL_SUCCESS); if(count == 0) { printf("No data found\n"); } else { printf("%d data found\n", count); } SQLFreeStmt(hstmt,SQL_DROP); //ディーラidの指定 printf("ディーラidを指定してください\n"); fgets(buf,256,stdin); sscanf(buf,"%s",dealer_id); strcpy(nyusyutu->dealer_id,dealer_id); //商品一覧 // ステートメント・ハンドル割り付け SQLAllocStmt(hdbc, &hstmt); // 変数と表内の値の結びつけ // 商品ID SQLBindCol(hstmt, 1,SQL_C_CHAR,item_id,MAX_DATA,&resultLength); // 品名 SQLBindCol(hstmt, 2, SQL_C_CHAR, item_name, MAX_DATA, &resultLength); // 品目 SQLBindCol(hstmt, 3, SQL_C_CHAR, item_article, MAX_DATA, &resultLength); // メーカー SQLBindCol(hstmt, 4, SQL_C_CHAR, item_maker, MAX_DATA, &resultLength); //検索する品目 parameterData = SQL_NTS; // 整数型を書き込むときは0 rc = SQLBindParameter(hstmt, 1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,256, 0,key,MAX_DATA,¶meterData); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // SELECT文の実行 rc = SQLExecDirect(hstmt, sql_select_i, SQL_NTS); // SQL_NTS は, sql_statement がヌルコードで終了していることを示す if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // 1行ずつfetch(データを取る) count = 0; do { if((rc = SQLFetch(hstmt)) != SQL_SUCCESS) break; count++; // 商品ID printf("商品ID : %s\t", item_id); // 品名 printf("品名 : %s\t", item_name); // 品目 printf("品目 : %s\t", item_article); // メーカー printf("メーカー : %s\t", item_maker); printf("\n"); } while(rc == SQL_SUCCESS); if(count == 0) { printf("No data found\n"); } else { printf("%d data found\n", count); } SQLFreeStmt(hstmt,SQL_DROP); //商品IDの指定 printf("商品IDを指定してください\n"); fgets(buf,256,stdin); sscanf(buf,"%s",item_id); strcpy(nyusyutu->item_id,item_id); // strcpy(zaiko->item_id,item_id); //printf("dbg%s\n",item_id); //操作の指定 printf("納入か発注を指定してください(納入なら1,発注なら2を入力してください)\n"); fgets(buf,256,stdin); sscanf(buf,"%d",&operation); //価格の指定 printf("価格を指定してください\n"); fgets(buf,256,stdin); sscanf(buf,"%d",&price); nyusyutu->price = price; //量の指定 printf("量を指定してください\n"); fgets(buf,256,stdin); sscanf(buf,"%d",&quantity); nyusyutu->quantity = quantity; // zaiko-> quantity = quantity; //在庫管理表から量を取得 // ステートメント・ハンドル割り付け SQLAllocStmt(hdbc, &hstmt); // 変数と表内の値の結びつけ /* // 商品ID SQLBindCol(hstmt, 1, SQL_C_CHAR, zaiko->item_id, 10, &resultLength); */ // 商品ID parameterData = SQL_NTS; rc = SQLBindParameter(hstmt, 1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,10, 0, item_id,MAX_DATA_LENGTH,NULL); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // 量 SQLBindCol(hstmt, 1, SQL_C_LONG,&(select_quantity), sizeof(int), &resultLength); // SELECT文の実行 rc = SQLExecDirect(hstmt, sql_select_zaiko, SQL_NTS); // SQL_NTS は, sql_statement がヌルコードで終了していることを示す if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } do { if((rc = SQLFetch(hstmt)) != SQL_SUCCESS) break; if(strcmp(zaiko->item_id, item_id)==0){ break; } } while(rc == SQL_SUCCESS); if(zaiko->item_id != item_id){ zaiko->quantity = 0; }else{ zaiko->quantity = quantity; } SQLFreeStmt(hstmt,SQL_DROP); // printf("%d",select_quantity); //倉庫内の量の検索と不足のチェック switch(operation){ case 1: zaiko->quantity += nyusyutu->quantity; strcpy(nyusyutu->operation, "納入"); break; case 2: zaiko->quantity -= nyusyutu->quantity; if(zaiko->quantity < 0){ printf("在庫が不足しています.\n"); goto restart; // continue; } strcpy(nyusyutu->operation, "発注"); break; default: printf("指定されていない番号です.\n"); break; } //printf("\n%d\n",select_quantity); // 在庫管理表の更新 if((strcmp(zaiko->item_id,item_id))==0){ // ステートメント・ハンドル割り付け SQLAllocStmt(hdbc, &hstmt); // パラメータと表内の列の結びつけ // 量 zaiko->quantity = quantity + select_quantity; //printf("%d",zaiko->quantity); parameterData = 0; // 整数型を書き込むときは0 rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,SQL_C_LONG,SQL_INTEGER,0,0, &(zaiko->quantity), sizeof(int), ¶meterData); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } strcpy(zaiko->item_id,item_id); // 商品ID parameterData = SQL_NTS; // 文字列を書き込むときはSQL_NTS rc = SQLBindParameter(hstmt, 2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,10, 0, zaiko->item_id, MAX_DATA_LENGTH, NULL); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } //printf("%s",zaiko->item_id); // UPDATE文の準備 // (準備だけして、あとからSQLExecute()で挿入すると毎回SQLDirectExec()で挿入するより速い) rc = SQLPrepare(hstmt, sql_up_zaiko, SQL_NTS); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // UPDATE文の実行 rc = SQLExecute(hstmt); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } SQLFreeStmt(hstmt,SQL_DROP);//ステートメントハンドルの解放 }else{ //在庫管理表の挿入 // ステートメント・ハンドル割り付け SQLAllocStmt(hdbc, &hstmt); strcpy(zaiko->item_id,item_id); // パラメータと表内の列の結びつけ // 商品ID parameterData = SQL_NTS; // 文字列を書き込むときはSQL_NTS rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,10, 0, zaiko->item_id, MAX_DATA_LENGTH, NULL); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // zaiko->quantity = quantity + select_quantity; // 量 parameterData = 0; // 整数型を書き込むときは0 rc = SQLBindParameter(hstmt,2, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,0, 0, &(zaiko->quantity), sizeof(int), ¶meterData); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // INSERT文の準備 // (準備だけして、あとからSQLExecute()で挿入すると毎回SQLDirectExec()で挿入するより速い) rc = SQLPrepare(hstmt, sql_insert_zaiko, SQL_NTS); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // INSERT文の実行 rc = SQLExecute(hstmt); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } SQLFreeStmt(hstmt,SQL_DROP);//ステートメントハンドルの解放 // printf("在庫に挿入\n"); } //入出庫管理表への挿入 // ステートメント・ハンドル割り付け SQLAllocStmt(hdbc, &hstmt); // パラメータと表内の列の結びつけ] // 時間 parameterData = sizeof(TIMESTAMP_STRUCT); rc = SQLBindParameter(hstmt, 1,SQL_PARAM_INPUT,SQL_C_TIMESTAMP,SQL_TIMESTAMP, 8, 0,nyusyutu->present_time,sizeof(TIMESTAMP_STRUCT), ¶meterData); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // ディーラID parameterData = SQL_NTS; rc = SQLBindParameter(hstmt, 2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,10, 0, nyusyutu->dealer_id,MAX_DATA_LENGTH,NULL); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // 商品ID parameterData = SQL_NTS; rc = SQLBindParameter(hstmt, 3,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,10, 0, nyusyutu->item_id,MAX_DATA_LENGTH,NULL); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // 操作 parameterData = SQL_NTS; rc = SQLBindParameter(hstmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 128, 0, nyusyutu->operation, MAX_DATA_LENGTH, NULL); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // 価格 parameterData = 0; rc = SQLBindParameter(hstmt, 5,SQL_PARAM_INPUT,SQL_C_LONG,SQL_INTEGER,0, 0, &(nyusyutu->price),sizeof(int),¶meterData); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // 量 parameterData = 0; rc = SQLBindParameter(hstmt, 6,SQL_PARAM_INPUT,SQL_C_LONG,SQL_INTEGER,0, 0, &(nyusyutu->quantity),sizeof(int),¶meterData); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // INSERT文の準備 // (準備だけして、あとからSQLExecute()で挿入すると毎回SQLDirectExec()で挿入するより速い) rc = SQLPrepare(hstmt, sql_insert_nyusyutu, SQL_NTS); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // INSERT文の実行 rc = SQLExecute(hstmt); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } // 最後にまとめてコミット SQLTransact(SQL_NULL_HENV, hdbc, SQL_COMMIT); SQLFreeStmt(hstmt,SQL_DROP);//ステートメントハンドルの解放 //指定されたデータと現在の在庫量の表示 //在庫表からのデータ // ステートメント・ハンドル割り付け SQLAllocStmt(hdbc, &hstmt); // 変数と表内の値の結びつけ // 量 SQLBindCol(hstmt,1,SQL_C_LONG,&(zaiko->quantity), sizeof(int), &resultLength); // 商品ID parameterData = SQL_NTS; rc = SQLBindParameter(hstmt, 1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,10, 0, zaiko->item_id, MAX_DATA_LENGTH, ¶meterData); if(rc != SQL_SUCCESS) { print_SQL_err(henv, hdbc, hstmt); goto prologue; } printf("在庫管理表\n"); printf("商品ID: %s\n", zaiko->item_id); printf("量: %d\n\n",zaiko->quantity); printf("入出庫管理表\n"); printf("ディーラID: %s\n", nyusyutu->dealer_id); printf("商品ID: %s\n", nyusyutu->item_id); printf("操作: %s\n",nyusyutu->operation); printf("価格: %d\n",nyusyutu->price); printf("量: %d\n",nyusyutu->quantity); // 最後にまとめてコミット SQLTransact(SQL_NULL_HENV, hdbc, SQL_COMMIT); SQLFreeStmt(hstmt,SQL_DROP);//ステートメントハンドルの解放 restart: printf("プログラムを続ける場合は1を,止める場合は0を入力してください\n"); fgets(buf,256,stdin); sscanf(buf,"%d",&judge); } prologue: free(nyusyutu); free(nyusyutu->present_time); free(zaiko); SQLDisconnect(hdbc); SQLFreeConnect(hdbc); SQLFreeEnv(henv); return(0); }