(1)編譯:
treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ gcc t18.c `mysql_config --cflags --libs` -o t18
(2)執行:
treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ ./t18
貨品基本資料編修 請輸入選擇(1新增 2進貨 3刪除 4列出 0結束) sel=1 1:貨品基本資料新增程式 請輸入貨品編號=a001 SQL: [ select gdsno,gdsname,unit,cost,stock_qty,fac_no from new_goods where gdsno='a001' ] Execute ok!
找不到gdsno: a001!!
很好,編號(a001)沒有被使用過 請輸入名稱=蕃茄 請輸入貨品單價=50 請輸入貨品單位=斤 初始貨品數量為0 請輸入貨品供應商代號=088 SQL指令: [insert into new_goods (gdsno,gdsname,cost,unit,stock_qty,fac_no) values ('a001','蕃茄',50.00,'斤', 0, '088')] 執行成功!
貨品基本資料編修 請輸入選擇(1新增 2進貨 3刪除 4列出 0結束) sel=1 1:貨品基本資料新增程式 請輸入貨品編號=a001 SQL: [ select gdsno,gdsname,unit,cost,stock_qty,fac_no from new_goods where gdsno='a001' ] Execute ok!
match : a001 蕃茄 斤 50 0 088
a001已為[蕃茄]使用!
貨品基本資料編修 請輸入選擇(1新增 2進貨 3刪除 4列出 0結束) sel=4 4:貨品基本資料列表 SQL: [ select gdsno, gdsname, unit, cost, stock_qty, fac_no from new_goods ] Execute ok!
gdsno, gdsname, unit, cost, stock_qty, fac_no, a001, 蕃茄, 斤, 50, 0, 088, a002, 青椒, 斤, 45, -14, 099, a009, 咖啡, 杯, 50, 0, 099, a089, 楊桃, 斤, 50, 0, 099, b004, 黃瓜, 斤, 50, 2234, 099, b006, 蜂密, 斤, 1000, 0, 099, 計6筆資料
貨品基本資料編修 請輸入選擇(1新增 2進貨 3刪除 4列出 0結束) sel=2 2:進貨程式 請輸入貨品編號=a001 SQL: [ select gdsno,gdsname,unit,cost,stock_qty,fac_no from new_goods where gdsno='a001' ] Execute ok!
match : a001 蕃茄 斤 50 0 088 請輸入進貨數量=2500 SQL指令: [update new_goods set stock_qty=stock_qty+(2500) where gdsno='a001'] 執行成功!
貨品基本資料編修 請輸入選擇(1新增 2進貨 3刪除 4列出 0結束) sel=4 4:貨品基本資料列表 SQL: [ select gdsno, gdsname, unit, cost, stock_qty, fac_no from new_goods ] Execute ok!
gdsno, gdsname, unit, cost, stock_qty, fac_no, a001, 蕃茄, 斤, 50, 2500, 088, a002, 青椒, 斤, 45, -14, 099, a009, 咖啡, 杯, 50, 0, 099, a089, 楊桃, 斤, 50, 0, 099, b004, 黃瓜, 斤, 50, 2234, 099, b006, 蜂密, 斤, 1000, 0, 099, 計6筆資料
貨品基本資料編修 請輸入選擇(1新增 2進貨 3刪除 4列出 0結束) sel=3 3:貨品基本資料刪除程式 請輸入貨品編號=b009 SQL: [ select gdsno,gdsname,unit,cost,stock_qty,fac_no from new_goods where gdsno='b009' ] Execute ok!
找不到gdsno: b009!! 察無b009的資料
貨品基本資料編修 請輸入選擇(1新增 2進貨 3刪除 4列出 0結束) sel=3 3:貨品基本資料刪除程式 請輸入貨品編號=b006 SQL: [ select gdsno,gdsname,unit,cost,stock_qty,fac_no from new_goods where gdsno='b006' ] Execute ok!
match : b006 蜂密 斤 1000 0 099 SQL指令: [delete from new_goods where gdsno='b006'] 執行成功!
貨品基本資料編修 請輸入選擇(1新增 2進貨 3刪除 4列出 0結束) sel=4 4:貨品基本資料列表 SQL: [ select gdsno, gdsname, unit, cost, stock_qty, fac_no from new_goods ] Execute ok!
gdsno, gdsname, unit, cost, stock_qty, fac_no, a001, 蕃茄, 斤, 50, 2500, 088, a002, 青椒, 斤, 45, -14, 099, a009, 咖啡, 杯, 50, 0, 099, a089, 楊桃, 斤, 50, 0, 099, b004, 黃瓜, 斤, 50, 2234, 099, 計5筆資料
貨品基本資料編修 請輸入選擇(1新增 2進貨 3刪除 4列出 0結束) sel=0
謝謝使用本編修程式 treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$
(3)程式碼t18.c
treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ cat t18.c #include <my_global.h> #include <mysql.h> #define HOST "localhost" #define USER "treehrt" #define PWD "abc123abc123" #define DB "ilvssell" typedef struct GOODSREC { char gdsno[20]; char gdsname[30]; // char spec[10]; double cost; // double price_in; // double price_special; // double price_sell; char unit[4]; int stock_qty; char fac_no[20]; // char mem[80]; } goodsRec ;
void finish_with_error(MYSQL *con) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } int GetGoodsRecByNo(char *gdsno, goodsRec *pv) { char st[280]; sprintf(st,"select gdsno,gdsname,unit,cost,stock_qty,fac_no from new_goods where gdsno='%s'",gdsno); MYSQL *con = mysql_init(NULL); if (con == NULL) finish_with_error(con);
if (mysql_real_connect(con, HOST, USER, PWD, DB, 0, NULL, 0) == NULL) finish_with_error(con); mysql_query(con, "set names utf8"); if (mysql_query(con, st)) finish_with_error(con); printf("SQL: [ %s ] Execute ok! \n\n", st); MYSQL_RES *result = mysql_store_result(con); if (result == NULL) finish_with_error(con);
MYSQL_ROW row; row = mysql_fetch_row(result); if (row){ strcpy(pv->gdsno,row[0]); strcpy(pv->gdsname,row[1]); strcpy(pv->unit,row[2]); pv->cost=atof(row[3]); pv->stock_qty=atoi(row[4]); strcpy(pv->fac_no,row[5]); printf("match : %s %s %s %s %s %s\n",row[0],row[1],row[2],row[3],row[4],row[5]); mysql_free_result(result); return 1; } else { printf("找不到gdsno: %s!!\n",gdsno); mysql_free_result(result); return 0; } }
void add_goods_qty(char *gdsno, int qty) { char st[500]; sprintf(st,"update new_goods set stock_qty=stock_qty+(%d) where gdsno='%s'",qty,gdsno); MYSQL *con = mysql_init(NULL); if (con == NULL) finish_with_error(con); if (mysql_real_connect(con, HOST , USER, PWD, DB, 0, NULL, 0) == NULL) finish_with_error(con); if (mysql_query(con, st)) finish_with_error(con); printf("SQL指令: [%s] 執行成功! \n\n", st); mysql_close(con); }
void list_new_goods() { char st[500]; sprintf(st,"select gdsno, gdsname, unit, cost, stock_qty, fac_no from new_goods"); MYSQL *con = mysql_init(NULL); if (con == NULL) finish_with_error(con); if (mysql_real_connect(con, HOST , USER, PWD, DB, 0, NULL, 0) == NULL) finish_with_error(con); mysql_query(con, "set names utf8"); if (mysql_query(con, st)) finish_with_error(con); printf("SQL: [ %s ] Execute ok! \n\n", st);
MYSQL_RES *result = mysql_store_result(con); if (result == NULL) finish_with_error(con);
int num_fields = mysql_num_fields(result);
MYSQL_FIELD *field; char *headers[num_fields]; for(int k = 0; (field = mysql_fetch_field(result)); k++) { headers[k] = field->name; printf("%s, ",headers[k]); } printf("\n"); MYSQL_ROW row; int c=0; while ((row = mysql_fetch_row(result))) { for(int i = 0; i < num_fields; i++) printf("%s, ", row[i] ? row[i] : "NULL"); c++; printf("\n"); } mysql_free_result(result); mysql_close(con); printf("計%d筆資料\n",c); }
void insert_new_goods_rec(goodsRec gR){ // printf("待寫入的資料:gR.gdsno=%s,gR.gdsname=%s gR.unit=%s gR.cost=%.2f gR.stcok_qty=%d gR.fac_no=%s\n",gR.gdsno,gR.gdsname,gR.unit,gR.cost,gR.stock_qty,gR.fac_no); char st[500]; sprintf(st,"insert into new_goods (gdsno,gdsname,cost,unit,stock_qty,fac_no) values ('%s','%s',%.2f,'%s', %d, '%s')",gR.gdsno,gR.gdsname,gR.cost,gR.unit,gR.stock_qty,gR.fac_no) ; MYSQL *con = mysql_init(NULL); if (con == NULL) finish_with_error(con); if (mysql_real_connect(con, HOST , USER, PWD, DB, 0, NULL, 0) == NULL) finish_with_error(con); mysql_query(con, "set names utf8"); if (mysql_query(con, st)) finish_with_error(con); printf("SQL指令: [%s] 執行成功! \n\n", st); } void del_new_goods_rec(char * gdsno){ char st[500]; sprintf(st,"delete from new_goods where gdsno='%s'",gdsno) ; MYSQL *con = mysql_init(NULL); if (con == NULL) finish_with_error(con); if (mysql_real_connect(con, HOST , USER, PWD, DB, 0, NULL, 0) == NULL) finish_with_error(con); if (mysql_query(con, st)) finish_with_error(con); printf("SQL指令: [%s] 執行成功! \n\n", st); mysql_close(con); }
int main(int argc, char **argv) { char gdsno[20]; int sel=0; goodsRec gR; while(1){ printf("\n貨品基本資料編修\n"); printf("請輸入選擇(1新增 2進貨 3刪除 4列出 0結束) sel="); scanf("%d",&sel); if (sel==0) { printf("\n謝謝使用本編修程式\n"); exit(1); } if (sel==1){ printf("1:貨品基本資料新增程式\n請輸入貨品編號="); scanf("%s",gdsno); if(GetGoodsRecByNo(gdsno, &gR)==1){ printf("\n %s已為[%s]使用!\n",gdsno,gR.gdsname); } else{ strcpy(gR.gdsno,gdsno); printf("\n很好,編號(%s)沒有被使用過\n請輸入名稱=",gdsno); scanf("%s",gR.gdsname); printf("請輸入貨品單價="); scanf("%lf",&gR.cost); printf("請輸入貨品單位="); scanf("%s",gR.unit); gR.stock_qty=0; printf("初始貨品數量為%d\n",gR.stock_qty); printf("請輸入貨品供應商代號="); scanf("%s",gR.fac_no); insert_new_goods_rec(gR); } } if (sel==2){ printf("2:進貨程式\n請輸入貨品編號="); scanf("%s",gdsno); if(GetGoodsRecByNo(gdsno, &gR)==1){ int qty=0; printf("請輸入進貨數量="); scanf("%d",&qty); add_goods_qty(gdsno, qty); } else printf("察無%s的資料\n",gdsno); } if (sel==3){ printf("3:貨品基本資料刪除程式\n請輸入貨品編號="); scanf("%s",gdsno); if(GetGoodsRecByNo(gdsno, &gR)==1){ del_new_goods_rec(gdsno); } else printf("察無%s的資料\n",gdsno); } if (sel==4){ printf("4:貨品基本資料列表\n"); list_new_goods(); } } exit(0); } |